Codementor Events

Mysql error: Cannot allocate memory for the buffer pool AWS EC2

Published Apr 05, 2020

Hello Techies!!

PROBLEM : Cannot allocate memory for buffer pool

SHORT DESCRIPTION : Processor ran out of memory to allocate to the service. It needs more RAM in order to process properly.

PRE-REQUISITE : AWS EC2, Linux instance

While working on AWS EC2 free tier, I came across this problem. This problem occurs when the applications running on your server takes up all the memory and then falls short. Due to this shortage of memory, logs of the service will show you cannot allocate memory error.

Here, I tried to run a django application with various other services present on the server in support of web application but total amount of memory needed by all services to run with ease exceeded the amount of memory free tier instance provide (1 GB RAM).

So, to solve this problem I searched through the internet and after reading many articles came across a solution which I would like to share with all of you.

There is one obvious solution is to upgrade our EC2 instance from free tier to get more RAM.

Another one is to create a swapfile for our instance.

What is swapfile?

It is a space on hard disk used as virtual memory extension of computer’s RAM. It allows OS to use the allocated space as systems Memory. It is more efficient than doing I/O operations on disk as it can be organised as single contiguous space.

STEPS TO CREATE SWAP FILE IN AWS EC2 LINUX INSTANCE :

As a general rule of thumb, It is suggested that swap space should 2 times the physical memory. It should never be less than 32 MB.

To check there are no existing swap files, use the following command to check memory stats of your instance.

free -m

Output:
miscellaneous-2-1.jpg

Here, you can see, there are no swap space in the instance since values in front of Swap is 0 – total, 0 – used, 0 – free

Let’s take the case of free tier. It has 1 GB of physical memory.

So, we will create a swap space of 2 GB.

Create a 2-GB swap file on the root file system. The best way to create a swap file is to use fallocate which creates a file of preallocated space instantly.

sudo fallocate -l 2G /swapfile

Now we can verify the amount of allocated space by using :

ls -lh /swapfile

Output :
miscellaneous-2-2.jpg

This confirms we have a created a swap file of size 2 GB.

Update the read and write permissions of the swapfile. We only want root user to have priviledges.

sudo chmod 600 /swapfile

Setup a linux swap area

sudo mkswap /swapfile

Make the swap file available for immediate use by adding the swap file to swap space

sudo swapon /swapfile

Verify that the procedure was successful

sudo swapon -s

Output :
miscellaneous-2-3.jpg

We can see a swapfile got added to swap space with size 2GB.

Now you can again run “free -m” to check values of Swap must have been updated.

This is how you can add a swap file to solve your problem of unallocated memory.

I hope this helped you in resolving your issue. You can always share your further related issues in the comments section. We are here to help each other out.

Have a great day !! See you guys in next tutorial. You can checkout more of my blogs here.

For further reference look into :

AWS knowledge center – ec2 memory swap file

Discover and read more posts from Piyush Agarwal
get started
post commentsBe the first to share your opinion
Show more replies