Sometimes we need to check linux memory usage, there are many GUI tools like KSysGuard or GNOME system monitor.
But here we are focusing on the CLI tools, which could be used with shell scripts, over SSH, serial port to check linux memory usage when using a GUI tool is not possible.
Amount of available memory keeps changing quickly between used, free, shared, buffers and cached, though total amount memory in a system is fixed, let's start.
Contents
Check Linux memory usage with the free command
To check available free memory and swaps, use the free
command. Just run the command like bellow, no root privilege required.
free -h
It's worth to pass few more command line arguments to get more convenient results.
- -h , in human readable form
- -b , results in bytes
- -k , results in KB
- -m , results in MB
- -c , display results many times,1 second delay
- -s , continuously print results with a delay.
Example
free -h -c 10 -s 0.5
Show linux memory usage in human readable form for 10 times with 0.5 second interval between results.
It's one of the most used command to check memory usage in Linux or any UNIX like OS, even available in most embedded systems including WiFi routers running OpenWrt or DD-Wrt.
Find linux memory usage from the /proc/meminfo file
Reading memory usage from this file is specially useful for scripting purpose. All results available in this file are formatted into multiples of KB , use cat
, grep
, awk
or your favorite text manipulation command to get the desired result.
grep -i 'MemTotal:' /proc/meminfo
grep -iE 'MemFree:|SwapTotal:' /proc/meminfo
awk -F 'MemFree:' '{print$2}' < /proc/meminfo | tr -s 'n'
These commands above could be handy to get linux memory info when other commands are not available, like on embedded systems or OpenWrt routers.
Check installed memory size with dmidecode command
This dmidecode command is more hardware specific, useful to know about the computer's hardware without opening it.
Use the DMI type 16 and 17 to get the memory related informatio, i.e. to get information about installed RAM modules on the system. This command requires root privilege, examples bellow.
sudo dmidecode -t 17
The result is somewhat long, here a little snippet
Handle 0x0022, DMI type 17, 34 bytes Memory Device Array Handle: 0x001B Error Information Handle: 0x0024 Total Width: 64 bits Data Width: 64 bits Size: 2048 MB Form Factor: SODIMM Set: None Locator: ChannelB-DIMM0 Bank Locator: BANK 2 Type: DDR3 Type Detail: Synchronous Speed: 1333 MHz Manufacturer: Kinston Serial Number: 8316C2B7 Asset Tag: 0123456789 Part Number: ACR256X64D3S1333C9 Rank: Unknown Configured Clock Speed: 1333 MHz
So, you can see the RAM modules form factor, installation slot, speed, type and other bunch of information. Which is generally useful while purchasing additional or replacement RAM modules.
This command is particularly useful for RAM upgrade and hardware related troubleshooting purpose, some more useful dmidecode commands here.
Note: Actual available memory is always less than the amount get from the dmidecode command, as a portion of the RAM is reserved for graphics and other purpose.
Check linux memory usage with the atop command
Atop is a another Linux memory usage command and system monitor. This command could be used to check linux memory usage info including per process usage. Use the apt command to install it on Ubuntu or any other Debian based distro.
sudo apt-get install atop
You can use the -m command line argument to get memory related info. Run this command as as super user for more detailed results.
sudo atop -m
Note: Atop considers RAM usage more than 90% as critical.
Get realtime linux memory info with top and htop commands
Both top and htop are interactive process and system resource viewer, while the htop provides a ncurses based colorful interface.
You may need to install htop, on any De bian based distro install it with apt . On the other hand the top utility is installed by default on most systems.
sudo apt-get install htop
You may want to decrease the update interval of top and htop, for a 0.5 second update interval use the -d
flag along with htop.
top -d 0.5
htop -d 5
Conclusion
It's normal that Linux using some more memory than expected, but it's normal.
So, this extra memory is for caching purpose to make the system more responsive, there is a dedicated site for this, Linux ate my RAM!.
So that's it, how to check linux memory usage with command line tools. If you have any suggestion or question, just drop a comment, I'll be happy to hear from you.
Patric says
Hey thanks for article, these CLI tools are great to check linux memory usage, but you might also want to include the vmstat command in the list.
Arnab Satapathi says
Thanks ! indeed the vmstat command is very useful, I'll include it too in the list.