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:
Possible output variants:
1. No swap¶
If the output is empty or contains only the header:
— swap is not configured in the system.
2. Swap partition in use¶
Example:
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:
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¶
2. Create (or recreate) a swap file of the desired size¶
Example for a 1 GB (1024 MiB) file:
Replace count=1024 with the desired size in megabytes (e.g., 512, 2048, 4096, etc.).
3. Set permissions and initialize swap¶
4. Enable swap¶
5. Verify the result¶
Expected output:
6. Make the changes permanent¶
To have swap automatically mount after reboot, add an entry to /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.