How to read `top`. Process list says processes have swap but overview says no swap is being used

I am using top to monitor my server. The swap row reads

Swap: 1044220k total, 0k used, 1044220k free, 148544k cached

I have hit O p to sort by swap.

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  SWAP COMMAND 
  703 mysql     20   0  930m  43m 7092 S    0  8.9   0:00.66 886m mysqld                                                         
 1555 www-data  20   0  346m 9792 3208 S    0  2.0   0:00.00 337m apache2                                                        
 1559 www-data  20   0  347m  10m 3712 S    0  2.2   0:00.01 336m apache2                                                        
 1567 www-data  20   0  345m 9448 2552 S    0  1.9   0:00.01 336m apache2                                                        
 1557 www-data  20   0  345m 9452 2556 S    0  1.9   0:00.01 336m apache2                                                        
 1562 www-data  20   0  345m 9508 2344 S    0  1.9   0:00.01 336m apache2                                                        
 1566 www-data  20   0  345m 9684 2684 S    0  1.9   0:00.02 335m apache2     

I was wondering how I should read this. Is this the swap available to these processes?
This morning top displayed mysqld was using 1.1 gb of swap and 30% of my swap was used up. AFter throwing more ram at the machine mysqld is down to 886m and top says swap usage is 0 (that math seems to makes sense). So I'm just wondering what is the swap column displaying?? Why doesn't the swap overview reflect the sum of the swap column? Thank you.


Solution 1:

It is calculated by subtracting physical memory from virtual memory:

SWAP = VIRT - RES

man top for more details:

   o: VIRT  --  Virtual Image (kb)
      The  total amount of virtual memory used by the task.  It includes all code, data and shared libraries
      plus pages that have been swapped out and pages that have been mapped but not used.

   p: SWAP  --  Swapped size (kb)
      Memory that is not resident but is present in a task.  This is memory that has been  swapped  out  but
      could  include additional non-resident memory.  This column is calculated by subtracting physical mem‐
      ory from virtual memory.

   q: RES  --  Resident size (kb)
      The non-swapped physical memory a task has used.

Solution 2:

I think the top calculation is wrong, i created the following program and i launched in server without any runnging software

#include <stdio.h>
#include <stdlib.h>


#define ALLOC_SIZE(e)( e * 1024 )

int main(){
       char *p = malloc(ALLOC_SIZE(1024 * 1024));
       sleep(190);
}

gcc -o kk kk.c

./kk &
[1] 9880

top -p 9880
Swap: 31457272k total,        0k used, 31457272k free,   392892k cached

PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  SWAP COMMAND
9880 root      16   0 1027m  352  284 S  0.0  0.0   0:00.00 1.0g kk

Free output

free -m
           total       used       free     shared    buffers     cached
Mem:        128966        715     128250          0         87        383
-/+ buffers/cache:        244     128721
Swap:        30719          0      30719

Meminfo

grep -i swap /proc/meminfo
SwapCached:          0 kB
SwapTotal:    31457272 kB
SwapFree:     31457272 kB

In few words, top says i have 1GB of swap used, but from previous output i don't have any swap space occupies, so i don't have any portion of memory swapped out to backing storage.

The rest of (VIRT - RES) is virtual never touched