5 commands to check memory usage on Linux
Memory Usage
On linux, there are commands for almost everything, because the gui might not be always available. When working on servers only shell access is available and everything has to be done from these commands. So today we shall be checking the commands that can be used to check memory usage on a linux system. Memory include RAM and swap.
It is often important to check memory usage and memory used per process on servers so that resources do not fall short and users are able to access the server. For example a website. If you are running a webserver, then the server must have enough memory to serve the visitors to the site. If not, the site would become very slow or even go down when there is a traffic spike, simply because memory would fall short. Its just like what happens on your desktop PC.
1. free command
The free command is the most simple and easy to use command to check memory usage on linux. Here is a quick example
The m option displays all data in MBs. The total os 7976 MB is the total amount of RAM installed on the system, that is 8GB. The used column shows the amount of RAM that has been used by linux, in this case around 6.4 GB. The output is pretty self explanatory. The catch over here is the cached and buffers column. The second line tells that 4.6 GB is free. This is the free memory in first line added with the buffers and cached amount of memory.
Linux has the habit of caching lots of things for faster performance, so that memory can be freed and used if needed.
The last line is the swap memory, which in this case is lying entirely free.
2. /proc/meminfo
The next way to check memory usage is to read the /proc/meminfo file. Know that the /proc file system does not contain real files. They are rather virtual files that contain dynamic information about the kernel and the system.
Check the values of MemTotal, MemFree, Buffers, Cached, SwapTotal, SwapFree.
They indicate same values of memory usage as the free command.
3. vmstat
The vmstat command with the s option, lays out the memory usage statistics much like the proc command. Here is an example
The top few lines indicate total memory, free memory etc and so on.
4. top command
The top command is generally used to check memory and cpu usage per process. However it also reports total memory usage and can be used to monitor the total RAM usage. The header on output has the required information. Here is a sample output
Check the KiB Mem and KiB Swap lines on the header. They indicate total, used and free amounts of the memory. The buffer and cache information is present here too, like the free command.
5. htop
Similar to the top command, the htop command also shows memory usage along with various other details.
The header on top shows cpu usage along with RAM and swap usage with the corresponding figures.
RAM Information
To find out hardware information about the installed RAM, use the demidecode command. It reports lots of information about the installed RAM memory.
Provided information includes the size (2048MB), type (DDR2) , speed(667 Mhz) etc.
Summary
All the above mentioned commands work from the terminal and do not have a gui. When working on a desktop with a gui, it is much easier to use a GUI tool with graphical output. The most common tools are gnome-system-monitor on gnome and
ksysguard on KDE. Both provide resource usage information about cpu, ram, swap and network bandwidth in a graphical and easy to understand visual output.
A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected] .
61 thoughts on “ 5 commands to check memory usage on Linux ”
Thanks for informations,
it is very helpfull to see for Linux Lite Versions to see how many ram is used by using small ram older notebooks as example the ideapad 100 modell 14 141I with 2 GB RAM.
So i can s-good ee what i can install better to take the ZorinOS 15, ZorinOS 15 lite or Lubuntu
Thanks for the valuable information..Linux command-line tools to check system memory usage
Anyone can share Linux command to set Cron job and access MySQL.
you can use mysql event for automation , thank me later
It’s very helpful for experienced and beginners
it is a good reference for memory-related commands. thanks for share it.
Use “free -h” for human readable output
Very helpfull for all
i have enough memory in my server(16 go), please tell me how to free buffers/cache memory
free -m
total used free shared buffers cached
Mem: 16052 15958 93 0 68 8858
-/+ buffers/cache: 7030 9021
Swap: 24575 89 24486
swapoff -a && swapon -a
That’s a great article, thank you for the commands.
I’ll definitely use them in order to monitor my RAM.
Any thoughts about memory monitoring using modern solutions such as Prometheus and Grafana?
An excellently written article, This is a good article which offers some helpful information for me, I will read more. I hope we can learn from each other.
E-Learning – Anytime, Anywhere | Automation Laboratories | Noida | Pune
Thanks for sharing useful commands
Great Tutorial.
My Favorite is free- m command I always use this command to check the free memory before deploying a new application on my server.
Very good article found helpful
htop command is really useful
it helped me to solve my server’s over usage problem.
special thanks to Silver Moon!
Thanks for the commands, it is very helpful
Thanks for sharing. Please, can you provide a command to kill a process using a specific port?
netstat -pl | grep “port” ### will retrun pid associated with this port
kill -9 pid ####kill that pid
check out https://github.com/TeoGia/jotun
Its a simple program that outputs heap usage in json format
Great write up, useful on how to read the memory used on the linux system, I also created an easy script to show a summary of free ram in a percentage and also list when you need to worry, i got the idea from the linux ate my ram webpage.
I always use command
> sar -r 1 10
Its shows memory usage for each seconds for 10 seconds. try it.
hi ..
I wanted to find the heap memory usage in a linux machine for every 10sec .. Do you have any command.
If you know any tool which gives graphical representation of Heap memory usage .. that will be more helpful.
Thanks,
Sateesh
I have a question, how to use ram access for each person?
linux working fne with spring boot for developing java application
Spring Boot is the best Java framework for microservices. We recommend you to become an expert at Spring Boot!
https://javatrainingchennai.tistory.com/22
It’s really helpful.
Is there any way to monitor memory usage in real time.
Something like a script which checks the memory usage and sending the report to a particular email id in a given interval of time.
How to see top processes sorted by actual memory usage?
I have a server with 12G of memory. A fragment of top is shown below:
The free -m shows the following:
If I understand correctly, the system has only 362 MB of available memory. My question is: How can I find out which process is consuming most of the memory?
Just as background info, the system is running 64bit OpenSuse 12 .
12 Answers 12
First, repeat this mantra for a little while: «unused memory is wasted memory». The Linux kernel keeps around huge amounts of file metadata and files that were requested, until something that looks more important pushes that data out. It’s why you can run:
and have the second find instance run at ridiculous speed.
Linux only leaves a little bit of memory ‘free’ to handle spikes in memory usage without too much effort.
Second, you want to find the processes that are eating all your memory; in top use the M command to sort by memory use. Feel free to ignore the VIRT column, that just tells you how much virtual memory has been allocated, not how much memory the process is using. RES reports how much memory is resident, or currently in ram (as opposed to swapped to disk or never actually allocated in the first place, despite being requested).
But, since RES will count e.g. /lib/libc.so.6 memory once for nearly every process, it isn’t exactly an awesome measure of how much memory a process is using. The SHR column reports how much memory is shared with other processes, but there is no guarantee that another process is actually sharing — it could be sharable, just no one else wants to share.
The smem tool is designed to help users better gage just how much memory should really be blamed on each individual process. It does some clever work to figure out what is really unique, what is shared, and proportionally tallies the shared memory to the processes sharing it. smem may help you understand where your memory is going better than top will, but top is an excellent first tool.
How to display `top` results sorted by memory usage in real time?
How can I display the top results in my terminal in real time so that the list is sorted by memory usage?
9 Answers 9
Use the top command in Linux/Unix:
- press shift + m after running the top command
- or you can interactively choose which column to sort on
- press Shift + f to enter the interactive menu
- press the up or down arrow until the %MEM choice is highlighted
- press s to select %MEM choice
- press enter to save your selection
- press q to exit the interactive menu
Or specify the sort order on the command line
References
The command line option -o (o standing for «Override-sort-field») also works on my Xubuntu machine and according to the Mac man page of top it should work on a Macintosh too. If I want to short by memory usage I usually use
which sorts by the column %MEM . But I can use VIRT , RES or SHR too. On a Macintosh I would probably use mem or vsize .
I don’t know why or how but this is pretty much different between Unix systems and even between Linux distributions. For example -o isn’t even available on my Raspberry running Wheezy. It may be worth give it a try though.