5

I need to know the amount of memory shared between two processes, that is, the intersection of their shared memories.

Any ideas?

slm
  • 369,824
idelvall
  • 153

1 Answers1

6

You can look at /proc/<pid>/maps, /proc/<pid>/smaps (or pmap -x <pid> if your OS supports) of interested process ID's and compare outputs to determine shared memory regions. That includes shared memory segments via shmget calls, as well as any shared libraries, files.

Edit: As mr.spuratic pointed out his answer here has more details on kernel side

You can look at a process RSS using ps, however it doesn't take into consideration all the shared pages. To see RSS for specific process, see below

cv@thunder:~$ ps -o rss,pid,comm -p $$,7023
  RSS   PID COMMAND
22060  7023 xfwm4
 6876 18094 bash

smem tool provides more detailed information, taking into consideration of shared pages. See below output for the same above process

cv@thunder:~$ smem -t |egrep "RSS|$$|7023"
  PID User     Command                         Swap      USS      PSS      RSS 
 9852 cv       grep -E RSS|18094|7023             0      340      367     2220 
18094 cv       bash                               0     3472     4043     6876 
 7023 cv       xfwm4 --display :0.0 --sm-c        0     5176     7027    22192 

From man smem:

   smem  reports  physical  memory usage, taking shared memory pages into account.  Unshared memory is reported as the USS (Unique Set Size).  Shared
   memory is divided evenly among the processes sharing that memory.  The unshared memory (USS) plus a  process's  proportion  of  shared  memory  is
   reported  as  the  PSS  (Proportional  Set  Size).   The USS and PSS only include physical memory usage.  They do not include memory that has been
   swapped out to disk.
VenkatC
  • 2,175
  • thanks @VenkatC, this is insightful, but the memory addresses these files refer in their records are virtual (process space), so I can't see how they overlap. Any way of seeing how they map to physical addresses? – idelvall Nov 16 '16 at 17:08
  • @idelvall /proc/kpageflags, see my answer to a related question here which explains some relevant kernel details. – mr.spuratic Nov 16 '16 at 17:21
  • @idelvall you can see shared segments with 's' flag in and read more info at https://www.kernel.org/doc/Documentation/vm/pagemap.txt on converting virtual to physical. – VenkatC Nov 16 '16 at 18:40
  • @idelvall also, what are trying exactly trying to find out or what problem are you trying to resolve? – VenkatC Nov 16 '16 at 18:43
  • @VenkatC i want to know the total amount of RSS of a set of processes – idelvall Nov 16 '16 at 20:18
  • @idelvall you could use simple ps output for RSS column or use tools like smem which gives more detailed information on shared pages etc https://www.selenic.com/smem/ – VenkatC Nov 16 '16 at 20:27
  • @VenkatC I want the RSS of the set of processes that is <= than the sum of the RSS of each process – idelvall Nov 16 '16 at 20:59
  • @VenkatC, mr.spurtic gave me all the info I needed, a more detailed explanation of your comments, anyway if you edit your response and include a link to his response I will mark it as accepted – idelvall Nov 16 '16 at 21:09
  • @idelvall updated answer – VenkatC Nov 16 '16 at 22:21