Retrieving virtual memory size (VSZ) value of a single process

6,096

Check virtual memory size (vsz) values under /proc/<PID>/stats

According to /proc man, the column #23 of /proc/<PID>/stats represents vsz values in bytes.

For example, PID is 3917 then:

cat /proc/3917/stat | cut -d" " -f23

Important to note, that the reported vsz values under /proc/PID/stats are estimated in bytes, while the output of the command ps -o vsz= -p "$pid" is estimated in kibibyte or kib (1 KiB is equal to 1024 byte).

In order to convert the output of vsz under /proc/PID/stat from byte to kib:

cat /proc/3917/stat | cut -d" " -f23 | xargs -n 1 bash -c 'echo $(($1/1024))' args
Share:
6,096

Related videos on Youtube

sundar
Author by

sundar

Updated on September 18, 2022

Comments

  • sundar
    sundar almost 2 years

    I would like to get VSZ value of a specific process running on Linux server

    The following command:

    ps -eo size,pid,user,command --sort -size | awk '{ hr=$1/1024 ; printf("%13.2f Mb ",hr) } { for ( x=4 ; x<=NF ; x++ ) { printf("%s ",$x) } print "" }'   
    

    is fetching all process memory high usage to low.

    How can I fetch only (VSZ) value of a single process.

    Let's say, I wanted to get the VSZ value of "proxyfarm". How can I achieve this using the command:

    ps ux | grep httpd | grep proxyfarm    can fetch proxyfarm complete details.But, i need only VSZ of it.
    
  • Stéphane Chazelas
    Stéphane Chazelas over 5 years
    That assumes the process name doesn't contain space or newline characters. Generally, to get data from stats, you need to look after the last occurrence of ) in their (which may not be on the first line). /proc/pid/status is easier to parse.
  • Admin
    Admin over 5 years
    @Stéphane Chazelas . Correct! On other side... actually, /proc man is referring to ps man to get this value and many others..
  • sundar
    sundar over 5 years
    Thanks for your quick help. unfortunately, I'm not getting neither result nor error when using command : ps -o vsz= -p "$34346". Could you help me.
  • sundar
    sundar over 5 years
    I'm getting result for command : cat /proc/34346/stat | cut -d" " -f23. but its not correct value. Can you suggest if i missed anything .
  • Admin
    Admin over 5 years
    Hi @sundar. you are correct. because in /proc/PID/stat you get the vsz in bytes while the command ps above output memory in kibibyte kib so just convert between both and you will find the same numbers, however 1 KiB is equal to 1024 byte.
  • Stéphane Chazelas
    Stéphane Chazelas over 5 years
    @sundar, $pid is meant to be a shell variable, as in pid=34346. Use ps -o vsz= -p 34346 if you don't need to use a variable. $34346 would be either like ${3}4346 (probably expanded to 4346 as $3 is probably unset) or ${34346} (probably empty) depending on the shell.
  • sundar
    sundar over 5 years
    Thank you so much . I'm getting the correct value now :)