Can I limit the memory usage of apache + passengers on a server with no swap space

I am running a rails app with apache + passenger on virtual servers that don't have any swap space.

The site receives a decent amount of traffic with 200K + daily requests, and sometimes the whole system will run out of memory, resulting in odd behavior throughout the system.

The question is, is there a way to configure apache or passenger so that it doesn't run out of memory (for example, gracefully restarting passenger instances when they start, say more than 300MB of memory).

The servers have 4GB of memory and I am currently using the PassengerMaxRequests passenger option, but this does not seem to be the hardest solution.

At the moment, I also cannot switch to nginx so that there is no way to save some room.

Any smart ideas that are probably missing are appreciated.

Edit: my workaround

I didn't go with restarting Rails instances when they exceed a certain amount of memory usage. Engine Yard wrote a great blog post on ActiveRecord memory bloat problem . This is our prime suspect on this matter. Since I didn't have a lot of time to optimize the application, I set PassengerMaxRequests to 300 and added 2GB extra memory to the server. Everything has been good since then. At first I was concerned that restarting Rails instances is constantly slowing things down, but it doesn't seem to affect me.

+2


source to share


4 answers


If you mean "limiting" as killing these processes, and if this is the only application on the server, and this is Linux, you have two options:

Set the maximum amount of memory a single process can have:

# ulimit -m
unlimited

      



Or use groups for similar behavior:

http://en.wikipedia.org/wiki/Cgroups

+1


source


I would advise not to restart instances (if possible) that go over the "memory limit", because this can put your system in infinite loops, where the process repeatedly reaches this limit and restarts.

Perhaps you could write a simple daemon that constantly monitors processes and kills anything that spills over to a certain amount of memory. Be sure to write down any information about the process that did this so you can fix the problem every time it appears.



I would also look for some real swap space there ... This looks like a bad hack.

0


source


I don't have a canned solution, but you can use the two commands that come with Passenger to track memory and nr process usage: passenger-status

and sudo passenger-memory-stats

, see the Nginx User Guide or the Apache User Guide .

0


source


I have an issue where passenger processes get out of control and consume too much memory. I wrote the following script that helps keep things in check until I find a real solution http://www.codeotaku.com/blog/2012-06/passenger-memory-check/index . This might be helpful.

Passenger web instances do not contain important state (generally speaking), so killing them is usually not a process and the passenger restarts them as needed.

0


source







All Articles