为什么程序要为简单的命令分配这么多内存?

为什么程序要为简单的命令分配这么多内存?

Valgrind 报告说,whois即使我只是询问它的版本,它也会分配大量内存。

$ valgrind whois --version
==18086== Memcheck, a memory error detector
==18086== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==18086== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==18086== Command: whois --version
==18086== 
Version 5.2.11.

Report bugs to <[email protected]>.
==18086== 
==18086== HEAP SUMMARY:
==18086==     in use at exit: 65 bytes in 1 blocks
==18086==   total heap usage: 56 allocs, 55 frees, 15,171 bytes allocated
==18086== 
==18086== LEAK SUMMARY:
==18086==    definitely lost: 0 bytes in 0 blocks
==18086==    indirectly lost: 0 bytes in 0 blocks
==18086==      possibly lost: 0 bytes in 0 blocks
==18086==    still reachable: 65 bytes in 1 blocks
==18086==         suppressed: 0 bytes in 0 blocks
==18086== Rerun with --leak-check=full to see details of leaked memory
==18086== 
==18086== For counts of detected and suppressed errors, rerun with: -v
==18086== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

尝试了其他几个实用程序后,我发现其中许多实用程序都有这个共同点:它们为简单命令分配大量内存。看看 gcc 就知道了!

$ valgrind gcc --version
==18141== Memcheck, a memory error detector
==18141== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==18141== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==18141== Command: gcc --version
==18141== 
gcc (Ubuntu 5.3.1-14ubuntu2.1) 5.3.1 20160413
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

==18141== 
==18141== HEAP SUMMARY:
==18141==     in use at exit: 116,660 bytes in 62 blocks
==18141==   total heap usage: 171 allocs, 109 frees, 135,692 bytes allocated
==18141== 
==18141== LEAK SUMMARY:
==18141==    definitely lost: 4,552 bytes in 13 blocks
==18141==    indirectly lost: 0 bytes in 0 blocks
==18141==      possibly lost: 0 bytes in 0 blocks
==18141==    still reachable: 112,108 bytes in 49 blocks
==18141==         suppressed: 0 bytes in 0 blocks
==18141== Rerun with --leak-check=full to see details of leaked memory
==18141== 
==18141== For counts of detected and suppressed errors, rerun with: -v
==18141== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

相关内容