Oleksandr
Oleksandr

Reputation: 25

Check Shared Memory(SHR) consumption for process

I have some process running on Linux and I need to get amount of shared memory consumed by this process. I know I can do it with top command, but since I'm writing a bash script, I need to receive just one number in bytes, not all table with all processes. For example, there is such command to check VIRT memory consumption for process: ps -o vsz= -p <PID>, but I could't find a similar command to get SHR memory.

Upvotes: 0

Views: 1702

Answers (1)

Erwin
Erwin

Reputation: 952

The S or share key is listed as obsolete, in the ps man page, so that doesn't seem to be possible:

https://www.man7.org/linux/man-pages/man1/ps.1.html

Your next best option may be something like this:

awk '{print $3}' < /proc/<PID>/statm

That's the number of resident shared pages (so multiple by the page size).

However, there's a big caveat: https://man7.org/linux/man-pages/man5/proc.5.html

       /proc/[pid]/statm
              Provides information about memory usage, measured in
              pages.  The columns are:

                  size       (1) total program size
                             (same as VmSize in /proc/[pid]/status)
                  resident   (2) resident set size
                             (inaccurate; same as VmRSS in /proc/[pid]/status)
                  shared     (3) number of resident shared pages
                             (i.e., backed by a file)
                             (inaccurate; same as RssFile+RssShmem in
                             /proc/[pid]/status)
                  text       (4) text (code)
                  lib        (5) library (unused since Linux 2.6; always 0)
                  data       (6) data + stack
                  dt         (7) dirty pages (unused since Linux 2.6; always 0)

Upvotes: 1

Related Questions