当我运行它时 - valgrind 发现问题:
==3335==
==3335== HEAP SUMMARY:
==3335== in use at exit: 2,271,338,496 bytes in 2,218,104 blocks
==3335== total heap usage: 2,218,105 allocs, 0 frees, 2,271,338,496 bytes allocated
==3335==
==3335==
==3335== Valgrind's memory management: out of memory:
==3335== newSuperblock's request for 8876032 bytes failed.
==3335== 3116339200 bytes have already been allocated.
==3335== Valgrind cannot continue. Sorry.
==3335==
==3335== There are several possible reasons for this.
==3335== - You have some kind of memory limit in place. Look at the
==3335== output of 'ulimit -a'. Is there a limit on the size of
==3335== virtual memory or address space?
==3335== - You have run out of swap space.`
即使使用 O2 O3 也不能消除该错误。是完整的样本吗?
更新
该标志不会改变输出,但如果我在崩溃前中断程序,valgrind 会显示下一个:
^C1890 mb
==3286==
==3286== HEAP SUMMARY:
==3286== in use at exit: 1,981,808,640 bytes in 1,935,360 blocks
==3286== total heap usage: 1,935,360 allocs, 0 frees, 1,981,808,640 bytes allocated
==3286==
==3286== 276,480 bytes in 270 blocks are possibly lost in loss record 2 of 3
==3286== at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==3286== by 0x8048472: main (mem_test.c:13)
==3286==
==3286== 1,981,530,112 bytes in 1,935,088 blocks are definitely lost in loss record 3 of 3
==3286== at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==3286== by 0x8048472: main (mem_test.c:13)
==3286==
==3286== LEAK SUMMARY:
==3286== definitely lost: 1,981,530,112 bytes in 1,935,088 blocks
==3286== indirectly lost: 0 bytes in 0 blocks
==3286== possibly lost: 276,480 bytes in 270 blocks
==3286== still reachable: 2,048 bytes in 2 blocks
==3286== suppressed: 0 bytes in 0 blocks
==3286== Reachable blocks (those to which a pointer was found) are not shown.
==3286== To see them, rerun with: --leak-check=full --show-reachable=yes
==3286==
==3286== For counts of detected and suppressed errors, rerun with: -v
==3286== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)