title: How to get accurate shared memory size of a process?
body: <p>I tried to get the shared memory size of a process on Linux. Here's the result of using 2 different commands:</p>
  
  <ol>
  <li><p>top and check with the SHR field:</p>
  
  <pre><code> 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
  </code></pre></li>
  <li><p>pmap -d :</p>
  
  <pre><code>mapped: 206672K    writeable/private: 4352K    shared: 128K
  </code></pre></li>
  </ol>
  
  <p>You can see that the shared memory size in pmap is much smaller than top.</p>
  
  <p>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:</p>
  
  <pre><code>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-&gt;end_code) - (mm-&gt;start_code &amp; PAGE_MASK))
                                                         &gt;&gt; PAGE_SHIFT;
      *data = mm-&gt;total_vm - mm-&gt;shared_vm;
      *resident = *shared + get_mm_counter(mm, MM_ANONPAGES);
      return mm-&gt;total_vm;
  }
  </code></pre>
  
  <p>It seems that all the file pages are counted as shared memory?</p>
  
  <p>And the pmap command is reading the info from /proc//maps and then calculate the shared memory through some flags:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>If the flags[3] == 's' then this map will be counted as shared one.</p>
  
  <p>So my question is which one is more accurate? And why they have different methods to calculate the shared memory size?</p>
  
  <p>Thanks in advance!</p>