Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using 256MB hosting for my site.

Unfortunately it is swapping almost all the time. When I type in console free i get something like:

            total       used       free     shared    buffers     cached
Mem:        251140     228968      22172          0        596      17356
-/+ buffers/cache:     211016      40124
Swap:       524284      60944     463340

If I reboot the server, the swapping stops for a little bit and then starts again.

How can I:

1) figure out which processes/functions cause the swapping

2) How to fix the issue

3) figure out if 256MB is too little and its time for upgrade rather than optimization


// using CakePHP/apache

share|improve this question
RAM is cheap. Buy more. – Tom O'Connor Jan 30 '12 at 16:59

1) Try ps aux and looking at /proc/$pid/status and /proc/$pid/smaps

2) Add more memory.

3) 256MB is clearly too little given your current load.

share|improve this answer

Are you using apache?

in case you are, do the follow:

  • Open top (just type top on a terminal)
  • Press shift M to order by memory consuption
  • On the Column RES you can see how much memory your apache instances are reserving

Lets say that you are consuming 20 Mb per process.

Then, do a simple math. put aside 156Mb for the SO, and divide the other 100 by the amount of memory that one apache is consuming: 100 / 20 = 5

This will give you how many apache process you can have simultaneously.

Then, go to your apache config (/etc/apache2) and find inside your main config file the following settings: - StartServers - ServerLimit - MaxClients

And set all them to 5.

Please keep in mind that 256 is too small for a apache setup. I would not run apache with less then 2Gb of memory.

share|improve this answer

For diagnosis over who is fighting, I suggest installing atop. Atop will show you the changes in a process level. For a quick demonstration, I primed MySQL to use all the memory it had available to it, then worked with some large files to force the MySQL memory to be swapped out in favor of caching my large test files. Below is a screenshot showing MySQL trying to read memory back in from swap (image instead of text so you can see highlights).

Notice the majflt and minflt numbers when MySQL tries to reach something that has been swapped out. You can also see other memory statistics, and really anything you'd ever want to get from top, ps, etc. {apt-get,yum} install atop.

atop showing majflt and minflt

share|improve this answer
The other incredibly useful thing that atop shows you is process exits. A process that's constantly starting and dying won't reliably show up on a top listing, and if it's the only thing doing that, you won't notice the PID numbers climbing quickly, either. With atop, you'll see lots of process exits in the bottom pane, indicating the problem. – Andrew Beals Jan 30 '12 at 19:05
  1. It's PHP.

  2. Add more memory, or reduce the number of concurrent PHP processes

  3. It's not a lot.

share|improve this answer

You can lower the swappiness, first check the current value with cat /proc/sys/vm/swappiness then change the value sysctl -w vm.swappiness=10

Also, you can always disable swap with sudo swapoff a or editing /etc/fstab

Nowadays I rarely use swap, even in small VM like yours.

share|improve this answer

You can make Apache use less memory, but it won't be easy.

  • Disable non-essential services on your server.
  • Use worker MPM module with PHP as FastCGI (it is incompatible with PHP as a module).
  • Configure worker module to use moderate number of MaxClients (for example MaxClients 64) and moderate number of threads (for example ThreadsPerChild 32).
share|improve this answer
  1. Why don't you try to find which processes are eating how much memory when heavy swapping occurs by isssuing


  2. After finding which process is taking the wholesome bite of memory, it should be first step as to how to fix the issue. eg. if mysql query is taking too much memory, maybe you decide to limit the memory of mysql server or maybe you do query optimization

  3. well 256MB is really seems small. Since you mentioned apache and i know apache by default comes with prefork mode of operation, it could be the real culprit. Consider running apache in worker mode with only needed modules started(usually apache comes with lots and lots of modules automatically started) or replacing apache altogether with lightweight Nginx.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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