我正在Ubuntu 18.10上运行bash
v4.4.19(1)-发行版。如果我在一个简单的脚本(甚至是valgrind
)上运行bash --version
,我肯定会丢失12个字节的内存,仍然可以达到46kB的数量级。仍然可以访问的内存并没有让我感到那么麻烦,因为我知道bash会通过进程处理和释放来解决问题,但是我不明白的是为什么我会丢失这12个字节。
这是我正在使用的简单测试脚本的示例(文件名是t.bash):
#!/bin/bash
A=2
((++A))
echo $A
我还尝试在脚本结尾处使用unset A
,这会在valgrind
输出的“仍然可访问”部分中产生大约14个字节 less (这很有意义)。我称valgrind为valgrind ./t.bash
(甚至叫valgrind bash --version
也是为了看看它是否也泄漏了)。
这正常吗?在更复杂的脚本中,我发现了相同的结果,每次bash生成都“绝对丢失”了12个字节。
上述脚本的valgrind输出示例:
ctimm@gordon:~$ valgrind ./t.bash
==37810== Memcheck, a memory error detector
==37810== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==37810== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==37810== Command: ./t.bash
==37810==
3
==37810==
==37810== HEAP SUMMARY:
==37810== in use at exit: 45,729 bytes in 641 blocks
==37810== total heap usage: 766 allocs, 125 frees, 55,160 bytes allocated
==37810==
==37810== LEAK SUMMARY:
==37810== definitely lost: 12 bytes in 1 blocks
==37810== indirectly lost: 0 bytes in 0 blocks
==37810== possibly lost: 0 bytes in 0 blocks
==37810== still reachable: 45,717 bytes in 640 blocks
==37810== suppressed: 0 bytes in 0 blocks
==37810== Rerun with --leak-check=full to see details of leaked memory
==37810==
==37810== For counts of detected and suppressed errors, rerun with: -v
==37810== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
对于在bash --version
上运行valgrind并进行全面泄漏检查,显示所有泄漏类型:
ctimm@gordon:~$ valgrind --leak-check=full --show-leak-kinds=all bash --version
==39097== Memcheck, a memory error detector
==39097== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==39097== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==39097== Command: bash --version
==39097==
GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
==39097==
==39097== HEAP SUMMARY:
==39097== in use at exit: 17 bytes in 2 blocks
==39097== total heap usage: 153 allocs, 151 frees, 21,051 bytes allocated
==39097==
==39097== 5 bytes in 1 blocks are still reachable in loss record 1 of 2
==39097== at 0x483774F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==39097== by 0x19007D: xmalloc (in /bin/bash)
==39097== by 0x1351FA: main (in /bin/bash)
==39097==
==39097== 12 bytes in 1 blocks are definitely lost in loss record 2 of 2
==39097== at 0x483774F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==39097== by 0x19007D: xmalloc (in /bin/bash)
==39097== by 0x189A7A: set_default_locale (in /bin/bash)
==39097== by 0x134EA6: main (in /bin/bash)
==39097==
==39097== LEAK SUMMARY:
==39097== definitely lost: 12 bytes in 1 blocks
==39097== indirectly lost: 0 bytes in 0 blocks
==39097== possibly lost: 0 bytes in 0 blocks
==39097== still reachable: 5 bytes in 1 blocks
==39097== suppressed: 0 bytes in 0 blocks
==39097==
==39097== For counts of detected and suppressed errors, rerun with: -v
==39097== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
谢谢。