Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Here is the following info:

Do I need more swap?

No, disk caching only borrows the ram that applications don't currently want. It will not use swap. If applications want more memory, they just take it back from the disk cache. They will not start swapping.

But I conducted an experiment that show something different. Just take a look:

# free -m
             total       used       free     shared    buffers     cached
Mem:          1001        941         59          0          2        127
-/+ buffers/cache:        811        189
Swap:         2347        693       1654

There's 811MiB in RAM and 693MiB in SWAP. Now is the time to close some apps, and I got something like this:

root:~# free -m
             total       used       free     shared    buffers     cached
Mem:          1001        531        469          0          4        126
-/+ buffers/cache:        400        600
Swap:         2347        361       1986

So, now it's 400MiB in RAM, and 361 in SWAP. If I run swapoff, there should be 400+361=761 MiB in RAM, but that's not going to happen:

root:~# swapoff -a
root:~# free -m
             total       used       free     shared    buffers     cached
Mem:          1001        860        140          0          6        251
-/+ buffers/cache:        602        398
Swap:            0          0          0

There's only 602MiB in RAM, and the question is where the hell is the other 159MiB ?

I thought it's because of the ZRAM feature. The following are statistics of SWAP devices before running swapoff:

root:~# swapon -s
Filename                                Type            Size    Used    Priority
/dev/mapper/debian_crypt-swap           partition       2097148 99620   10
/dev/zram0                              partition       153596  135072  70
/dev/zram1                              partition       153596  135092  70

But I also checked the normal SWAP partition, and the effect was more or less the same, just the amount of cached data was smaller (60MiB).

Is there a way to prevent SWAP from collecting cached data?

share|improve this question
AFAIK swap does not collect cached data. What makes you think it does? Or is this a pure conjecture to explain on the figures? How much data do you have on tmpfs (df -t tmpfs)? I'm not familiar with zram, why can't it explain what you're seeing? –  Gilles Feb 14 '14 at 15:36
Just look at the values. How can you explain that after swapoff there's -160MiB in RAM? –  Mikhail Morfikov Feb 15 '14 at 1:02

1 Answer 1

Swapping and caching is not as simple as one might think. The kernel tries to balance these two (and thus the available memory as well) so that it gets as much performance as possible and the metrics can vary greatly. That means that under some circumstances it might be actually preferable to

  1. swap out code/data and keep cache in RAM to make sure cached data is available to the application that was not been swapped out.

  2. swap out cache, because it will be more localized (less disk seeks) - I'm not sure Linux (or any other kernel for that matter) has this sort of optimizations, but I wouldn't be overly surprised if it did. Also note that tmpfs (at least in Linux) is build on top of cache, so anything you put there will be swapped out if needed. And these days it is often used as /tmp or /run which (especially the first one) can incur non-negligible payload.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.