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.


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:


  • Actually there is no way to force a process to stay in ram except the kernel. But you can do some tuning. If you are sure you have enough ram, set swapiness (which is kernel option) to a low value. – fpmurphy Dec 26 '11 at 16:27
  • I don't want to force apache not to SWAP, I just want to indicate to the kernel that, in case of need, background tasks should be swapped before apache. Apache can be swapped if the kernel really needs to swap it for some reason. – Snowangelic Dec 27 '11 at 10:05
  • Yeah check out the documentation, you can set different swappiness settings for different groups of processes indicating which groups should be swapped first in cases of need. – n8whnp Dec 27 '11 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.