SWAP Memory
Swap memory is a space on a storage device (usually a hard drive or SSD) that is used as virtual memory when the physical RAM (Random Access Memory) is full. When the system runs out of RAM, inactive pages of memory are moved to the swap space to free up RAM for active processes.
This allows the system to continue running even if it exceeds the available physical memory, but it can be slower since accessing data from disk is much slower than accessing data from RAM.
In short, swap memory acts as an overflow area for RAM, helping to prevent crashes or slowdowns when the system runs low on memory. However, heavy reliance on swap can degrade system performance due to slower access speeds compared to RAM.
Configuring swap :
Step 1: Create a Swap File
Determine the size of the swap file you want to create. For example, to create a 2 GB swap file, use the following command:
/swapfile
is the location and name of the swap file (you can choose another path if desired).bs=1M
sets the block size to 1 MB.count=2048
creates a 2 GB file (2048 MB). Change this value to suit the desired swap size (e.g., for 4 GB swap, usecount=4096
).
This command creates a file filled with zeros at the location specified, which will be used as swap space.
Step 2: Set Correct Permissions for the Swap File
Set the proper permissions on the swap file to ensure that only the root user can access it:
This ensures that no one other than the root user can read or write to the swap file.
Step 3: Set Up the Swap Area
Make the swap file usable by setting up the swap area:
This initializes the file so that it can be used as swap space.
Step 4: Activate the Swap File
Enable the swap file so it becomes active immediately:
You can verify that the swap is active with the following command:
This will display the swap file along with the used swap space.
Step 5: Make the Swap File Permanent (Persistent Across Reboots)
To make sure the swap file is used automatically at boot time, you need to add it to the
/etc/fstab
file.Open
/etc/fstab
in a text editor (e.g., nano):Add the following line at the end of the file:
/swapfile
is the path to the swap file you created.none
is the mount point, as swap files are not mounted as regular file systems.swap
specifies the file system type.sw
are the default options.0
means no need for dump backup.0
means no file system check (this is not applicable for swap).
Save and close the file (
CTRL+X
, then pressY
to confirm andEnter
to save).
Step 6: Verify the Swap File Configuration
Check if the swap file is active by running:
This will display your system's memory, including swap usage.
Alternatively, you can verify the swap file using:
This command will list all the active swap spaces on your system.
Adjusting Swappiness:
Swappiness controls how aggressively Linux swaps memory to disk. The default value is 60, but you can lower it to make the system less likely to use swap.
To check the current swappiness value:
To temporarily change swappiness (e.g., to 10):
To make the change permanent, add it to
/etc/sysctl.conf
:Add the following line:
Save and exit (CTRL+X
, then Y
and Enter
).
Last updated