Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get accurate shared memory size of a process?

I tried to get the shared memory size of a process on Linux. Here's the result of using 2 different commands:

  1. 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
    
  2. 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!

like image 604
010Bytes Avatar asked Jan 17 '13 18:01

010Bytes


People also ask

How can I see shared memory in process?

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.

How can I measure the actual memory usage of an application or 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':

What is the shared memory size?

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.


1 Answers

The SHR column in top does not report the same thing that pmaps 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.

like image 175
twalberg Avatar answered Nov 15 '22 06:11

twalberg