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 ipcs -mS command can be used to view all shared memory segments that have been allocated on the entire system, and the svmon -P <PID> command can be used to view shared memory segments that are currently being used by a specific process.
If you really want to know what amount of memory your application actually uses, you need to run it within a profiler. For example, Valgrind can give you insights about the amount of memory used, and, more importantly, about possible memory leaks in your program. The heap profiler tool of Valgrind is called 'massif':
Shared memory uses a set of parameters similar to the ones for message queues. The Squid DISKD implementation uses one shared memory area for each cache_dir. Each shared memory area is about 800 kilobytes in size.
The SHR
column in top
does not report the same thing that pmap
s shared
entry does. top
is 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. pmap
seems to be showing "shared memory" segments, which are data pages that may be read-write or read-only, and are shared between processes with the shmget()
and related functions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With