2

I have a strange problem that when I run top command it shows that some processes are using around 1.5GB of swap space but then the overall usage of the system swap is way to less to something around 117MB, so why is that? I thought the overall system swap usage is the aggregated usage of all the processes, which doesn't seem to be true in this case. Here's the output:

Tasks: 392 total,  16 running, 373 sleeping,   0 stopped,   3 zombie
Cpu0  : 95.1%us,  4.9%sy,  0.0%ni,  0.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu1  : 98.0%us,  2.0%sy,  0.0%ni,  0.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu2  : 91.1%us,  6.9%sy,  0.0%ni,  0.0%id,  0.0%wa,  0.0%hi,  2.0%si,  0.0%st
Cpu3  : 95.0%us,  3.0%sy,  0.0%ni,  0.0%id,  0.0%wa,  0.0%hi,  2.0%si,  0.0%st
Mem:   4148160k total,  4007820k used,   140340k free,    15968k buffers
Swap:  4096552k total,   117584k used,  3978968k free,  2909396k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  SWAP COMMAND                                                                                                          
 5784 apache    18   0 1567m 5772 4568 S  0.0  0.1   0:00.03 1.5g C:\windows\system32\explorer.exe /desktop                                                                         
 5776 apache    25   0 1558m 2564 2124 S  0.0  0.1   0:00.00 1.5g C:\windows\system32\winedevice.exe MountMgr                                                                        5774 apache    25   0 1558m 2324 1928 S  0.0  0.1   0:00.00 1.5g C:\windows\system32\services.exe                                                                                  
 9395 apache    15   0 90536 2988 1340 S  0.0  0.1   0:00.00  85m /usr/sbin/httpd                                                                                                   
 9419 apache    18   0 90536 2988 1340 S  0.0  0.1   0:00.00  85m /usr/sbin/httpd                                                                                                   
27016 apache    18   0 91520 4000 2964 S  0.0  0.1   0:00.04  85m /usr/sbin/httpd                                                                                                   
 7773 apache    16   0 91012 3592 1464 S  0.0  0.1   0:07.04  85m /usr/sbin/httpd   
  • Are you using Wine (I see a `Winedevice` process and `C:\windows\system32\explorer.exe`)? If that's the case, the only processes that use 1.5GB of swap space are Wine processes. I don't use Wine myself, but couldn't that simply be Wine failing to report correctly? – jaume Jan 31 '13 at 21:31

2 Answers2

6

Top (at least in some versions) calculates SWAP per process as VIRT - RSS instead of reporting real swap usage. Under Linux the result is a completely meaningless number.

x22
  • 181
  • 1
  • Very accurate: one can take a look at `man top` from Debian/Ubuntu `procps` package which has patched(`./debian/patches/top_1_swap.patch`) man page that explains precisely how `SWAP` is computed in `top`: http://manpages.ubuntu.com/manpages/precise/en/man1/top.1.html – SaveTheRbtz Feb 01 '13 at 09:24
3

Short answer

The SWAP column in topis the computation (roughly) of the swap process usage and any mapped file and/or shared objects used by this process. Mapped files are not part of the swap, they are files read by the process in a certain manner, and shared objects are dynamic libraries (.so) or blocks of memory shared between processes.

So if two processes share a big chunk of memory together, it will be counted twice in top once for each process. This chunk of memory (if there is enough free memory and if it is active) might not be in the swap. This chunk of memory is "reserved" for the process, it does not mean either that it is used entirely, so it is not necessarily occupying much physical RAM.

The man page states: "SWAP is calculated by subtracting physical memory from virtual memory". The computed value should not be rely upon unless you are a developer (and know what you are doing), for the casual users this has no real meaning, see below why.

Long answer, what is virtual memory and the mapped files?

Note: VIRT stands for virtual memory.

Linux process virtual memory

Each process in Linux (and this as similarities in many Unix systems too) is allocated a virtual address space. On a 32bit system, this virtual address space is up to 4GiB (2^32). On a 64bit system. this virtual address space is up to 256TiB (2^48 yes a 64 bit bases system using the current AMD64 or Intel 64bit extension can only address 48 bits of memory).

Each process virtual memory contains the kernel space and user space area. On a 32bit system, the splits depends if PAE is activated or not, so it can be 3GiB/1GiB or 2GiB/2GiB. On 64bit, it is always (AFAIK) half/half, so the first 47bit (128TiB) are for user space, and at the end of the 64 bit area, there is 128TiB reserved for the kernel.

Virtual memory comprises the code, data and BSS of the running process, it includes further the stack and heap, and the memory mapping area. This area contains any shared memory (IPC, shared objects) and can also contains (AFAIK) mapped file in memory. See this nice graphic I've made for 64 bit system.

Some more info about mapped files

One can mapped files bigger than available memory, so the VIRT size can be bigger than the memory. The kernel is clever enough to load/unload the data from the storage to memory to accelerate i/o as much as the available physical free RAM permits.

You should not care as a user about the VIRT size. This is mainly intended for (hard core) developers.

In conclusion, the SWAP column compute the virtual memory minus physical memory (more than probably RSS) this will probably includes the swap part used and any mapped file that are also on disk.

Huygens
  • 1,678
  • 2
  • 19
  • 36