Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding memory usage of a process in Linux [closed]

Tags:

linux

bash

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).

like image 292
supereater14 Avatar asked Mar 07 '14 21:03

supereater14


People also ask

How do I check memory usage per process in Linux?

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.

How much memory is available in Linux?

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.

How do I rank processes by memory usage in Linux?

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:

How do I view the top memory users of a process?

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 ?


1 Answers

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
like image 112
mklement0 Avatar answered Nov 15 '22 06:11

mklement0