DEV Community

BC
BC

Posted on • Updated on

Create swap file in Ubuntu

Create a swap file to let OS use when the physical memory is not big enough. Make the swap file as 1~2x big as your physical memory.

How much swap size do you need

Based on your physical memory size, here is a chart you can use:

  • 1G -> 2G
  • 4G -> 6G
  • 6G -> 8G
  • 8G -> 11G
  • 16G -> 20G
  • 32G -> 38G

Create swap file

CreateSwap.sh:

#!/usr/bin/env bash

fallocate -l 12G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile

echo "/swapfile swap swap defaults 0 0" >> /etc/fstab
Enter fullscreen mode Exit fullscreen mode

Then use swapon --show to check current swaps.

If your system already configured other swaps, you can use swapoff to remove it, for example:

swapoff /dev/sdb
Enter fullscreen mode Exit fullscreen mode

Also remember to remove it in the /etc/fstab file such that it won't get used when system rebooted next time.

Reference

Top comments (1)

Collapse
 
peter279k profile image
peter279k

We can also use the parted command to create the swap partition.

Here are examples to do that:

pi@raspberrypi:~ $ free -mh
              total        used        free      shared  buff/cache   available
Mem:          7.7Gi       369Mi       6.8Gi        24Mi       579Mi       7.1Gi
Swap:          99Mi          0B        99Mi
pi@raspberrypi:~ $ sudo parted
(parted) print free
Model: SD SD (sd/mmc)
Disk /dev/mmcblk0: 62.1GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
        16.4kB  4194kB  4178kB           Free Space
 1      4194kB  273MB   268MB   primary  fat32        lba
 2      273MB   31.9GB  31.6GB  primary  ext4
        31.9GB  62.1GB  30.2GB           Free Space

(parted) mkpart primary linux-swap
Start? 31.9GB
End? 44GB
(parted) print  free
Model: SD SD (sd/mmc)
Disk /dev/mmcblk0: 62.1GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system     Flags
        16.4kB  4194kB  4178kB           Free Space
 1      4194kB  273MB   268MB   primary  fat32           lba
 2      273MB   31.9GB  31.6GB  primary  ext4
        31.9GB  31.9GB  524kB            Free Space
 3      31.9GB  44.0GB  12.1GB  primary  linux-swap(v1)  lba
        44.0GB  62.1GB  18.1GB           Free Space

(parted)
Information: You may need to update /etc/fstab.
pi@raspberrypi:~ $ sudo mkswap /dev/mmcblk0p3
Setting up swapspace version 1, size = 11.3 GiB (12084834304 bytes)
no label, UUID=d89e85d9-e8be-470f-9c67-86648cc590f7
pi@raspberrypi:~ $ sudo swapon /dev/mmcblk0p3
pi@raspberrypi:~ $ free -mh
              total        used        free      shared  buff/cache   available
Mem:          7.7Gi       377Mi       6.9Gi        24Mi       427Mi       7.1Gi
Swap:          11Gi          0B        11Gi
pi@raspberrypi:~ $
pi@raspberrypi:~ $ echo "UUID=d89e85d9-e8be-470f-9c67-86648cc590f7 swap swap defaults 0 0" | sudo tee -a /et
c/fstab
UUID=d89e85d9-e8be-470f-9c67-86648cc590f7 swap swap defaults 0 0
pi@raspberrypi:~ $ cat /etc/fstab
proc            /proc           proc    defaults          0       0
PARTUUID=a2dee777-01  /boot           vfat    defaults          0       2
PARTUUID=a2dee777-02  /               ext4    defaults,noatime  0       1
# a swapfile is not a swap partition, no line here
#   use  dphys-swapfile swap[on|off]  for that
UUID=d89e85d9-e8be-470f-9c67-86648cc590f7 swap swap defaults 0 0
Enter fullscreen mode Exit fullscreen mode