Enabling RAM Compression on Lubuntu

Many of us are using systems lean on RAM, like 4 GB Chromebooks and 2 GB virtual machines. While such low-RAM systems are useful and usable, they can also be a pain when you’re trying to do anything intensive. Thanks to Linux’s “do anything to avoid crashing anything” behavior, when you run out of RAM, the whole system slows to a usually unusable crawl, as it starts desperately juggling the system’s memory contents in an attempt to keep everything working. While this probably works great on servers that got slammed for a minute, it works very poorly on desktops, and it usually ends up with the user forcing a poweroff and losing all the work they were doing.

While this problem can frequently be solved by simply making a swapfile, some of us have reasons to not use a swapfile. For instance, maybe we don’t want the wear-and-tear on the flash storage of the system we’re using, or maybe we just don’t have enough drive space to permit a sufficiently sized swapfile (both legitimate concern on a Chromebook with a 16 GB eMMC chip for storage, or when using a fully installed Lubuntu system on a flash drive or SD card). Thankfully, Linux provides a very good solution for tight spots like this, and that is RAM compression.

RAM compression works by turning a significant portion of your system’s RAM into a compressed RAM disk. The RAM disk acts just like a typical storage drive, but it’s stored entirely in your computer’s RAM. Anything stored in the RAM disk is compressed so that it takes less RAM. Once a RAM disk is created, you can turn it into a swapfile. This places your swapfile in RAM, but compressed. The result? Once your system starts to run low on RAM, any data that would usually have been swapped to disk instead gets compressed and socked away in the RAM disk. This trick lets you store more data in your RAM than is supposed to fit, essentially giving your system extra RAM at the expense of some CPU power, and enabling you to do tasks that wouldn’t usually be possible with a low-RAM system.

RAM compression is useful even on systems that have plenty of RAM. This is because RAM compression is lightning fast, faster even than swapping to an SSD. I have a desktop computer with 16 GB of RAM, and I almost always use RAM compression on it because it works so well and is so fast. Even when I max out my free RAM and the compressed RAM starts filling up, my system still performs great. I can hammer the thing with four simultaneous VMs, a heavyweight host OS, and a memory-hungry web browser playing music and doing research, and still be sailing smoothly when my system usually would have ground to a halt. It feels like having 24+ GB of RAM, when I only have 16 GB.

So that’s what RAM compression does and how. Now, for how to actually use this glorious solution:

The quick way

The fastest way to enable RAM compression is by hitting Ctrl+Alt+T to open a terminal, and then slapping the following command sequence into it:

sudo su -
modprobe zram
zramctl --find --size=3G
mkswap /dev/zram0
swapon /dev/zram0

This command sequence does the following things:

  1. Elevates your privileges to root. This allows you to run anything as admin. You will be asked for a password.
  2. Enables compressed RAM disk support.
  3. Creates a 3 GB compressed RAM disk. Note that the RAM disk won’t actually take all of this space you give it when you create it, and it almost certainly won’t take up anywhere near that much space even when it’s packed full.
  4. Sets up the RAM disk to act as a swapfile.
  5. Tells Linux to use the RAM disk as a swapfile.

Note that this assumes that you’re not already using ZRAM devices for anything else. Also, this command sequence is for a system with 4 GB of RAM - you should adjust the zramctl line for your system. I usually set it to make a RAM disk that’s 1 GB smaller than my total RAM, so on a 2 GB machine, it would be zramctl --find --size=1G, while a 16 GB system would use zramctl --find --size=15G.

That’s it! Your system is now compressing RAM, and you should immediately benefit from the extra memory.

The downside with this approach is that you have to repeat the process every time you boot or reboot your computer. This might not be so bad… if you can remember to do it every time it’s needed. Sadly, I’ve learned the hard way that human memory isn’t reliable here - I’ve forgotten to enable RAM compression many times, and had my systems run out of RAM or nearly run out of RAM many times as a result. So if you’re just trying out RAM compression, this works, but if you intend to rely on it to make your system more usable, you should probably set up your system to enable RAM compression every time you boot up. That’s what the next section covers.

The permanent way

If you decide you like what RAM compression does for your system, you can use systemd to automatically enable RAM compression when the computer boots up. The way I do this involves making a Bash script and a systemd unit.

First, pop open a terminal window with Ctrl+Alt+T. Next, type ls /usr/local/bin/rampak.sh; ls /etc/systemd/system/rampak.service. This command should return two “No such file or directory” error messages. If so, then we know it’s safe to create the files for enabling RAM compression.

Next, type sudo su - to gain admin privileges. You will be asked for your password when you do this. Once you’re in, type nano /usr/local/bin/rampak.sh. This will open up a text editor in your terminal, where you can create the Bash script for enabling RAM compression.

The Bash script looks like this:

#! /bin/bash
modprobe zram
sleep 1
zramctl --find --size=3G
mkswap /dev/zram0
swapon /dev/zram0

Note the new sleep 1 line - this is important, the script won’t work without it. I’m guessing this is because the modprobe zram command “finishes” before the module is fully ready for use. Adding the 1-second sleep makes everything work on my test system. Again, you should modify the zramctl line if your system is using less than 4 GB of RAM, and if you’re already using compressed RAM disks.

Type the above script into the terminal, then press Ctrl+S. This will save the script in your system. Finally, press Ctrl+X to exit.

Now that the script is made, it needs to be marked as executable. NOTE: This step is very important - if you miss it, RAM compression will not work. To mark the script as executable, run chmod +x /usr/local/bin/rampak.sh. The script will now be recognized as an executable program.

The next step is to make the systemd unit. Type nano /etc/systemd/system/rampak.service. Once nano is open, type in the following code:

[Unit]
Description=RAM Compression
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/bin/rampak.sh
[Install]
WantedBy=multi-user.target

Press Ctrl+S, then Ctrl+X to save the file and close nano.

Now we need to enable the systemd unit we just made. To do that, run systemctl enable rampak.service. Finally, reboot and see if it’s working!

Once your system has booted up, open htop to see if you have more swap space than before. If so, congratulations! You have finished setting up RAM compression!

The quick and permanent way

If you decide you’d like RAM compression on your system at all times, but you don’t want to trudge through the steps above, here’s a script you can use to automatically set the whole thing up for you::

#! /bin/bash
sudo tee /usr/local/bin/rampak.sh << EOF > /dev/null
#! /bin/bash
modprobe zram
sleep 1
zramctl --find --size=3G
mkswap /dev/zram0
swapon /dev/zram0
EOF
sudo chmod +x /usr/local/bin/rampak.sh
sudo tee /etc/systemd/system/rampak.service << EOF > /dev/null
[Unit]
Description=RAM Compression
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/bin/rampak.sh
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl enable rampak.service

Note that this script assumes that neither /usr/local/bin/rampak.sh or /etc/systemd/system/rampak.service exist already, so if for some reason these files already exist on your system, this probably won’t go as planned. You can test if these files already exist on your system by opening a terminal with Ctrl+Alt+T, and typing ls /usr/local/bin/rampak.sh; ls /etc/systemd/system/rampak.service in a terminal. If you get two “No such file or directory” errors when you do this, the files don’t exist and you’re good to go!

Also, this script is designed for systems with 4 GB of RAM - adjust the zramctl line as necessary for your system.

Copy this script into a text file, and save it as “rampakinst.sh”. Right-click on the script, click on “Properties”, and select the “Permissions” tab. Check the “Make this file executable” box, and click OK. Finally, double-click on the script, and click “Execute in terminal” in the pop-up window. You will be asked for your password once. Once the script finishes, reboot, and you’re done! Make sure to use htop to check and make sure everything went as planned.

Final notes

It might be safe to make a ZRAM device larger than your system’s physical RAM, but I don’t know for sure. My own testing was inconclusive. Personally, I’d err on the side of caution and leave that 1 GB uncompressed breathing room to make sure that nothing goes weird, but if you like living dangerously or know better than I do, you might be able to squeeze even more power out of your RAM with this trick.

If you’re already using ZRAM devices for some purpose, you may need to change these scripts or your own scripts or config files so that your RAM compression and other ZRAM use don’t conflict. If you’re in this scenario, you probably already know what to tweak to make everything work for you.

Conclusion

RAM compression is a powerful technique for squeezing more power out of lightweight systems and getting the most out of powerful systems, and it’s quite easy to set up. If you’re using a system with less than 8 GB of RAM, and aren’t using a swapfile, RAM compression may solve your low-RAM woes.

I hope this was helpful! Have a nice day.

10 Likes