"nice" for memory management

Before the question I give you a bit of background : I am running a website on a VPS. The VPS itselmf runs the apache web server, the PHP code that produce the pages with acvces to a SQLite DB, and runs some background tasks triggered by cron jobs.

These background tasks mainly update DB values, and add new records to be displayed to the website users.

Performance wise, the responsiveness of the website is a priority, but background tasks are not time sensitive, there is not problem at all if they must be deplayed, slowed down, ... . Considering that I have not that much visits (around 4000 page view / day, most of them beeing cached), I can afford to run those on low priority, and they still get the job done. CPU wise, I run apache with "niceness" of 0 (the default), and the background tasks have a "niceness" going from 10 to 18. And everything is fine.

Memory is a problem because some of these tasks have high memory footprint. Some of them actually crash because there is not enough memory left when they need it. What usually happens is :

  1. High memory consuming task 1 is triggered
  2. Users visits the website => Task 1 is delayed (I got no problem with that)
  3. Task 1 resumes
  4. High memory consuming task 2 is triggered. Due to delays, task 1 is not finished
  5. Users visits the website => both tasks are delayed (still no problem)
  6. Task 1 resumes and needs more memory => No memory left and Task 1 crashes
  7. Task 2 finishes
  8. User visits the website
  9. ...

The VPS runs on a 1 Gig of RAM system and there is currently no SWAP defined. I think that adding a SWAP space (for free since it does not cost me more), could be a good solution to my problem. In the course of event described above, some of the memory used by the background tasks could be swapped, thus reducing the performances of those tasks, but then again, I have no problem with that.

What I would like to avoid is to have the apache memory swapped, because it would reduce the responsiveness of the website for the end user. Just like I indicated with "nice" that apache is more important than the backgroudn tasks. If you see another solution to my problem you are welcome to suggest anything. I am also improving the background tasks to reduce their memory footprint, but that is taking a bit of time.


Solution 1:

You can use cgroups to do this. It lets you set limits for memory and memory+swap, so you can force processes into swap.

Alternatively it also lets you adjust the swappiness of each cgroup independently, so you can increase the odds that your background pages will be swapped out:

http://www.kernel.org/doc/Documentation/cgroups/memory.txt