1

I am using slicehost.com 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

thanks

// using CakePHP/apache

mgPePe
  • 225
  • 1
  • 3
  • 7

7 Answers7

6

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.

Chopper3
  • 100,240
  • 9
  • 106
  • 238
2

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.

Leon Waldman
  • 132
  • 3
2

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

Jeff Ferland
  • 20,239
  • 2
  • 61
  • 85
  • 1
    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
  1. It's PHP.

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

  3. It's not a lot.

adaptr
  • 16,479
  • 21
  • 33
1

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.

tarzxvf
  • 11
  • 1
1

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).
Tometzky
  • 2,649
  • 4
  • 26
  • 32
0
  1. Why don't you try to find which processes are eating how much memory when heavy swapping occurs by isssuing

    top

  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.

kaji
  • 2,510
  • 16
  • 17