Notes

Notes

Training mode

© 2025

Dark Mode

Swap file for old laptop

What is swap space?

Swap space is a space on a disk that is used when the amount of physical RAM memory is full. When a Linux system runs out of RAM, inactive pages are moved from the RAM to the swap space

Add swap file is important for me because my laptop is kind of slow, it has only 4GB RAM.

Here is the following steps to change the swapfile size for my laptop

1) turn off the swap processes 2) resize the swap file 3) make the file usable as swap 4) active the swap file 5) check the amounf of swap file

  • Step 1: turn off the swap processes sudo swapoff -a

  • Step 2: resize the swap file (in this case is 8GB swapfile which is double the RAM memory) sudo dd if=/dev/zero of=/myswapfile bs=1024 count=8388608

  • Step 3: make the file usable as swap sudo mkswap /swapfile

  • Step 4: activate the swap file sudo swapon /swapfile

  • Step 5: Check the amount of the swap file ls -l /myswapfile

or check the amount of swap available. grep SwapTotal /proc/meminfo


Turn swap off

This moves stuff in swap to the main memory and might take several minutes

sudo swapoff -a

Create an empty swapfile

Note that “1M” is basically just the unit and count is an integer.

Together, they define the size. In this case 8GiB.

sudo dd if=/dev/zero of=/swapfile bs=1M count=8192

Set the correct permissions

sudo chmod 0600 /swapfile

sudo mkswap /swapfile # Set up a Linux swap area sudo swapon /swapfile # Turn the swap on

Check if it works

grep Swap /proc/meminfo


In the following steps, change /media/fasthdd/swapfile.img to anything you like. For example, it can be /swap.img as well. /media/fasthdd/swapfile.img is just an example filename. If you are using this one, then of course there must be a directory /media/fasthdd/ with enough free space for your new swap file.

Use any terminal application to run the commands of the following steps. All commands should be run with root privileges. To do this, you can either add sudo to the beginning of every command or run sudo bash before running the commands.

Create an empty file:

This file will contain virtual memory contents so make file big enough for your needs. This one will create a 1GiB file, which means +1GiB swap space for your system:

dd if=/dev/zero of=/media/fasthdd/swapfile.img bs=1024 count=1M

If you want to make a 3GiB file, then change count value to count=3M. See man dd for more information.

Bake the swap file:

The following command is going to make a "swap filesystem" inside your fresh swap file.

mkswap /media/fasthdd/swapfile.img

Bring up on boot:

To make sure that your new swap space is activated while booting up computer, you should add it to the filesystem configuration file /etc/fstab. Add it to the end of the file. This is recommended because other filesystems (at least one that contains a swap file) must be mounted in read-write mode before we can access any files.

# Add this line to /etc/fstab
/media/fasthdd/swapfile.img swap swap sw 0 0

Activate:

You can either reboot your computer or activate the new swap file by hand with the following command:

swapon /media/fasthdd/swapfile.img

If everything goes well, you should see that more swap space is available for use. You can use the following commands to check your new swap and confirm that it is active:

$ cat /proc/swaps

Filename Type Size Used Priority

/media/fasthdd/swapfile.img file 8388604 2724 -1

$ grep ‘Swap’ /proc/meminfo

SwapCached: 4772 kB

SwapTotal: 8388604 kB

SwapFree: 8355812 kB