Blog

Mastering Unix Swap: A Guide with shell commands

1. Introduction

In this tutorial, we’ll introduce the Unix swap space, its advantages, and a few simple commands to manage it.

2. The Unix Swap Space

Swap or paging space is basically a portion of the hard disk that the operating system can use as an extension of the available RAM. This space can be allocated with a partition or a simple file.

Thanks to a memory paging technique, specifically the virtual memory management technique, our OS is able to load applications that require more memory than the RAM physically present in our computer.

When the RAM is full, portions of active applications’ data are transferred to the swap space, freeing up RAM for other data needed immediately.

The reasons behind the implementation of this technique are historical: the first machine to feature virtual memory was the Atlas super-computer built in Cambridge in the 1960s, when physical memory was very expensive.

The savings, the additional security, and the increased reliability provided a strong incentive to switch all systems to virtual memory.

University of Manchester Atlas, January 1963

The first Atlas, installed at Manchester University and officially commissioned in 1962, was one of the world’s first supercomputers, considered to be the most powerful computer in the world at that time. See the original Wikipedia article for more info.

3. Create a Swap Space

As mentioned earlier, the swap is a space reserved for memory management.

We have two ways to tell the OS where this space is to be allocated. We can create a new file within an existing partition, or we can allocate an entire partition of type swap.

3.1. Create a Swap File

First of all, we need to create an empty file with the desired size.

To do that, we use the dd command, configuring the size options appropriately:

sudo dd if=/dev/zero of=/myswapfile bs=1M count=4096

Output

yaml

4096+0 records in
4096+0 records out
4294967296 bytes (4.3 GB, 4.0 GiB) copied, 20.0082 s, 215 MB/s

Now, let’s set the correct permissions with chmod:

sudo chmod 600 /myswapfile

The next step is to set the file as swap space using the command mkswap:

sudo mkswap /myswapfile

Output

arduino

Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)
no label, UUID=20721910-090f-4c2a-8442-0e6f50c9c1d6

and enable it with swapon:

sudo swapon /myswapfile

Finally, we’ll make our changes permanent by modifying the fstab file, in case we need to restart the machine.

To append the configuration at the end of the file we echo it to the standard output and pipe it into tee, which can parse the input line by line:

echo "/myswapfile swap swap defaults 0 0" | sudo tee -a /etc/fstab

3.2. Create a Swap Partition

The partition case is different from the previous case only because of the format used — instead of a file, we can allocate an entire portion of the disk.

In order to do that, we need to modify the partitions table to make space and create a new partition of type swap.

There are plenty of GUI tools (such as GParted) that can easily change the partitions table securely and visually with just a few mouse clicks.

Once the new partition is saved, let’s define it as swap:

mkswap /dev/sdXX

Output

javascript

mkswap: /dev/sdXX: warning: wiping old swap signature.
Setting up swapspace version 1, size = 4.0 GiB (5266130944‬ bytes)
no label, UUID=a0a2d61c-bc3a-442a-acf1-120ecb041f9d

Where sdXX should be replaced with the new partition name.

Then, we activate it:

sudo swapon /dev/sdXX

Finally, like in the file scenario, we have to add the new partition to our fstab file, so that it loads the next time we restart:

echo "/dev/sdXX swap swap defaults 0 0" | sudo tee -a /etc/fstab

4. Monitor the Swap Space

Now that our swap space is active, we may need to monitor how the OS is using it. To do this, we have various commands available that can give us information about the free swap left, some more graphical than others.

Let’s explore a few simple examples of text-based solutions.

4.1. swapon –show

The swapon command usually activates a swap space, but if we specify the –show option, it will return data about the active swap spaces:

sudo swapon --show

Output

NAME TYPE SIZE USED PRIO
/myswapfile file 4024M 0B -2
/dev/sdXX partition 4G 0B -3

4.2. free -h

The free command displays the amount of free and used memory in the system, including the swap space:

free -h

Output

vbnet

total used free shared buff/cache available
Mem: 4.9Gi 2.2Gi 193Mi 13Mi 2.6Gi 2.7Gi
Swap: 8.0Gi 2.0Mi 8.0Gi

4.3. cat/proc/swaps

The Unix proc filesystem contains information about various kernel data structures, including swap:

cat /proc/swaps

Output

Filename Type Size Used Priority
/myswapfile file 4194300 2272 -2
/dev/sdXX partition 4194300 0 -3

4.4. top

Often used to monitor processes and resources, top has a useful header with all kind of statistics, including those about swap:

top

Output

yaml

top - 17:40:08 up 54 min, 1 user, load average: 0.29, 0.22, 0.12
Tasks: 188 total, 1 running, 187 sleeping, 0 stopped, 0 zombie
%Cpu(s): 17.5 us, 0.7 sy, 0.0 ni, 81.6 id, 0.0 wa, 0.0 hi, 0.2 si, 0.0 st
MiB Mem : 5050.7 total, 840.9 free, 1555.4 used, 2654.3 buff/cache
MiB Swap: 8192.0 total, 8189.8 free, 2.2 used. 3407.1 avail Mem

5. Delete a Swap Space

In order to remove a swap space, we have to follow a few simple steps:

csharp

Deactivate the space to remove with swapoff:

sudo swapoff /myswapfile

mathematica

Remove the swap entry from /etc/fstab
Remove the file or partition

That’s all we need to reset the machine to its original state.

Conclusion

In this tutorial, we explored briefly what the Unix swap space is used for and how to manage it using a few simple shell commands.

Scroll to Top