Retrieving virtual memory size (VSZ) value of a single process
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
Related videos on Youtube
![sundar](https://lh6.googleusercontent.com/-RcDq8qc7Q4I/AAAAAAAAAAI/AAAAAAAAAAA/AAN31DUk9GOJ4Lbu3Wo_pL4z0_LpozAMpA/mo/photo.jpg?sz=256)
sundar
Updated on September 18, 2022Comments
-
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 over 5 yearsThat 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 over 5 years@Stéphane Chazelas . Correct! On other side... actually,
/proc man
is referring tops man
to get this value and many others.. -
sundar over 5 yearsThanks 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 over 5 yearsI'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 over 5 yearsHi @sundar. you are correct. because in
/proc/PID/stat
you get the vsz in bytes while the commandps
above output memory in kibibytekib
so just convert between both and you will find the same numbers, however1 KiB is equal to 1024 byte
. -
Stéphane Chazelas over 5 years@sundar,
$pid
is meant to be a shell variable, as inpid=34346
. Useps -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 over 5 yearsThank you so much . I'm getting the correct value now :)