Swap memory setup - Ubuntu 12.04 LTS
Problem statement
I hosted a website in a VPS. I installed apache2, mysql, mail system (postfix + dovecot + spamassisn) etc.
So far it was working fine. Next I enabled SSL for that site. Hopefully after that memory demand in system increased.
And very often I found MySQL aborting. On checking error log (in /var/log/mysql/error.log), I found while allocating memory for InnoDB buffer pool, memory allocation fails.
InnoDB: Initializing buffer pool, size = 128.0M
InnoDB: mmap(137363456 bytes) failed; errno 12
InnoDB: Completed initialization of buffer pool
InnoDB: Fatal error: cannot allocate memory for the buffer pool
[ERROR] Plugin 'InnoDB' init function returned error.
[ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
[ERROR] Unknown/unsupported storage engine: InnoDB
[ERROR] Aborting
InnoDB: mmap(137363456 bytes) failed; errno 12
InnoDB: Completed initialization of buffer pool
InnoDB: Fatal error: cannot allocate memory for the buffer pool
[ERROR] Plugin 'InnoDB' init function returned error.
[ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
[ERROR] Unknown/unsupported storage engine: InnoDB
[ERROR] Aborting
My VPS RAM is just 512M, hence this is crashing. So as a first step I deleted below files.
rm /var/lib/mysql/ib_logfile0
rm /var/lib/mysql/ib_logfile1
rm /var/lib/mysql/ib_logfile1
Though it reduced frequent MySQL crash, still it was crashing at times.
So I thought lets add swap memory. With this, this problem was resolved.
What is swap memory?
If your system memory (RAM) is less, and you are running multiple applications then you are under risk of memroy allocation failures. This issue can be resolved by making use of swap memory, but at a cost of reduced performance. The below steps are followed for increasing swap memory in a linux machine.
Environmnet
OS: Ubuntu 12.04 LTS 64 bit
Steps
Please follow below steps to allocate swap memory in your machine.
- Login as root to system
- Execute below command to see if swap space is allocated previouslyswapon -s
If no swap space allocated, you will get empty list like
Filename Type Size Used Priority - Now you are sure, swap file is not present.
- Chedk disk size using df command and check system memory (HD) and decide how much swap memory you would like add.df
The ouput is as below
/dev/vda1 20511356 4861172 14585224 25% /
udev 240064 8 240056 1% /dev
tmpfs 50184 236 49948 1% /run
none 5120 0 5120 0% /run/lock
none 250908 0 250908 0% /run/shm - So I decided to allocate 2GB of swap memory. Say I want to name the swap file as swapfile.dd if=/dev/zero of=/swapfile bs=1024 count=2048k
mkswap /swapfile
swapon /swapfile
swapon -sWith this, your system swap memory is set as 2GB.
Hold on, if you restart your system it may go off. If you want to make it permanent the executevi /etc/fstabAdd below line of code to /etc/fstab file (at the end of file)
/swapfile none swap sw 0 0 - Executeecho 10 | sudo tee /proc/sys/vm/swappiness
echo vm.swappiness = 10 | sudo tee -a /etc/sysctl.conf
chown root:root /swapfile
chmod 0600 /swapfile - Next you can verify if swap memory is available by executingfree -m
Output looks as below
total used free shared buffers cached
Mem: 490 476 13 0 4 42
-/+ buffers/cache: 429 60
Swap: 2047 0 2047You can see swap line above.