As Ubuntu users, especially as administrators, we need to check how much RAM resources our system uses and how much of it is free. We also know that most administration-related tasks can be done better through the Linux command line than through the graphical user interface. For example, servers usually work on the shell, and there is no GUI available at all. Since it is most important to control the memory resources on the servers, it is best to learn the appropriate commands that can help us with server administration.
This article explains how to use the following 5 commands to check the available memory:
- The free command
- The vmstat command
- The /proc/meminfo command
- The top command
- The htop command
By using these commands, you can always be sure that enough memory resources are available for the very important processes running on your servers. For example, if you are running a web server, you can be sure that the lack of resources will not slow down access to the website or even crash the website.
We ran the commands and procedures mentioned in this article on an Ubuntu 20.04 LTS system.
To display the memory usage, we use the Ubuntu command line, the Terminal application. You can open the terminal either by using the system dash or the key combination Ctrl+alt+T.
Method 1: The free command
Since the free command is the most widely used and without any doubt the most helpful, we will mention its usage first. This command is used to check information about the RAM usage by your system. Here is the command you will enter in the Terminal:
$ free -m
The m flag means that the information will be displayed in MBs.
The available column indicates the available memory. The used column in the swap entry is also 0 which means that it is all unused and thus free.
Method 2: The vmstat command
In order to view memory statistics through the vmstat command, you can use it in the following manner:
$ vmstat -s
The s flag provides detailed statistics about memory usage.
You can view the free memory and the free swap memory entry in the output, indicating the available memory in your system.
Method 3: The /proc/meminfo command
The following command extracts memory-related information from the /proc file system. These files contain dynamic information about the system and the kernel rather than the real system files.
This is the command you will use to print memory information:
$ cat /proc/meminfo
The output of this command is similar to the vmstat command. You can easily view the free memory in the MemFree result and the free swap memory in the SwapFree result.
Method 4: The top command
The top command is used to print CPU and memory usage of your system. You can simply use this command as follows:
In the header of the output, you can see the KiB Mem and Kib Swap entries through which you can check the used and free memory resources.
Method 5: The htop command
Just like the top command, the htop command also gives a detailed analysis of your CPU and memory usage. If you do not have installed htop on your system, you can install it by first updating your abt repositories through the following command:
$ sudo apt-get update
And then installing htop by entering the following command as sudo:
$ sudo apt install htop
Once htop is installed, you can simply use the following command to print the required information:
The Mem (Memory aka RAM) and Swp (Swap) entries in the header indicate the used and total memory through which you can calculate the free memory available on your system.
With the commands that we have mentioned in this article, you can monitor that your system processes are never out of memory. You can avoid the GUI altogether and still keep a check on memory usage on your personal computers and servers.