Skip to Content

4 ways to check swap space in Linux

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 server 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

 

Get Your Free Linux training!

Join our free Linux training and discover the power of open-source technology. Enhance your skills and boost your career! Learn Linux for Free!

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.

Here is more info about the /proc directory in Linux.

The /proc directory in Linux is a virtual file system that provides an interface to kernel data structures and system information. It contains files and directories that expose information about various system resources, including processes, memory usage, CPU usage, network connections, and hardware devices.

Some of the commonly used files in /proc directory are:

  • /proc/cpuinfo: Provides information about the CPU installed on the system, such as its model, clock speed, and cache size.
  • /proc/meminfo: Provides information about the system’s memory usage, including the total amount of memory available, how much is in use, and how much is free.
  • /proc/filesystems: Lists all the file systems currently supported by the kernel.

 

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.

We can use free -h command to display the swap usage statistics in a human-readable format.

Let’s see another example.

df -h
            total used free shared buff/cache available
Mem: 7.7G 2.2G 1.5G 551M 4.0G 4.6G
Swap: 2.0G 50M 1.9G

Look at the “Swap” row in the output. The columns under this row show the following information:

  • total: the total size of the swap space
  • used: the amount of swap space currently being used
  • free: the amount of unused swap space

 

This indicates that the system has a total of 2.0GB of swap space, with 50MB currently being used and 1.9GB free.

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

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. 

  • Once top is running
  • Look for the Swap: line in the header section of the display. This line displays the total amount of swap space, as well as the amount of used and free swap space.

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.

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

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.