当使用Valgrind运行时,malloc返回null

Jay*_*y D 4 c c++ linux valgrind memory-management

我有一个巨大的分配(数十GB)与malloc调用发生在正常情况下工作.系统确实有一个巨大的RAM,是一台运行2.6 x86_64内核的64位机器.

mem rlimit已作为INFINITY以setrlimit完成.

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

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

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

有什么投入?

问候,-J

sar*_*old 9

请注意,这malloc(3)对你说谎 - 它实际上并不是一次性分配所有内存,它只是向操作系统询问它,操作系统就是这样malloc(3).这是完全正常的行为,大部分时间都可以正常工作./proc/sys/vm/overcommit_memoryin 的描述proc(5)包含详细信息:

   /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.
Run Code Online (Sandbox Code Playgroud)

Valgrind不能那么轻率; 它实际上跟踪进程的已分配,已初始化和未初始化的内存.因此,它需要比进程本身更多的内存,并且它对过度提交的内存没有相同的容忍度.

我不知道在valgrind下运行程序需要多少内存,但是尝试添加几个千兆字节的交换空间.您可以通过使用将零写入文件来创建新的交换文件dd - 不要使用稀疏文件 - 然后mkswap(8)在文件上运行以初始化它并swapon(8)使用文件名运行以告知系统将其用作交换文件.