【问题标题】:malloc returns null when run with Valgrind使用 Valgrind 运行时 malloc 返回 null
【发布时间】:2012-05-03 01:37:34
【问题描述】:

在正常情况下工作的 malloc 调用发生了巨大的分配(数十 GB)。该系统确实具有巨大的 RAM,并且是运行 2.6 x86_64 内核的 64 位机器。

mem rlimit 已通过 setrlimit 完成为 INFINITY。

我想用 Valgrind 运行它来进行内存分析并检查泄漏。

但是当使用 valgrind 运行时 malloc 失败并返回 NULL 指针。

我尝试减少分配的大小,但没有帮助。

任何输入?

问候, -J

【问题讨论】:

  • Valgrind 使用大量 VM 来跟踪 VM 的其余部分。
  • 这个系统有多少内存?多少几十GB?

标签: c++ c linux memory-management valgrind


【解决方案1】:

请注意,malloc(3) 是在骗你——它实际上并没有一次分配所有内存,它只是向操作系统请求它,而操作系统对malloc(3) 说谎。这是完全正常的行为,大部分时间都可以正常工作。 proc(5)/proc/sys/vm/overcommit_memory 的描述包含详细信息:

   /proc/sys/vm/overcommit_memory
          This file contains the kernel virtual memory
          accounting mode.  Values are:

                 0: heuristic overcommit (this is the default)
                 1: always overcommit, never check
                 2: always check, never overcommit

          In mode 0, calls of mmap(2) with MAP_NORESERVE are not
          checked, and the default check is very weak, leading
          to the risk of getting a process "OOM-killed".  Under
          Linux 2.4 any nonzero value implies mode 1.  In mode 2
          (available since Linux 2.6), the total virtual address
          space on the system is limited to (SS + RAM*(r/100)),
          where SS is the size of the swap space, and RAM is the
          size of the physical memory, and r is the contents of
          the file /proc/sys/vm/overcommit_ratio.

Valgrind 不能这么轻率;它实际上跟踪进程的已分配、已初始化和未初始化的内存。因此,它比进程本身需要更多内存,并且它对过度使用内存的容忍度不同。

我不知道在 valgrind 下运行程序需要多少内存,但请尝试添加更多 GB 的交换空间。您可以通过使用dd 将零写入文件来创建一个新的交换文件——不要使用稀疏文件——然后在文件上运行mkswap(8) 来初始化它并运行@ 987654328@ 与文件名告诉系统将其用作交换文件。

【讨论】:

  • vm/overcommit_memory 设置为 2,这意味着它永远不会过度使用。我可能应该通过将其设置为 1 来进行测试。并且 overcommit_ratio 为 110 。
  • 我认为这不是问题所在。相反,valgrind 需要大量内存来跟踪/调试您使用的所有内存。如果我没记错的话,对于某些类型的分配,它甚至会复制它们以供自己的跟踪使用,因此分配 10gb 实际上可能需要 20gb...
  • @R..: 取决于检查运行,但是,memcheck 实际上复制了内存,因为它跟踪 bit 级别的写入。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-14
  • 2016-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-18
  • 2016-09-20
相关资源
最近更新 更多