A computer is a multitasking machine that performs different tasks at a time. The CPU of the computer can easily handle all these tasks because it has a multiple-core processor. Each task runs one or more than one processes. The process requires memory to function properly, which it takes from RAM. In contrast to this if a proper memory is not given to the process then it stops working properly. Therefore, this article gives you a proper overview of memory statistics in Linux/Ubuntu.
Get Memory Usage Statistics in Ubuntu 22.04
There are different ways to check memory usage in the Linux operating system.
- Use the “free” Command to check memory usage
- Use the “vmstat” Command to check memory usage
- Use the “cat” Command to check memory usage
- Use the “top” Command to check memory usage
- Use the “htop” Command to check memory usage
- Use the GUI method to check the memory usage
Method 1: Use the “free” Command to Check Memory Usage
The simplest method to check RAM memory usage on Linux is by using the “free” command. The stated command retrieves memory usage details like total memory, free space, used memory, and so on. The command used to be executed is given as:
free
Now use this command with different flags to obtain the results in your desired memory statistics. Execute the below-given command to obtain the results in Megabytes(MB).
free - m
In the above command
- “free” gives the memory output.
- The “-m” flag is used to give values in Megabytes(MB)
The output of the free command shows the “total” memory, “used” memory, “free” space, “shared” and “buffer/ cache” memory.
Now to obtain the results in some more detail by using the free command execute the below given command.
free -m -t
Method 2: Use the “vmstat” Command to Check Memory Usage
Ubuntu supports a “vmstat” command that is used to get a detailed view of the memory usage. To use the vmstat command in Ubuntu 22.04, use the syntax given below:
vmstat
From the above output, it can be seen that the results returned by the vmstat command are not properly structured. To obtain the output result in a manageable shape you can use the “vmstat” command with the “-s” flag.
vmstat -s
In the above command, “-s” forces the “vmstat” command to give the output result in a well-structured format.
The output result shown in the above figure contains the total memory, used memory, swap cache, boot time, and other information.
Use the “cat” Command to Check Memory Usage
Another important way to obtain memory usage in the Linux terminal is by using the “cat” command. This command returns detailed information about the memory usage, such as the “MemTotal” which is the total memory, “MemFree” which is the free space, “Buffers”, “cached”, etc. The command used to be executed for this purpose is given as
cat /proc/meminfo
In the above command
- The “cat” command reads the data from a file in the Linux terminal.
- “proc/meminfo” is a file containing the memory usage information.
Now if you want to obtain more memory statistics results from the above command execute the below given command.
more /proc/meminfo
If you narrow down the memory statistics result then execute the below-given command after executing the “cat /proc/meminfo” command.
less /proc/meminfo
Use the “top” Command to Check Memory Usage
The usage of your CPU and memory usage is found out by implementing the “top” command. This command gives a detailed view of memory such as “MiB Mem” and “MiB Swap” and both these are different prefixes of memory. The full form of MiB Mem is mebibyte memory and for MiB Swap mebibyte swap, is used to swap the data between the RAM and storage device. The executed command to get the memory usage statistics is given as
top
Use the “htop” Command to Check Memory Usage
The next version of the command “top” is “htop” and it gives the best output result for the memory usage in the Linux terminal. The output results are graphical and easily readable by using this command. This command updates the results of CUP and memory in real time. To obtain the memory usage statistics the command to be executed is given as.
htop
Use the GUI Method to Check the Memory Usage
The GUI-based result of memory usage is obtained by entering the “gnome-system-moniter” command in the Linux/Ubuntu terminal. There are three options in the output results such as “processes”, “Resources” and “File systems”. You can learn more by switching between these options.
gnome-system-moniter
The output result obtained by the GUI method contains the following statistics, %CPU, ID, Memory, Disk read total, and other information.
Conclusion
To get the memory statistic in Linux/ Ubuntu there are many different ways such as using the commands in the terminal that are “free”, “vmstat”, “cat”, “top”, “htop”, and “gnome-system-monitor”. Among all these methods the most easy one is by using the “free” command. In this method, you open the terminal and run the command “free -t”.