howtouselinux

4 ways to check swap space size 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 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.

Check swap space size from /proc/swaps file in Linux

The easiest way to check swap space size 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

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 size with free command in Linux

The free command can be used to check the size of your swap space in Linux. To use this command, simply type free followed by the name of the swap space that you want to check:

$ 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 size 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

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. This means that our system is not currently using this swap space.

check swap space size 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.

What should I do if my system is not using swap space?
If your system is not using swap space, it means that your system has enough physical memory available. However, you may want to increase the size of your swap space in order to give your system more virtual memory.

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.

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn

If you meet any issues with Linux, record them with Loom and send the link to us here. We will reply ASAP.

Load WordPress Sites in as fast as 37ms!