There's a similar question other than this and answering the same here to let people know about how linux proc stat vm info currently is not accurate.
Valgrind can show detailed information but it slows down the target application significantly, and most of the time it changes the behavior of the app.
I assume what everyone wants to know WRT "memory usage" is the following...
In linux, the amount of physical memory a single process might use can be roughly divided into following categories.
- M.a anonymous mapped memory
- .p private
- .d dirty == malloc/mmapped heap and stack allocated and written memory
- .c clean == malloc/mmapped heap and stack memory once allocated, written, then freed, but not reclaimed yet
- .s shared
- .d dirty == there should be none
- .c clean == there should be none
- M.n named mapped memory
- .p private
- .d dirty == file mmapped written memory private
- .c clean == mapped program/library text private mapped
- .s shared
- .d dirty == file mmapped written memory shared
- .c clean == mapped library text shared mapped
I would prefer to get the numbers as follows to get the real numbers in least overhead.
You have to sum these up in order to divide what ps shows as RSS and get more accurate numbers not to confuse.
/proc/(pid)/status tries to show these numbers, but they are failing.
So instead of trying to label [anon], [stack], correctly to each mapping, my wish is
that linux kernel people will mainline the proc entry code to sum and show these M.a.p.d, M.a.p.c, M.n.p.d, .... numbers.
Embedded linux people will get really happy IMHO.
M.a.p.d:
awk '/^[0-9a-f]/{if ($6=="") {anon=1}else{anon=0}} /Private_Dirty/{if(anon) {asum+=$2}else{nasum+=$2}} END{printf "sum=%d\n",asum}' /proc/<pid>/smaps
M.a.p.c:
awk '/^[0-9a-f]/{if ($6=="") {anon=1}else{anon=0}} /Private_Clean/{if(anon) {asum+=$2}else{nasum+=$2}} END{printf "sum=%d\n",asum}' /proc/<pid>/smaps
M.n.p.d:... and so on