How to check if MySQL has been swapped out?

How to check if any MySQL memory has been swapped out? This post explains it.

Check if system is currently using any swap:

server ~ # free -m
             total       used       free     shared    buffers     cached
Mem:          3954       2198       1755          0        190       1040
-/+ buffers/cache:        968       2985
Swap:         3906          0       3906

In the above example swap is not in use, so no further checks would be necessary.

However if free command would report some usage, how to check whether MySQL memory was swapped out or not?

It is not possible to determine that using standard tools such as ps or top. They will report various memory related information per each process, but no clear indication whether something is in RAM or in swap space. But it is possible with this trivial command:

awk '/^Swap:/ { SWAP+=$2 } END { print SWAP" kB" }' /proc/$(pidof mysqld)/smaps

For example:

server ~ # awk '/^Swap:/ { SWAP+=$2 } END { print SWAP" kB" }' /proc/$(pidof mysqld)/smaps    
612 kB

Linux reported 612KB of MySQL memory to have been swapped out. /proc/<pid>/smaps file was introduced in kernel 2.6.14, so it should be available on many, even not so recent, Linux distributions.

On Solaris, the quickest way is:

root@osol1:~# pmap -S 925 |grep mysqld
925:    /opt/csw/mysql5/libexec/mysqld --defaults-file=/opt/csw/mysql5/var/my.
0000000000400000       8784          - r-x--  mysqld
0000000000CA3000       1940       1940 rw---  mysqld

The third column shows the swap usage.

If you need more infomration about How to prevent swapping on a MySQL server?, this post will drive you through all details.

[MySQL Health Check]
About Thomas

LAMP consultant with 12+ years of experience in online media industry. Enthusiast of modern web technologies, networking and databases.

Comments

  1. Modern versions of ‘top’ can show the process list ordered by swap usage. Run ‘top’, press Shift-O (Order by) and press P to sort by swap usage.

    • Dmitri,

      Indeed, the latest version of top finally shows it correctly. It is available on CentOS6, but it’s not available from the Gentoo portage for example.

      • watch out the version of procps ! as we just discussed it on chat, I comment it here, it may help some pple:

        RedHat 6.2 returns the right value in top, it uses procps-3.2.8-21.el6.x86_64
        RedHat 6.0 returns an exotic value in top, it uses procps-3.2.8-14.el6.x86_64

        From top’s man page in RHEL6.0:
        p: SWAP — Swapped size (kb)
        The swapped out portion of a task’s total virtual memory image.
        From top’s man page in RHEL 6.2:
        p: SWAP — Swapped size (kb)
        Per-process swap values are now taken from /proc/#/status VmSwap field.

Speak Your Mind

*