howtouselinux

4 ways to check swap space in Linux

Table of Contents

In Linux, the swap space is used as a place for the operating system to store data that it can no longer fit into the main memory. If your computer starts to run low on memory, the operating system will use the swap space to store data temporarily.

This can cause your computer to run slower, so it’s important to make sure that you have enough swap space available. In this blog post, we will discuss 4 ways that you can check the size of your swap space in Linux.

The following commands can get you the swap space in Linux.

  • cat /proc/swaps
  • free -m
  • top
  • grep SwapTotal /proc/meminfo

 

Procedure to check swap space in Linux

The procedure to check swap space in Linux is as follows:

  • Open the terminal application.
  • Type cat /proc/swaps
  • Press Enter to run the command.
  • The output will show you a list of all the active swap devices on your system, as well as their size and status.

 

Check swap space from /proc/swaps file in Linux

The easiest way to check swap space in Linux is to use the /proc/swaps file. This file contains information about all of the swap spaces on your system. To view the contents of this file, you can use the cat command: $ cat /proc/swaps

The cat command is used to view the contents of a file. To use it, type ‘cat’ followed by the name of the file you want to view. In this case, we’re going to use it to view the contents of the ‘/proc/swaps’ file.

You will see output similar to this

Filename Type Size Used Priority
/dev/sda15 partition 786432 0 -17
/dev/sdb11 partition 786432 0 -18

From this output, we can see that there are two swap spaces on this system, /dev/sda15 and /dev/sdb11. We can also see the size of each swap space in kilobytes. In this example, both swap spaces are exactly the same size.

check swap space with free command in Linux

The free command is a great tool to check the size of your swap space in Linux. To use this command, simply type free and press enter.

The free command displays information about the total amount of free and used physical memory and swap space on the system, as well as the buffers and caches used by the kernel. It is a powerful tool that can help you troubleshoot performance issues on your system.

$ free

total used free shared buff/cache available
Mem: 16384000 141824 124928 120320 13691776 159636480
Swap: 786432000 0 786432000

In this output, we can see that the size of our swap space is 786432000 kilobytes. We can also see that there is no used space in this swap space. This means that our system is not currently using this swap space.

check swap space with top command in Linux

The top command can also be used to check the size of your swap space in Linux. To use this command, simply type top at the command line: $ top

The ‘top’ command is used to display information about all the active processes running on the system. It can be helpful for troubleshooting performance issues. You can use the ‘top’ command to see which processes are using the most CPU or memory, or to identify deadlock situations.

To use the ‘top’ command, simply type it into a terminal window. The output will show a list of all the active processes, along with information about their CPU and memory usage. You can use the up and down arrow keys to scroll through the list, and press q to exit.

You will see output similar to this:

  • top – 15:37:45 up 12 days, 0 users, load average: 0.00, 0.01, 0.05 Tasks: 245 total, 0 running, 244 sleeping, 0 stopped, 0 zombie %Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB
  • Mem : 16384000 total, 141824 free, 124928 used, 13691776 buff/cache KiB
  • Swap: 786432000 total, 786432000 free, 0 used. 159636480 avail Mem

 

From this output, we can see that the size of our swap space is 786432000 kilobytes. We can also see that there is no used space in this swap space.

check swap space from file /proc/meminfo in Linux

You can check the size and current usage of swap space by running the command: grep SwapTotal /proc/meminfo.

This command will print out the total amount of swap space available on your system, as well as the amount of swap space that is currently being used. The output will look something like this:

grep SwapTotal /proc/meminfo
SwapTotal: 786432000 kB

 

In this example, we can see that our system has 786432000 kilobytes of total swap space available. The /proc/meminfo file contains information about the current memory usage of your system. This file includes information about both physical and virtual memory. To view the contents of this file, you can use the cat command: $ cat /proc/meminfo

FAQ about swap space in Linux

How often should I check my swap space usage?
There is no definitive answer to this question, as it will depend on your system and how it is used. However, it is generally a good idea to check your swap space usage on a regular basis. This will help you to ensure that your system has enough available swap space if it needs to use it.

What should I do if my system is using swap space?
If your system is using swap space, it means that your system is running low on physical memory. You should check to see what processes are using the most memory and try to terminate or kill them if possible. If you can’t free up enough memory, you may need to add more physical memory to your system.

how to add swap space in Linux

To add swap space in Linux, you can use the dd command. This command will create a new swap space on your system. The size of the swap space will be determined by the size of the argument that you pass to the dd command.

The following command will create a 10 gigabyte swap space:
dd if=/dev/zero of=/myswap bs=1024 count=10240000

This command will create a 100 megabyte swap space:
dd if=/dev/zero of=/myswap bs=1024 count=102400

You can also use the mkswap command to create a new swap space. This command will take the size of the swap space as an argument, in megabytes. The following command will create a 100 megabyte swap space: mkswap /myswap 100

After you have created your new swap space, you need to activate it. This can be done with the swapon command. To activate the swap space that we created in the previous example, we would use the following command: swapon /myswap

In this article, we have shown you four ways to check the size of your swap space in Linux. We have also answered some frequently asked questions about swap space in Linux. If you have any further questions, please feel free to contact us.

David Cao
David Cao

Hey there! I am David, a Cloud & DevOps Enthusiast and 18 years of experience as a Linux engineer. I work with AWS, Git & GitHub, Linux, Python, Ansible, and Bash. I am a technical blogger and a Software Engineer, enjoy sharing my learning and contributing to open-source.