目录
Linux监控命令各字段含义详解:
linux监控命令各字段含义详解_weixin_34277853的博客-CSDN博客
voidcn.com/article/p-egpecvnq-yk.html
Linux 了解内存使用:
Linux 了解内存使用_黄规速博客:学如逆水行舟,不进则退-CSDN博客
proc.txt:
Linux Kernel Documentation / filesystems / proc.txt
Linux中程序详细的内存使用查看_panderan的博客-CSDN博客
Malloc internal:
https://sourceware.org/glibc/wiki/MallocInternals
程序的虚拟内存
每个线程维护自己的heap用于malloc。机制的介绍:https://kushal.fedorapeople.org/siddhesh/posts/malloc-per-thread-arenas-in-glibc.html
Heap的大小可以用mallopt改变(M_MMAP_MAX)。
可以用pmap看到
# pmap 1
1: /usr/lib/systemd/systemd --system --deserialize 18
0000561240af8000 1412K r-x-- systemd
0000561240e58000 140K r---- systemd
0000561240e7b000 4K rw--- systemd
00005612418e9000 1856K rw--- [ anon ]
00007f5534000000 164K rw--- [ anon ]
00007f5534029000 65372K ----- [ anon ]
00007f553c000000 164K rw--- [ anon ] used heap for thread *
00007f553c029000 65372K ----- [ anon ] unused heap for thread * (used + unused = 64M)
00007f5540dc7000 4K ----- [ anon ]
00007f5540dc8000 8192K rw--- [ anon ] stack for thread x
00007f55415c8000 4K ----- [ anon ]
00007f55415c9000 8192K rw--- [ anon ] stack for thread y
00007f5541dc9000 4K ----- [ anon ]
00007f5541dca000 8192K rw--- [ anon ]
00007f55425ca000 16K r-x-- libuuid.so.1.3.0
pmap就是将/proc/$pid/smaps内容打印出来。
each thread stack size configuration can be found:
# ulimit -a
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 256778
strace命令可以发现虚拟内存的堆分配:
#strace -f -ff -tt -e trace=all -o ~/systemcall.log <pid>
-f Trace child processes as they are created by currently traced processes as a result of the fork(2), vfork(2) and clone(2) system calls.
-ff If the -o filename option is in effect, each processes trace is written to filename.pid
#cat systemcall.log
....
mmap(0x7efcbc000000, 67108864, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0) = 0x7efcb4000000
.....
Dump memory:
dd if=/proc/12987/mem bs=1 skip=$((0x7f521effe000)) count=$((4*1024)) status=none
dd if=/proc/6744/mem bs=1 skip=$((0x01bb7000)) count=$((0x01c3e000-0x01bb7000)) status=none | strings | less
valgrind
valgrind能够查看内存的分配,但无法区分那个线程分配的内存。而且会改变原有的内存分配机制。因此主要是查看实际使用内存的分配和free的情况,无法查看实际虚拟内存的分配情况。当然也可以查看调用栈。
#valgrind --keep-stacktraces=alloc-and-free --trace-children=yes <command & args>
#valgrind --tool=callgrind --track-origins=yes --separate-threads=yes <command & args>