我在C程序中添加了一些需要大量内存的功能,每次运行它都会崩溃,但是在减少内存分配时它可以正常工作。使用GDB进行调试时,我发现一些奇怪的结果,例如赋值会更改其他指针,而printf不起作用。我以为这可能是内存问题,但是使用top
监视内存使用情况,我发现仍然有足够的内存。
最后,我使用Valgrind
来监视内存,即使valgrind内存不足也是如此:
==3449== Valgrind's memory management: out of memory:
==3449== memcheck:allocate new SecMap's request for 16384 bytes failed.
==3449== 7,625,605,120 bytes have already been mmap-ed ANONYMOUS.
==3449== Valgrind cannot continue. Sorry.
==3449==
==3449== There are several possible reasons for this.
==3449== - You have some kind of memory limit in place. Look at the
==3449== output of 'ulimit -a'. Is there a limit on the size of
==3449== virtual memory or address space?
==3449== - You have run out of swap space.
==3449== - Valgrind has a bug. If you think this is the case or you are
==3449== not sure, please let us know and we'll try to fix it.
==3449== Please note that programs can take substantially more memory than
==3449== normal when running under Valgrind tools, eg. up to twice or
==3449== more, depending on the tool. On a 64-bit machine, Valgrind
==3449== should be able to make use of up 32GB memory. On a 32-bit
==3449== machine, Valgrind should be able to use all the memory available
==3449== to a single process, up to 4GB if that's how you have your
==3449== kernel configured. Most 32-bit Linux setups allow a maximum of
==3449== 3GB per process.
==3449==
==3449== Whatever the reason, Valgrind cannot continue. Sorry.
但是使用ulimit -a
对视觉空间没有限制:
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 96254
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 96254
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
使用top
可以告诉服务器内存配置:
KiB Mem : 24678616 total, 13461572 free, 3457528 used, 7759516 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 20754480 avail Mem
那什么原因可能导致此结果?
更新:
操作系统:
Linux astl09 4.4.0-83-generic #106-Ubuntu SMP Mon Jun 26 17:54:43 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
程序为disksim 4.0。我将其用于一些研究。
valgrind:
valgrind-3.11.0