user3048109
user3048109

Reputation: 217

Why is MySQL server using swap space even when there is enough RAM to map pages?

We are using MySQL 5.1.72 version

$ free
             total       used       free     shared    buffers     cached
Mem:         16027      14171       1856          0        584       3303
-/+ buffers/cache:      10283       5744
Swap:         2047       1274        772

The above output shows the memory details on sesrver taken yesterday (27-feb-2014) before the backup. One can see that more than half the swap space(about 1.3 GB) is already used and only 700 MB is left. With little mathematics we can conclude that there is about 5GB or Physical RAM left on the box. 14171-(584+3303) = 10283 Actual RAM used.

Now total RAM – Actual RAM used = 16027-10283 = 5744 MB The question is why MySQL server is using SWAP when there is enough RAM to map pages.

Upvotes: 3

Views: 9836

Answers (3)

miklosq
miklosq

Reputation: 95

As said, the OS virtual memory algorithm controls which page to swap out from memory and which page to allocate from physical memory. The above output given by you shows that an earlier state on your system. IMHO, it was running out memory and the OS had to use the swap area of virtual memory but did not re-allocate them since then. To make sure what memory a certain process use, you can see it with 'ps' command. For example, take a look at the memory allocation details of all bash processes on my system below:

 $ ps -C bash -o comm,pid,vsz,rsz,cmd
COMMAND           PID    VSZ   RSZ CMD
bash             2007  20572  3216 /bin/bash
bash             2055  20552  3180 /bin/bash
bash             2103  20556  3192 /bin/bash
bash             4640  21388  4100 /bin/bash
bash             4688  20784  3492 /bin/bash
bash             8200  20608  2984 -bash

In the above output, the 'RSZ' column shows how much non-swapped physical memory that a task has used. The 'VSZ' shows the virtual memory size that a task is allocated, that is physical memory and swapped memory (if any). Values are in KiB (kilobytes). Now you can calculate, but take the shared memory pages into account, which is not shown in the above output.

See the ps man for more info.

Upvotes: 0

Zafar Malik
Zafar Malik

Reputation: 6854

Even now RAM is free on server, but if any time server uses swap due to any reason like heavy query/traffic etc. then after some time your normal RAM will be freed but swap will not free, so you need to restart your server to free swap.

Note: Even now your RAM is free, but as server has been taken swap, so server operations will be slowed due to this, so need to clear swap usage.

Upvotes: 0

canni
canni

Reputation: 5885

Whatever to use swap or not is an OS decision, MySQL has nothing to do here. Check yours kernel swappiness flag http://en.wikipedia.org/wiki/Swappiness

Upvotes: 2

Related Questions