我在 Ubuntu 18.10 上运行
bash
v4.4.19(1)-release。如果我在一个简单的脚本上运行 valgrind
(甚至是 bash --version
),我会发现我肯定丢失了 12 个字节的内存,并且仍然可以访问大约 46kB 的内存。仍然可访问的内存并没有太困扰我,因为我知道 bash 将通过进程处理和释放来完成它的工作,但我不明白的是为什么我会丢失这 12 个字节。
这是我正在使用的简单测试脚本的示例(文件名是 t.bash):
#!/bin/bash
A=2
((++A))
echo $A
我还尝试在脚本末尾使用
unset A
,这会导致 valgrind
输出的“仍然可达”部分中的 less大约 14 个字节(这是有道理的)。我将 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)
谢谢。