How to know shared memory between two processes?

6,169

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.
Share:
6,169

Related videos on Youtube

idelvall
Author by

idelvall

Updated on September 18, 2022

Comments

  • idelvall
    idelvall over 1 year

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

    Any ideas?

    • Nawaz Sohail
      Nawaz Sohail over 7 years
      doesn't top commands help?
    • countermode
      countermode over 7 years
      ipcs -m may be the answer.
    • phemmer
      phemmer over 7 years
      Are you interested in just shared memory segments (shmget()), or also shared memory due to process fork & copy-on-write, mmap()ped files, etc?
  • idelvall
    idelvall over 7 years
    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?
  • mr.spuratic
    mr.spuratic over 7 years
    @idelvall /proc/kpageflags, see my answer to a related question here which explains some relevant kernel details.
  • VenkatC
    VenkatC over 7 years
    @idelvall you can see shared segments with 's' flag in and read more info at kernel.org/doc/Documentation/vm/pagemap.txt on converting virtual to physical.
  • VenkatC
    VenkatC over 7 years
    @idelvall also, what are trying exactly trying to find out or what problem are you trying to resolve?
  • idelvall
    idelvall over 7 years
    @VenkatC i want to know the total amount of RSS of a set of processes
  • idelvall
    idelvall over 7 years
    @VenkatC I want the RSS of the set of processes that is <= than the sum of the RSS of each process
  • idelvall
    idelvall over 7 years
    @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
  • VenkatC
    VenkatC over 7 years
    @idelvall updated answer