- How to check memory usage per process in Linux
- Using top and ps to check memory usage per process (VSS and RSS)
- Using smem to check memory usage per process
- Related Posts
- 5 thoughts on “How to check memory usage per process in Linux”
- Showing memory usage in Linux by process and user
- There are several commands for checking up on memory usage in a Linux system, and here are some of the better ones.
- Using top
- Using ps
- Using top
- Using ps
- Using ps along with other commands
- How to Check Memory Usage Per Process on Linux
- Checking Memory Usage Using ps Command:
- Checking Memory Usage of Processes with pmap:
- Using pmap to List Memory Usage of All the Processes in Kilobytes:
How to check memory usage per process in Linux
Table of Contents
Linux check memory usage per process. how to check which process is using more memory in Linux. Linux how much memory is a process using. Linux track process memory usage over time. linux check memory usage per process in mb. Linux check memory. linux show memory usage. linux see memory usage. linux memory usage info. linux process memory usage. how to check memory usage 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
- the virtual set size (vss)
- the resident memory size (rss)
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. It is the sum of all the regions shown in /proc/
/map . This number is of limited interest, since only part of the virtual memory is committed to physical memory at any one time.
Using top and ps to check memory usage per process (VSS and RSS)
The ps command shows Vss (VSZ) and Rss (RSS) with the options, -Aly , and a custom format which includes vsz and rss , as shown here
Likewise free and top also shows a summary of the free memory and memory usage per process:
Using smem to check memory usage per process
In 2009, Matt Mackall began looking at the problem of accounting for shared pages in process memory measurement and added two new metrics called the unique set size or Uss , and the proportional set size or Pss
- Uss: This is the amount of memory that is committed to physical memory and is unique to a process; it is not shared with any other. It is the amount of memory that would be freed if the process were to terminate.
- Pss: This splits the accounting of shared pages that are committed to physical memory between all the processes that have them mapped. For example, if an area of library code is 12 pages long and is shared by six processes, each will accumulate two pages in Pss . Thus, if you add the Pss numbers for all processes, you will get the actual amount of memory being used by those processes. In other words, Pss is the number we have been looking for.
The information is available in /proc/
/smaps , which contains additional information for each of the mappings shown in /proc/
/maps . Here is one section from such a file which provides information about the mapping for the libc code segment:
There is a tool named smem that collates the information from the smaps files and presents it in various ways, including as pie or bar charts. The project page for smem is https://www.selenic.com/smem.
There are various filters which you can apply with smem as shown below with the latest available release (1.4) at the time of writing this article
To check memory usage per process in total we can execute below command
This will give you memory usage detail of every process active on your system.
To get the memory usage of a single process we can grep the process from the list
Here as you see we get similar results as above with /proc/31768/smaps and with smem tool. So the actual memory usage of amsHelper is 56.4 MB
Lastly I hope the steps from the article to check memory usage per process on Linux was helpful. So, let me know your suggestions and feedback using the comment section.
Related Posts
Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud
If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.
For any other feedbacks or questions you can either use the comments section or contact me form.
Thank You for your support!!
5 thoughts on “How to check memory usage per process in Linux”
There’s definately a lot to know about this
issue. I really like all of the points you’ve made.
I am happy that I noticed this site, precisely the right information that I was looking for! .
Thank you – excellent writeup!
But I would add this, from the smem man page: The USS and PSS only include physical memory usage. They do not include memory that has been swapped out to disk”
I’d also note that it would be faster and easier to choose the process via the -P option:
smem -k -P amsHelper
Thank you for sharing this additional information.
Cool! Thanks for the full ps flags to list the many kinds of memory options, as well as the tip about looking at the /proc/PID/smaps file for more information. I was trying to figure out how much memory one of my processes was consuming, but, alas, my Linux distro (running on an embedded system) has no way to install either smem or the other popular pmap tool. I’ve been just using good, old top for that, but your article gave me a vast array of many more suggestions! Thanks so much for that 🙂
Источник
Showing memory usage in Linux by process and user
There are several commands for checking up on memory usage in a Linux system, and here are some of the better ones.
There are a lot of tools for looking at memory usage on Linux systems. Some are commonly used commands like free and ps while others are tools like top that allow you to display system performance stats in various ways. In this post, we’ll look at some commands that can be most helpful in identifying the users and processes that are using the most memory.
Here are some that address memory usage by process.
Using top
One of the best commands for looking at memory usage is top. One extremely easy way to see what processes are using the most memory is to start top and then press shift+m to switch the order of the processes shown to rank them by the percentage of memory each is using. Once you’ve entered shift+m, your top output should reorder the task entries to look something like this:
Notice the %MEM ranking. The list will be limited by your window size, but the most significant processes with respect to memory usage will show up at the top of the process list.
Using ps
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:
In the example above (truncated for this post), sort is being used with the -r (reverse), the -n (numeric) and the -k (key) options which are telling the command to sort the output in reverse numeric order based on the fourth column (memory usage) in the output from ps. If we first display the heading for the ps output, this is a little easier to see.
If you like this command, you can set it up as an alias with a command like the one below. Don’t forget to add it to your
/.bashrc file if you want to make it permanent.
Here are some commands that reveal memory usage by user.
Using top
Examining memory usage by user is somewhat more complicated because you have to find a way to group all of a user’s processes into a single memory-usage total.
If you want to home in on a single user, top can be used much in the same way that it was used above. Just add a username with the -U option as shown below and press the shift+m keys to order by memory usage:
Using ps
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:
Using ps along with other commands
What gets complicated is when you want to compare users’ memory usages with each other. In that case, creating a by-user total and ranking them is a good technique, but it requires a little more work and uses a number of commands. In the script below, we get a list of users with the ps aux | grep -v COMMAND | awk ‘
Output from this script might look like this:
There are a lot of ways to report on memory usage on Linux. Focusing on which processes and users are consuming the most memory can benefit from a few carefully crafted tools and commands.
Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as «USL» (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she’s chasing the bears away from her bird feeders.
Источник
How to Check Memory Usage Per Process on Linux
Each of these programs runs as one or more processes. Every process allocates some amount of RAM or memory for itself. It is essential for the process to function correctly. If a process fails to allocate enough RAM or memory, then the process can’t be created and the program won’t be able to start.
So, one of the basic task you do on your computer is to check how much memory or RAM (Random Access Memory) each of the process is using. Because, RAM or memory of your computer is limited.
Imagine a case, where you want to run some program and it fails because you don’t have enough memory. May be some of the processes are using a lot of memory that you don’t need right now. You can kill or stop these processes to free up RAM or memory, so that you can start your important programs.
In this article, I will show you how to check memory usage of each of the processes running on your Linux machine. I will be using Debian 9 Stretch for all the demonstration in this article. But it should work on any modern Linux distributions. Let’s get started.
Checking Memory Usage Using ps Command:
You can use the ps command to check memory usage of all the processes on Linux. There is one problem with this procedure. ps don’t really show you how much memory a process uses in KB or MB format, but it will show you how much memory is being used in percentage.
You can check memory usage (in percentage) of all the process running on your Linux operating system with the following command:
As you can see, all the processes with memory usage in percentage is listed in descending order (The processes using most of the memory is listed first).
Checking Memory Usage of Processes with pmap:
You can check memory of a process or a set of processes in human readable format (in KB or kilobytes) with pmap command. All you need is the PID of the processes you want to check memory usage of.
Let’s say, you want to check how much memory the process with PID 917 is using. To do that, run pmap as follows:
As you can see, the total memory used by the process 917 is 516104 KB or kilobytes. You can also see how much memory the libraries and other files required to run the process with PID 917 is using as well here.
If you don’t care about how much memory the libraries or other dependent files are using, then run pmap as follows:
As you can see, only the total memory used by the process with PID 917 is printed on the screen.
If you want, you can further filter this with awk and get only the size in KB or kilobytes. To do that, run pmap as follows:
As you can see, only the memory usage in KB or kilobytes is printed.
Now you can also list how much memory is used by multiple processes using their PIDs with pmap as follows:
NOTE: Here 917 and 531 are process IDs or PIDs. You can put as many PIDs as you want this way.
Using pmap to List Memory Usage of All the Processes in Kilobytes:
In this section, I will show you how to write your own shell script to list memory usage of all the processes running on your Linux operating system in human readable format (kilobytes or KB).
First make a new file sysmon in your current working directory with the following command:
Now make the file executable with the following command:
sysmon is the shell script that will display all the running processes PID, OWNER, MEMORY (in KB in descending order) and COMMAND. Let’s start.
Open the sysmon script with your favorite text editor, I am going to use Kate.
Now, the first command I am going to run will give me the PID, OWNER and COMMAND of all the running processes separated by colon (:) symbol and store it in the RAWIN variable. Then loop through the output and print it on the screen.
As you can see, I am getting the correct output.
Now it’s time to process each line, store the colon delimited information in separate variables. That’s what I did on line 7, 8 and 9.
As you can see, I can print PID, OWNER and COMMAND in my own format now.
Now it’s time to fetch memory usage of each PID. Line 10 does just that.
As you can see, everything is working perfectly. Now I can print memory usage of each process in kilobytes (KB) as well.
Now all that is left to do is format the output to look nice. I prefer table format. Line 5 prints the header of each column of the table.
Finally, I printed PID, OWNER, MEMORY (in KB) and COMMAND of each processes in a tabular format using line 14.
As you can see, it’s working kinda well. There is a little bit of problem though, the processes are not correctly sorted in descending order by memory usage.
To fix that, I removed sort -bnr -k3 from line 3 and wrapped everything in a shell function sysmon_main(). Then left the job of sorting to the sort command.
The final shell script looks something like this:
As you can see, it works great.
Now you can move it to somewhere like /usr/bin and execute it just like other commands as follows:
Источник