Because it is directly associated with the overall responsiveness, any operating system prefers to keep the users’ most frequently used data (programs, files etc) in RAM because data on RAM can be accessed extremely fast when compared to other storage mediums (disk drives, flash drives …).
That said however, since RAM is not cheap, its capacity is limited on most occasions and thus, when dealing with memory hungry programs (and while trying to find ‘room’ for opening new applications), the OS is going to have to move some of the currently unused data into another storage medium (temporarily), which is usually the disk drive.
This (special) location on a disk where data from RAM is moved into (it could be a file or a separate partition), is called ‘Swap Space’, in GNU/Linux. The process of moving data from RAM to a ‘Swap Space’ or moving data into RAM from a ‘Swap Space’, is called ‘page swapping’. Again, the OS only does this if it has no other choice because when compared to RAM, ‘Swap Space’ is very slow and thus loading programs or opening files can take a long time, which if not intelligent managed, can affect the OS’s responsiveness in a very negative manner.
As a result, OS developers have come up with few ways (mainly 2, that I am aware of) of decreasing the ‘page swapping’. One suggestion is to intelligently manage the data on RAM. The other method is to compress data that is about to be sent off to a ‘Swap Space’, within the RAM itself, with high compression ratios, so that RAM at the end can hold more data, hence reducing the need to move data into a horribly slow ‘Swap Space’.
‘Zswap’ in that sense, is a tool that was introduced with the 3.11 Kernel that compresses data which is about to be sent off to a ‘Swap Space’. ‘Zswap’ however is disabled by default, and if you are running under limited RAM capacity, then you can safely enable it in Ubuntu 13.10 (or any distribution that has 3.11 or newer Kernel installed) to see if it improves the overall system responsiveness. For enabling it, please follow the below procedure.
Step 1:
Open your Terminal window and enter the below command.
sudo gedit /etc/default/grub
Step 2:
This will open up the GRUB configuration file. Then as shown in the below screenshot, locate the ‘GRUB_CMDLINE_LINUX_DEFAULT
‘ line and put the below code between the quotation marks (make sure to add a ‘space’ after the existing quiet splash argument).
zswap.enabled=1
Step 3:
Then save your changes and enter the below command to update ‘GRUB’ entries.
sudo update-grub
Once that command finishes running, reboot the computer and when the next time the desktop loads ‘Zswap’ should be running.
Does it really work ?
Well, since it is unwise to take someone’s opinion just for the sake of it ;-), I decided to come up with a very simple test for measuring the performance of ‘Zswap’. Below is a brief info about my hardware.
Intel Core i3-2330M CPU, Intel HD 3000 GPU, 4GB RAM (DDR3), Toshiba 7200 RPM (320GB) SATA HDD, Intel N-1030 Wireless adapter, Realtek network adapter ('RTL8168'), LED display with 1366x768 resolution (60Hz/60FPS). It's a Dell Vostro V-131 notebook.
*. I first performed a clean installation of Ubuntu 13.10 and then I made sure that only 1GB of my 4GB RAM is available to Ubuntu 13.10 (you can easily do that by passing an argument using GRUB configuration file). Then I created a 1GB of ‘Swap Space’ (a ‘swap file’, not a partition) as well.
*. Later I added ‘GIMP’ image editor, VLC (they were both manually installed) to the ‘application launcher’. Then I rebooted the computer and once the desktop was loaded fully, I let it idle for about 30 seconds, then opened: ‘Gnome-Terminal’, Firefox, LibreOffice Writer/Calc/Impress, VLC, GIMP and Ubuntu Software Center.
*. When they were fully loaded I measured the ‘Swap Space’ usage through Gnome-System-Monitor utility. Then I closed all of them, and re-opened some of the applications (listed in the below graphs), each by its previous order of execution (gnome-terminal, Firefox …) and took a note of their individual loading times. Then I rebooted the computer and carried out the same test again and again for 3 times for getting average results.
After that was done, I enabled ‘Zswap’ and ran the same tests (3 times) all over again.
Why did you re-opened the apps and measured their loading times ?
Because if ‘Zswap’ actually works, then application loading times with it enabled should be shorter (compared to the application loading times with it disabled) as more application data can (and should) now be kept in RAM (which again improves their loading times since it minimizes the data exchange with the ‘Swap Space’ which is terribly slow). And the only way to know that for sure it to measure them :).
In any case, by using the gathered data, I came up with the below graphs for comparing.
As you can see, after enabling ‘Zswap’ the ‘Swap Space’ usage has actually increased by about 75.2 MiB (72%) which is the opposite of what it promises of being able to achieve.
Below are two graph of the (re) loading times of some of the individual applications.
Below is the second graph.
As you can see, except for LibreOffice Writer and despite the increased ‘Swap Space’ usage, ‘Zswap’ has been able to reduce the application loading times, though it is nowhere near being exceptional or useful, for that matter. I also noticed that the overall system responsiveness actually got worsened after enabling ‘Zswap’! (while opening all the applications for the first time).
As an example, ‘Compiz’ (window manager of ‘Unity’ desktop shell) dims-out application windows that are not responding, and when you open a lot of memory hungry apps, ‘Compiz’ sometimes dims them out for few seconds, even though in reality they are not actually stuck. This happened on both occasions, but it got worsened after enabling ‘Zswap’. Also, the whole PC got stuck (not being able to move the mouse …) for few seconds in all 3 tests after enabling ‘Zswap’ and it did not occur in such magnitudes while running tests without it being enabled.
So from the perspective of this simple test, ‘Zswap’ has failed because the responsiveness was not improved, though the re-loading of applications were improved slightly. However, a simple test as this one is not enough for making claims such as that says ‘Zswap’ is going to fail on your computer too, as one has to take many things into account before making such claims, but I tried hard for not being careless, thus I stand firmly behind the results.
In any case, I humbly advice anyone to actually enable ‘Zswap’ if you are running a computer with limited RAM availability to see whether it improves things or not. And if it makes things worse, you can always disable it with ease. You can read more detailed articles (with benchmark results) from here and here.
If you think the test is not that accurate in terms of trying to measure ‘Zswap’, then you are more than welcome to send suggestions, and correct me where I am wrong.
Great work – how did you find zswap compared to zram? It would be nice to measure these two methods of compressed swap. As you already have shown – theory is theory, practic on the desktop is the other truth.
Hi salvadhor,
First of all, thank you :). Secondly, no, I actually haven’t tested ‘zram’.
IMHO the application re-loading is not a real-world scenario for the low-memory systems. I think it should be tested on a 1GB RAM system, where someone tries to work with several applications together. For example firefox with “Don’t load tabs until selected” turned OFF – to load all tabs plus editing a big file with open office and drawing something in gimp. The memory usage should be over the RAM installed so the swap to get used. For example ~ 2GB memory usage with 1GB RAM installed. Then the responsiveness of the system should be evaluated. When you switch from app to app, you’ll notice that it’s slow with heavy I/O. …
Interesting idea, & I do agree with you that the test I ran might not exactly represent a common scenario, on a low memory system. But, what about a system that has, say 512MiB of RAM capacity, would it not be reasonable to assume that a user might still try to run 3 or 4 somewhat memory hungry programs, on it ?
In any case, I strongly believe that the test I ran should still be able give some idea about Zswap’s performance because it was able to increase the RAM usage up to a point thus forcing the OS to use the ‘Swap space’ (even if it was not by that much). Sure one will notice that it is slow when switching from app to app (under heavy I/O), but again, remember, I am considering the ‘felt responsiveness’, when compared (before & after using ‘Zswap’).
yeah it slows down my system too during heavy io (couple of btrfs defrags + compresses, file copies, ..), but then I’m not really ram constrained as I have 16GB
16gb ram
500 tb evo 840 ssd (swap is on this one.. in b4 but swap kills ssds and tlc ones are especially voulnrable)
3 tb wd red hd
3 tb wd red hd
750tb wd something laptop hd
it basically goes from smooth to stuttering as a lot and cpu load from ~50% to 80~
Thank you for the input, that’s one hell of a system you have there! 😉 . Not exactly sure to what to make up with the CPU load, maybe it’s a bug, or simply the result of data compression.
the original 50 was a couple of btrfs filesystems, being compressed/defraged and me browsing the net (I thinik I had ff and chrome open), electrum and kde’s desktop effects (window transparency and other stuff
the additional 30 came when I turned on zswap (for no real reason, just wondered what would happen and since it only needs one echo to enable….)…. maybe it would have been faster with lz4 vs lzo, but then with lzo if any of the data was compressed by zswap or btrfs it shouldn’t need to be decompressed and re-compressed
thanks for the compliment for the system, but i’m a bit sorry i bought only the i5 4460 and not one of the cheap hasswell xeons (basically the same thing as a i7 but costs less (somewhere in the middle between the i5 and i7, and I still plan to get a few more ssds + 16gb more ram and a real graphics card (not really sure about that, are there any modern games even worth playing anymore and/or gpgpu apps and the card needs to be cheap (a little over 200 eur which would atm get me some kind of gtx 960 or r380)?) or is it just shitty console ports… i mean the igpu runs bg2 just fine)
note i spent almost a year stuck with a windows tablet with 2gb ram and crap io (some slow emmc)… the cpu was surprisingly fast but the ram and io just killed it… the z3775 would actually be an awesome cpu if these things had 4-8gb and a real ssd (nothing extreme, 120-250gb would be more than enough)
let me tell you…. WHOEVER THOUGHT 2GB RAM WAS A GOOD IDEA deserves to use it for a few months, open ff, chrome (less then 20 tabs) each so nothing extreme and enjoy your crashes or out of memory errors
You’re getting higher CPU usage because zswap compresses pages you swap in, and decompresses them again for access.
Checkout `free -mt`. While most of your memory is usually in use, a lot of that is filesystem cache that can be freed, which is usually the first to go when you start running out of memory because it doesn’t require swapping, you just read the file back into cache when needed.
I believe what you’re referring to as application data, are the files an application depends on, since the application is no longer running in the context you used the term. A system, or even applications, that only runs for a short time before being rebooted/closed will not benefit from zswap because your system doesn’t have enough time to gather statistics on what pages should be swapped. Try letting programs run for awhile, then switch between applications or different sections of a program (such as browser tabs) on a system with most of its memory in use, this will cause pages to be churned between regular memory, zswapped memory, and (hopefully not) swap.
For more info, these may be of use:
https://linux-mm.org/PageReplacementDesign
http://serverfault.com/questions/85470/meaning-of-the-buffers-cache-line-in-the-output-of-free
https://www.ibm.com/developerworks/community/blogs/fe313521-2e95-46f2-817d-44a4f27eba32/entry/new_linux_zswap_compression_functionality7?lang=en
Hi,
Thank you for the excellent suggestion. However, I’m still questioning the core functionality of Zswap. In other words, I was simply interested in knowing if Zswap was going to be able to reduce ‘page swapping’ on a system with limited RAM capacity. Maybe your observations are correct, but still, since the core functionality of Zswap is to move data that is about to be sent off to a Swap Device, into a compressed memory pool on RAM instead, I was simply testing whether it could’ve at least reduced the amount of data that’ll be sent off to the Swap Device (thus improving the overall responsiveness at least), but unfortunately, the observation was the complete opposite.
Hi, there
I’m a web developer and use PyCharm, Angular4 (ng) and Google Chrome with dev tools opened as well on Ubuntu 17.10.1.
I was facing issues of RAM going 100 and swap being used with HDD. Which actually halt my pc and I had to reboot after an hour. After reading about zswap, I thought to give it a try. I didn’t feel any lag in performing operations and plus point no system halt observed. Can you please suggest me some tools for bench-marking. So that I can share results.
I had browser pauses (20+ seconds unresponsive) with zcache on modern CPU and current Fedora. It always struck me as counterintuitive to steal some ram for paging when paging is to conserve RAM. I’m sure there are some valid use cases but for day to day browsing Fedora handles memory and swap near perfectly.