May 2024
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

Categories

May 2024
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

Additional Swap File in Linux

What is swap?
Swap space is the area on a hard disk which is part of the Virtual Memory of your machine, which is a combination of accessible physical memory (RAM) and the swap space. Swap space temporarily holds memory pages that are inactive. Swap space is used when your system decides that it needs physical memory for active processes and there is insufficient unused physical memory available. If the system happens to need more memory resources or space, inactive pages in physical memory are then moved to the swap space therefore freeing up that physical memory for other uses. Note that the access time for swap is slower therefore do not consider it to be a complete replacement for the physical memory. Swap space can be a dedicated swap partition (recommended), a swap file, or a combination of swap partitions and swap files.

swap file is a special file in the filesystem that resides amongst your system and data files.
To Add
Step1: Use dd command and create 4g swap file
krishbe$ dd if=/dev/zero of=/swapfile bs=1024 count=4194304
4194304+0 records in
4194304+0 records out
krishbe$
Step2: Use mkswap command and setup linux swap area
krishbe$ mkswap /swapfile
Setting up swapspace version 1, size = 4294963 kB
krishbe$
krishbe$ free -g
             total       used       free     shared    buffers     cached
Mem:            15          9          5          0          0          7
-/+ buffers/cache:          2         12
Swap:            1          1          0
krishbe$
Step3: Use swapon command and activate newly created swap area
krishbe$ swapon /swapfile
Step4: Use free command and make sure additional swap space activated
krishbe$ free -g
             total       used       free     shared    buffers     cached
Mem:            15          9          5          0          0          7
-/+ buffers/cache:          2         12
Swap:            5          1          4
krishbe$
Step5: To make it permanent append /etc/fstab with the below entry
/swapfile               swap                    swap    defaults        0 0

To Remove
use swapoff command and remove swap file entry from  /etc/fstab
#swapoff /swapfile

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>