The swap is the small portion of the secondary memory which is used as a virtual memory. It improves the performance of the system, even if the system memory has low space. The swap is used to store the pending processes so that the main memory can store more processes in it. In Ubuntu 22.04, the swap space is automatically assigned during the installation.
In this article, we will check if the swap is active, or not by using the terminal.
How Can I Check if Swap is Active from the Terminal?
When you are using a system that has low hardware specifications, you can improve the performance of the system by the optimization of the resources. Swap is one of the important resources that works with the main memory to improve the overall performance of the system. If you want to check the swap service in Ubuntu 22.04 follow the methods mentioned below.
Method 1: Using the File proc/meminfo
The “proc/meminfo” file is used to store information about the usage of memory in a system. You can use this file to check the amount of swap space that is being used and free if it is enabled.
Use the following command to open the contents of the file in the terminal:
$ cat /proc/meminfo
The command will display the memory information report:
You can see the SwapTotal and SwapFree fields along with the values. It means that my swap service is active.
Method 2: Using the “swapon -s” Command
The “swapon -s” command is used to check the status of the swap service. If it is active the command displays the swapfile and details of the file.
$ sudo swapon -s
The command will display the following output if the swap service is active:
The output shows the following details:
- Filename: Which displays the filename
- Type: Displays the type either file or partition
- Size: Displays the swap space size
- Used: Displays the used swap space
- Priority: Priority value displays the usage priority of the swap
Method 3: Using the “swapon –show” Command
The “swapon –show” command is used to display the swap usage information more concisely. Use the following command to display the swap usage:
$ swapon -show
The command will display the following output:
The details are as follows:
- NAME: Which displays the filename
- TYPE: Displays the type either file or partition
- SIZE: Displays the swap space size
- USED: Displays the used swap space
- PRIO: Priority value displays the usage priority of the swap
Method 4: Using the “top” Command
The top command is used to monitor the system resources. You can also use this command to check the swap service. Use the following command to check the swap service:
$ top
The following output will be shown:
Note: If the swap service is not active you can enable it by using the following instructions:
- Execute the following command to check whether the swap service is enabled or not
$ swapon -show
If the swapon –show command does not display the output it means that the swap is not active:
- You can enable the service by using the following command:
$ sudo swapon -a
- Run the “swapon –show” command again
$ swapon -show
Now your swap service is active. You will see the following output:
This is how you can check if the swap service is active from the terminal in Ubuntu 22.04.
Conclusion
To check if the swap is active from the terminal in Ubuntu 22.04, you can use the proc/meminfo file, the “swapon -s” command, the “swapon –show” and the “top” command. In this article, we have learned how you can check swap is active from the terminal.