Linux memory leaks workaround fix
  • 1

Linux memory leaks workaround fix

Memory leaks, can cause the whole server to be inaccessible, for example today I ran into this problem.


Symptoms: Slow website performance, loading and navigating.


What’s the workaround for memory leaks?

For example as a system adminis. Shouldn’t edit the customer code to fix the memory leak.

I didn’t edit the code, I chose to fix it by editing web server configuration files.

So what I do instead as a work around for the following errors:

  • 502 bad gateway
  • Gateway timeout
  • 500 internal error


You have to learn first how to dig in your web server log files.

We’ll try to figure out what’s causing the memory leaks .

We’ve covered log reading in a previous post which is available here

Here are also some common error codes you might run into:

mod_fcgid: can’t apply process slot.


The previous error, is for example if you’re using php as an apache module.


 Connection reset by peer) while reading response header from upstream.

This error would be cause by miscommunication between apache and nginx.


So how I fixed all of this mess is simple by playing with the following values:


memory leaks apache fix


These are actually the values for apache process, the most important line remains:


MaxRequestsPerChild  Check this manual  

My server was running out of memory today, but not anymore!

For the optimal values
Timeout 150
KeepAlive On
MaxKeepAliveRequests 1000
KeepAliveTimeout 5

<IfModule prefork.c>
StartServers      20
MinSpareServers   20
MaxSpareServers   30
ServerLimit      256
MaxClients       256
MaxRequestsPerChild  10000

All the above has saved me more than 5GB memory.

Now let’s have a look on our resources using the famous top command no more memory leaks:


Fixed apache memory leak and nginx


As you can see I still have  plenty of memory left on a website with 100k+ hits per second.


Believe it or not, it all started with wordpress database connection error  🙂 More like this:


Error establishing database connection


If you happen to face similar problem, at least you know where to start now.

Comments are closed.