As Ubuntu users, especially administrators, we need to keep a check on how much memory resources our system is using and how much of them are free. We also know that most of the administration related tasks can be better achieved through the Linux command line rather than the graphical user interface. For example, the servers mostly work on the shell and there is no UI available in the first place. Since it most important to keep a check on memory resources on servers, it is best to learn the relevant commands that can help us with server administration. The article explains the use of the following 5 commands in order 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 there are enough memory resources for the very vital processes running on your servers. For example, if you are running a web server, you can be sure that the lack of resources does not slow down the website access or the website does not go down altogether.
We have run the commands and procedures mentioned in this article on a Ubuntu 18.04 LTS system.
In order to view the memory usage, we are using the Ubuntu command line, the Terminal application. You can open the Terminal either through the system Dash or the Ctrl+alt+T shortcut.
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.