Create Additional Swap Space

by on May.05, 2011, under Linux

Use a File for Additional Swap Space

If you don’t have additional disk, you can create a file on your filesystem, and use the file for swap space.

Using dd, create a swap file with the name “swap_file” under /root directory with a size of 4096MB (4GB).

# dd if=/dev/zero of=/root/swap_file bs=1M count=4096
4096+0 records in
4096+0 records out
4294967296 bytes (4.3 GB) copied, 136.328 s, 31.5 MB/s

# ls -lh /root/swap_file
-rw-r–r– 1 root root 4.0G May 5 14:43 swap_file

Change the permission of the swap file so that only root can access it.

# chmod 600 /root/swap_file

Mark this file as a swap file using the mkswap command.

# mkswap /root/swap_file
Setting up swapspace version 1, size = 4194300 KiB no label, UUID=982c0db9-d522-4f08-a072-29168de28c64

Enable the newly created swapfile.

# swapon /root/swap_file

To make this swap file available as a swap area even after the reboot, add the following line to the /etc/fstab file.

# grep swap_file /etc/fstab
/root/swap_file swap swap defaults 0 0

Verify whether the newly created swap area is available for your use.

# swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 1048572 9272 -1
/root/swap_file file 4194300 0 -2

# free
total used free shared buffers cached
Mem: 508772 492700 16072 0 39384 311016
-/+ buffers/cache: 142300 366472
Swap: 5242872 9272 5233600

Note: In the output of swapon -s command, the Type column will say “file” if the swap space is created from a swap file.

If you don’t want to reboot to verify whether the system takes all the swap space mentioned in the /etc/fstab, you can do the following, which will disable and enable all the swap partition mentioned in the /etc/fstab.

# swapoff -a
# swapon -a

To increase swap by extending a Logical Volume:

# cat /proc/swaps
Filename Type Size Used Priority
/dev/mapper/Volume00-lv_swap partition 16777208 208 -1

# swapoff /dev/Volume00/lv_swap
# lvextend -L +900M /dev/Volume00/lv_swap
# mkswap /dev/Volume00/lv_swap
# swapon /dev/Volume00/lv_swap

No comments for this entry yet...

Leave a Reply

You must be logged in to post a comment.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

But Wait, There's More!

A few highly recommended friends...