相关文件:
include/linux/kmemleak.h
mm/kmemleak.c
功能开关:
CONFIG_DEBUG_KMEMLEAK
提供一种检测内核下内存泄漏的方法,类似于垃圾回收。
一个内核进程名为kmemleak会每隔一段时间(默认10分钟)扫描内存,并且打印新发现的没有被引用对象的数目。
显示可能的内存泄漏的详细信息:
需要先挂载debugfs文件系统:
mount -t debugfs nodev /sys/kernel/debug
cat /sys/kernel/debug/kmemleak
内存扫描的参数可以在运行时修改,通过写/sys/kernel/debug/kmemleak。
off - disable kmemleak (irreversible)
stack=on - enable the task stacks scanning (default)
stack=off - disable the tasks stacks scanning
scan=on - start the automatic memory scanning thread (default)
scan=off - stop the automatic memory scanning thread
scan=<secs> - set the automatic memory scanning period in seconds
(default 600, 0 to stop the automatic scanning)
scan - trigger a memory scan
clear - clear list of current memory leak suspects, done by
marking all current reported unreferenced objects grey
dump=<addr> - dump information about the object found at <addr>
也可以在启动参数中带上kmemleak=off来关闭该功能。
当检测到有内存泄漏时会发印:
kmemleak 6 new suspected memory leaks (see sys/kernel/debug/kmemleak)
这里的6表示发现了6个对象,然后就可以到kmemleak中查看详细信息了。