zram swap on Desktop

John Moser john.r.moser at gmail.com
Sat Mar 3 08:50:03 UTC 2012


On 03/03/2012 12:05 AM, Phillip Susi wrote:
> On 02/27/2012 08:58 PM, John Moser wrote:
>
> I believe that swap space is only actually freed when the memory it is 
> backing is freed.  In other words, if the process frees the memory, 
> the swap is freed, but when the page is read back in from swap, it is 
> left in swap so that the page can be discarded again in the future 
> without having to write it back out again.  This can lead to some 
> wasted memory by having pages still in zswap that have also been moved 
> back into regular ram.
>

This may be true.  Also zswap seems to not bother compacting until it's 
being added to, so it bloats and then doesn't shrink much.  For example 
you can put 500MB into zswap at 29% compression ratio and 30% total 
memory usage, free 200MB and have it stay at 29% compression ratio with 
the actual data 70MB smaller ... but around 40% total memory usage 
because you only saved 20MB of RAM, as fragmentation left a lot of empty 
space in the zswap in pages that also contain in-use compressed data.  
It doesn't background compact.

>> - Desktops may benefit by eschewing physical swap for RAM
>> * But this breaks suspend-to-disk; then again, so does everything:
>> + Who has a 16GB swap partition? Many people have 4GB, 8GB, 16GB RAM
>> + The moment RAM + SWAP - CACHE > TOTAL_SWAP, suspend to disk breaks
>
> Cache can be discarded at hibernate time, so you only need RAM + SWAP. 
> Also people generally don't go to hibernate while that much ram is in 
> use, and almost never have much swap used.  Also, I *think* I saw a 
> patch somewhere recently to address this by avoiding the zswap device 
> for hibernation and falling back to other swaps instead.
>

Well I mean I shut off my VMs and all.  A quick glance and some math at 
top tells me right now I'm using 2.3GB ... and there's nothing I'd want 
to close if I decided to hibernate my computer for the night.  Closing 
down programs sort of defeats the purpose.  Maybe LibreOffice.
> It looks like CleanCache/CompCache is a better solution since it 
> avoids the step of emulating a block device.
>
>

zcache on cleancache is just for compressing page cache (file backed), 
not swap (anonymous).  zcache on freeswap is the solution for 
compressing swap without a block device, also written by the zram guy, 
not sure how to configure it though.  CleanCache and zcache are in 3.2 
staging, freeswap is not.  For what it's worth I'm running both zram 
swap and CleanCache zcache in tandem; one does not affect the other.  
I've tested this running Ubuntu 11.10 at 288MB of RAM, which is painful; 
it's crippling MUCH faster without zcache enabled.

http://i.imgur.com/aAeSE.png

All of the above is swap on zram, no disk backed device.  With zcache 
enabled and two CPUs (kswapd uses 60%-70% CPU like this!) I can get this 
far with about 20-25MB of page cache, and then I can still raise and 
lower Firefox and open a new tab in gnome-terminal to run killall on 
Firefox (attempting to close Firefox was taking too long for dialog 
boxes to load and draw--it annoyed me).

I'm pretty sure zram will be superceded by zcache on freeswap.  zcache 
is a tmem backend, freeswap and CleanCache are freemem frontends.  Any 
backend can be used on any frontend, so when (if) the freeswap frontend 
goes into mainline zcache will load onto that.  zcache is zram, it uses 
zram's xvmalloc when running on freeswap (uses a different allocator on 
page cache) and everything, it's even written by the same guy.  zcache 
is just zram ported to tmem, which makes it both the same and 
separate--it is zram, but it's not zram.  As tmem looks like the way the 
kernel is moving in the future, zram will probably go away--the 
appropriate compressed in-memory file system is tmpfs with zswap, as RAM 
used to back tmpfs can be swapped and thus zcache and zram will both act 
to compress tmpfs, so zram's usefulness as a block device in RAM that 
can house a compressed file system is limited.

Of course, that theory then raises the question:  what about when you 
don't have swap?  Does the kernel make its swapping decisions and then 
ask freeswap if it's got something to do with this memory when you don't 
actually have any swap space?  (i.e. attempting to swap without swap)

Anyway in the end I feel the situation boils down to this:  You get an 
Intel CPU these days and it comes with 6 cores.  What do you do with 6 
cores?  You run some pretty extreme applications.  What does your 
average desktop do with 6 cores with hyperthreading enabled running 12 
way SMP?  It uses 1-2 cores ... what do you do with the other 10?  Run 
compression/decompression and compact fragmented compressed swap, what 
else?  On something like i.e. the XO laptop where RAM is limited, it's 
simply a necessity*.  On a desktop with a smaller RAM space and a slower 
CPU, it's a livable trade-off that does enhance performance some (it's a 
godsend on a dual core with just 512MB RAM trying to run Unity).



*I believe the use is different on a system like the XO.  On the desktop 
you might want 2/3 of your RAM to be compressed and 1/3 working set when 
you have tons and tons of CPU--if you think you need 17G and you only 
have 8G.  More correctly, you can take your system out that far with a 
visible but livable decrease in performance.  But on a system like the 
XO you want to figure out what your typical working set is and make the 
zram swap space 3 times bigger, as data is on average 1/3 its original 
size when compressed... this will avoid excessive swapping from 
additional memory pressure above the typical working set.




More information about the Ubuntu-devel-discuss mailing list