xorl %eax, %eax

How to Increase the Swap File in UNIX(-like) Operating Systems

with one comment

First of all, I’m not saying on which operating system because this applies to numerous UNIX, UNIX derivates and UNIX-like operating systems. For example, the process of increasing the swap file is the same on all Linux, AIX, HP-UX, FreeBSD, NetBSD, OpenBSD, IRIX, Tru64 and possibly more. So, assuming that we have approximately 2GB of swap…

root:~# free
             total       used       free     shared    buffers     cached
Mem:       1026140     690164     335976          0     226408     246340
-/+ buffers/cache:     217416     808724
Swap:      2064376          0    2064376
root:~#

We first create an empty file with the size of additional swap we need. For example if we need 512MB of additional swap we’ll create a file like this:

root:~# dd if=/dev/zero of=/example_swap bs=1024 count=500000
500000+0 records in
500000+0 records out
512000000 bytes (512 MB) copied, 7.03446 seconds, 72.8 MB/s
root:~#

Which will obviously create file with size of 512MB filled with zeros…

root:~# ls -l /example_swap
-rw-r--r-- 1 root root 512000000 May 24 11:49 /example_swap
root:~#

Next, we are making this a swap area using the provided utility.

root:~# mkswap /example_swap
Setting up swapspace version 1, size = 511995 kB
root:~#

And we attach this to the system’s swap file as shown below…

root:~# swapon /example_swap
root:~# free
             total       used       free     shared    buffers     cached
Mem:       1026140    1005904      20236          0     197632     579308
-/+ buffers/cache:     228964     797176
Swap:      2564368          0    2564368

As you can see the space was increased. It would be wise to also update /etc/fstab to map this area at boot time or when using swapon -a command.

In order to stop using this area, simply remove it with the appropriate system call’s wrapper utility.

root:~# swapoff /example_swap
root:~# free
             total       used       free     shared    buffers     cached
Mem:       1026140    1005780      20360          0     197648     579316
-/+ buffers/cache:     228816     797324
Swap:      2064376          0    2064376
root:~# 

Written by xorl

May 31, 2011 at 13:57

Posted in administration

One Response

Subscribe to comments with RSS.

  1. It is advised to set r/w permission of swap file:
    # chmod 600 /example_swap
    Sensitive data can be read directly from the file without this simple restriction.

    ZL

    June 3, 2011 at 01:20


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s