I tried to get the shared memory size of a process on Linux. Here’s the result of using 2 different commands:
-
top and check with the SHR field:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1734 root 20 0 201m 4072 1012 S 0.0 0.1 22:00.65 php-fpm -
pmap -d :
mapped: 206672K writeable/private: 4352K shared: 128K
You can see that the shared memory size in pmap is much smaller than top.
I read some source code to find the reason. It seems that top is reading the value from /proc//statm and the values are calculated by:
unsigned long task_statm(struct mm_struct *mm,
unsigned long *shared, unsigned long *text,
unsigned long *data, unsigned long *resident)
{
*shared = get_mm_counter(mm, MM_FILEPAGES);
*text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
>> PAGE_SHIFT;
*data = mm->total_vm - mm->shared_vm;
*resident = *shared + get_mm_counter(mm, MM_ANONPAGES);
return mm->total_vm;
}
It seems that all the file pages are counted as shared memory?
And the pmap command is reading the info from /proc//maps and then calculate the shared memory through some flags:
3dc822a000-3dc822d000 rw-p 0002a000 08:13 5134288 /usr/lib64/libmcrypt.so.4.4.8
start-end flags file_offset dev_major:dev_minor inode
If the flags[3] == ‘s’ then this map will be counted as shared one.
So my question is which one is more accurate? And why they have different methods to calculate the shared memory size?
Thanks in advance!
The
SHRcolumn intopdoes not report the same thing thatpmapssharedentry does.topis reporting the amount of memory that is shared with other processes because it’s in a dynamic library that is loaded once into memory, and all processes using that library include the same pages in their image, since those pages are read-only.pmapseems to be showing “shared memory” segments, which are data pages that may be read-write or read-only, and are shared between processes with theshmget()and related functions.