I am trying to find the current memory usage of a particular process. Every guide I've found online so far gives instructions on how to get the usage as a percentage of total memory. I want thr process' ram usae as a discrete value (i.e. in megabytes).
There are several metrics available to check memory usage per process in Linux. I will begin with the two that are easiest to obtain both of which are available in most implementations of the ps and top commands. Vss: called VSZ in the ps command and VIRT in top, is the total amount of memory mapped by a process.
By default, the amount of memory is display in kilobytes. watch -n 5 free -m watch command is used to execute a program periodically. According to the image above, there is a total of 2000 MB of RAM and 1196 MB of swap space allotted to Linux system.
Just add a username with the -U option as shown below and press the shift+m keys to order by memory usage: You can also use a ps command to rank an individual user's processes by memory usage. In this example, we do this by selecting a single user's processes with a grep command:
The ps command includes a column that displays memory usage for each process. To get the most useful display for viewing the top memory users, however, you can pass the ps output from this command to the sort command. Here’s an example that provides a very useful display: $ ps aux | sort -rnk 4 | head -5 nemo 400 3.4 9.2 3309580 563336 ?
To only get a single memory-usage number of interest, try ($pid
is a placeholder for the PID of interest; in bash
, use $$
to refer to the current shell process, for instance):
ps -o rss= $pid # resident set in kbytes; e.g., 2461016
ps -o vsz= $pid # virtual size in kbytes; e.g., 1048
As Sammitch points out, a way to get both values in a single space-separated output line is:
ps -o vsz=,rss= $pid
To convert a single value to MB (rounded to an integer, adjust as needed), try something like:
ps -o rss= $pid | awk '{printf "%.0f\n", $1 / 1024}' # e.g., 1
In PowerShell, you can simplify to (note that $PID
there actually refers to the shell's process itself, analogous to $$
in POSIX-compatible shells such as bash
):
(ps -o rss= $pid) / 1mb # e.g., 1.0443000793457031
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With