Skip to content

Managing swap: creation and resizing

Swap is a virtual memory mechanism that temporarily offloads inactive portions of RAM to secondary storage (disk), freeing up RAM for active processes.

Checking current swap status

Before making any changes, verify which swap is in use on the system:

swapon -s

Possible output variants:

1. No swap

If the output is empty or contains only the header:

Filename    Type    Size    Used    Priority

— swap is not configured in the system.

2. Swap partition in use

Example:

Filename        Type      Size     Used   Priority
/dev/vda5       partition 1046524  4568   -1

Here swap is implemented via a separate disk partition /dev/vda5. To increase the total swap volume, you can add a swap file—it will operate alongside this partition.

3. Swap file in use

Example:

Filename    Type    Size     Used   Priority
/swap       file    1048572  0      -1

Swap is organized through the file /swap. You can safely replace it with a larger file; the /etc/fstab entry is likely already present (step 6 of the instructions can be skipped).


Creating or resizing a swap file

Attention

Attention: All operations require superuser privileges (sudo).

1. Disable the current swap

sudo swapoff -a

2. Create (or recreate) a swap file of the desired size

Example for a 1 GB (1024 MiB) file:

sudo dd if=/dev/zero of=/swap bs=1M count=1024

Replace count=1024 with the desired size in megabytes (e.g., 512, 2048, 4096, etc.).

3. Set permissions and initialize swap

sudo chmod 600 /swap
sudo mkswap /swap

4. Enable swap

sudo swapon /swap

5. Verify the result

swapon -s

Expected output:

Filename    Type    Size     Used   Priority
/swap       file    1048572  0      -1

6. Make the changes permanent

To have swap automatically mount after reboot, add an entry to /etc/fstab:

echo "/swap none swap sw 0 0" | sudo tee -a /etc/fstab

Note

If a swap file already existed before (e.g., /swap), ensure that /etc/fstab contains no duplicate entries. If necessary, delete the old entry manually.

question_mark
Is there anything I can help you with?
question_mark
AI Assistant ×