我有一个巨大的分配(数十 GB)发生在正常情况下工作的 malloc 调用。该系统确实有一个巨大的 RAM,是一个运行 2.6 x86_64 内核的 64 位机器。
mem rlimit 已通过 setrlimit 设置为 INFINITY。
我想用 Valgrind 运行它来进行内存分析并检查是否有泄漏。
但是当使用 valgrind 运行时,malloc 失败并返回 NULL 指针。
我尝试减少分配的大小,但这没有帮助。
任何输入?
问候, -J
最佳答案
请注意 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)
在文件上初始化它并使用文件名运行 swapon(8)
以告诉系统将其用作交换文件。
关于c++ - 与 Valgrind 一起运行时,malloc 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10423992/