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)
Check swap usage
To check whether you’re using swap space, run the following command:
[root@vps /]# 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.
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:
[root@vps /]# 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:
[root@vps /]# 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:
[root@vps /]# 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).
Setup swap file
Now we’ll setup the swap file as a Linux swap area. We’ll do this by executing the following command:
[root@vps /]# 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
Enable swap file
To immediately have CentOS use your swap file, run the following command:
[root@vps /]# swapon /swap
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:
[root@vps /]# vi /etc/fstab
And add the following:
# device name mount point fs-type options dump-freq pass-num
/swap swap swap defaults 0 0
Check swap status
Lastly, if you check your available memory again, you should now see that your swap file was successfully setup:
[root@vps /]# free -m
total used free shared buffers cached
Mem: 490 483 7 0 12 190
-/+ buffers/cache: 280 209
Swap: 767 0 767
Featured image by Jeremy Bezanger.
Comments (1)
Previously posted in WordPress and transferred to Ghost.
manasa
January 10, 2017 at 3:16 am
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.