SoFunction
Updated on 2025-04-13

How to modify the Swap swap space size in Ubuntu

Preface

When installing Ubuntu system, the default space allocation scheme was selected. The Swap space is only 1G, while the actual physical memory is 32G. The space allocated to Swap is at least 1 times the memory, preferably 2 times the memory value. The system is quite stuttering. After reworking the system, why does the hard-working environment need to be redeployed? It's a headache!

1. Check the memory size of the native Swap swap space

The command is as follows

free -m

The execution results are as follows:

longzhiye@longzhiye-laptop:~$ free -m
              total        used        free      shared  buff/cache   available
Mem:          31995         374       30603           9        1016       31142
Swap:           976          65         911

You can see that the Swap space of this machine is about 976M, that is, 1G space.

Let's expand it 64G below

2. Create a Swap file

sudo -i

After entering your password, switch to root.

mkdir /swap

Create swap folder

cd /swap/

Enter the swap folder

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

Create swapfile with size bs*count = 64M * 1k = 64G

sudo mkswap -f swapfile

Convert the generated file to a Swap file

The execution results are as follows:

longzhiye@longzhiye-laptop:~$ sudo -i
[sudo] longzhiye Password: 
root@longzhiye-laptop:~# cd /
root@longzhiye-laptop:/# mkdir /swap
root@longzhiye-laptop:/# cd swap/
root@longzhiye-laptop:/swap# sudo dd if=/dev/zero of=swapfile bs=64M count=1k
Recorded1024+0 Read in
Recorded1024+0 Written out
68719476736 bytes (69 GB, 64 GiB) copied, 276.311 s, 249 MB/s
root@longzhiye-laptop:/swap# sudo mkswap -f swapfile
Setting up swapspace version 1, size = 64 GiB (68719472640 bytes)
No label, UUID=c7feaf13-7f02-4941-a07f-86a43bdf3ef5

3. Activate the Swap file

sudo swapon swapfile
free -m

Activate the Swap file and re-view the Swap space memory

The execution results are as follows:

root@longzhiye-laptop:/swap# sudo swapon swapfile
swapon: /swap/swapfile:Unsafe permissions 0644,Recommended to use 0600。
root@longzhiye-laptop:/swap# free -m
              total        used        free      shared  buff/cache   available
Mem:          31995         381         374           9       31238       31065
Swap:         66512          64       66448

You can see that the Swap space memory changes from 1G to 64G = our expanded 64G + original 1G.

4. Set as permanent Swap

If this step is not done, the settings before restarting are invalid! ! ! !

sudo gedit /etc/fstab

Change the swap path.

# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda2 during installation
UUID=b726da21-83a0-497b-b3eb-a09f16403d60 /               ext4    errors=remount-ro 0       1
# /boot/efi was on /dev/sda1 during installation
UUID=C0BE-3630  /boot/efi       vfat    umask=0077      0       1
#UUID=c7bb6d12-b247-4119-9c27-797f9995884e none            swap    sw              0       0
/swap/swapfile            swap            swap    sw              0       0

V. Expand

If you need to uninstall this swap file, you can enter the created swap file directory.

Execute the following command:

sudo swapoff swapfile

If you need to keep this swap forever, you can write it to the /etc/fstab file.

/swap/swapfile /swap swap defaults 0 0

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.