How to Clear Inactive Memory in Linux

This simple tip is based on my experience on how to tweak MySQL Server for best performance.
Just this year, sourcecodester.com has moved again to a new server to served millions of page views every month.

Since our new server is unmanaged, I am having a hard time sometimes configuring our new server, especially when tweaking Nginx, PHP-FPM and MySQL.

By default, MySQL will give you a sample configuration to start with. But this default configuration is not enough if you want to run MySQL to its best possible performance.

Before, we are using MyISAM instead of InnoDB. But as the time pass by, and a new version of MySQL is developed, we migrated our entire database to InnoDB. The reason is that, InnoDB is better than MyISAM. But the problem is InnoDB consumes more RAM compared to MyISAM.

Although we have 32GB of RAM in our dedicated server, but still we are running out of memory sometimes until I found the solution to this problem.

I almost spend two months of research to optimize MySQL without luck. The problem is that, linux consumes a lot of memory and used it for nothing. Though, it’s not actually for nothing, but when you are running your server without rebooting it, the “Inactive Memory” grows over time.

This value of memory consumed by “Inactive Memory” can be found using vmstat –s –S m command.

Here’s the sample output:

As you can see, the inactive memory is 4882 MB. Just before we fixed this problem, the value was around 11 GB.

Inactive memory will be release only if you reboot your system. But since this is a server and we don’t want any down time, all you can do is only find a solution without rebooting your server.

Here’s the command to clear the inactive memory of your linux system:

free && sync && echo 3 > /proc/sys/vm/drop_caches && echo "" && free

In my next article, I will also discuss on how to tweak Nginx and PHP-FPM.

I was impressed with your articles. :) But I have a question. Do you know why inactive memory increases when running MySQL?

Add new comment