System running out of memory: create a swap file

If you have a virtual private server (VPS) and are getting errors like “system running out of memory,” you may want to double check whether you’re using a swap volume or file.

Swap space in Linux is used when the amount of physical memory (RAM) is full. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space. (source)

1. Check swap usage

To check whether you’re using swap space, run the following command:

[[email protected] /]# swapon -s
Filename	Type	Size	Used	Priority

If you see something listed, that means you’re already using a swap file, but if you don’t see anything, like on my server above, you can proceed with the steps below.

2. Create swap file

One thing to do before creating a swap file is to make sure you have enough space on your hard drive. You can see the available space by running the following command:

[[email protected] /]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/vda               20G  1.8G   17G  10% /
none                  246M     0  246M   0% /dev/shm

l have about 17G left, so that’s plenty. Online users generally agree that a good amount of swap space is at least the size of the available RAM, if not 1.5 or 2 times the amount (up to 2GB). You can check how much RAM you have on your server with the following command:

[[email protected] /]# free -m
             total       used       free     shared    buffers     cached
Mem:           490        461         28          0         13        225
-/+ buffers/cache:        222        267
Swap:            0          0          0

l have about 490MB of RAM (advertised as 512MB), so I’ll make my swap file 768MB. You can do this via:

[[email protected] /]# dd if=/dev/zero of=/swap bs=1024 count=768k
786432+0 records in
786432+0 records out
805306368 bytes (805 MB) copied, 2.56213 s, 314 MB/s

Here is a breakdown of the options:

  • if– read from file. We’re not looking to read anything, hence /dev/zero
  • of– write to file. We’re looking to create a file in the root called swap.
  • bs– amount of bytes to read/write at a time. We’ll set this to 1024.
  • count– size of our file. In this case it’s going to be 768k (768MB).

3. Setup swap file

Now we’ll setup the swap file as a Linux swap area. We’ll do this by executing the following command:

[[email protected] /]# mkswap /swap
mkswap: /swap: warning: don't erase bootbits sectors
        on whole disk. Use -f to force.
Setting up swapspace version 1, size = 786428 KiB
no label, UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

4. Enable swap file

To immediately have CentOS use your swap file, run the following command:

[[email protected] /]# swapon /swap

5. Auto-enable swap file

Should you restart your VPS, the swap file would have to be enabled again, which is why we’re going to add an entry into the fstab configuration file to automatically enable the swap file for us on boot.

Open the fstab configuration file:

[[email protected] /]# vi /etc/fstab

And add the following:

# device name    mount point    fs-type    options     dump-freq    pass-num
/swap            swap           swap       defaults    0            0

6. Check swap status

Lastly, if you check your available memory again, you should now see that your swap file was successfully setup:

[[email protected] /]# free -m
             total       used       free     shared    buffers     cached
Mem:           490        483          7          0         12        190
-/+ buffers/cache:        280        209
Swap:          767          0        767

One thought on “System running out of memory: create a swap file

  1. manasa

    2. doubts.

    from which file system swap file is created if multiple file systems present.
    Can we externally specify from which file system to take.

    i have two file systems
    tmpfs and rootfs and by default swap file is created by suing rootfs memory evwn tmpfs have more size.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *