Linux: Less Ram? Want more? A guide to increasing your ram in Linux
Download more ram from here. Note: This article for linux based users only
Intro
I, a Cybersecurity enthusiast and an Android developer, often ended up with a hung-up computer most of the time while running multiple VMs for practicing penetration testing or, while using the legendary Android Studio.
After a lot of research, I have actually found ways to squeeze in more programs in less ram and even increase your ram using few techniques (Not gonna tell you to go and buy a new ram stick, I promise, or… do I?).
Minimizing the ram usage(cliche ones)
The DE
I have been fond of XFCE since I entered into Linux environments. It is a lightweight DE (like LXDE) that can be fully-featured DE(unlike LXDE), as well as have the benefit of not being resource-heavy (unlike GNOME). It is the best of both worlds with a base ram usage of <1GB. If you have really really less ram then you can also try LXDE but if you a reasonable ram like 4GB, XFCE will do.
Browser Extensions
Are you one of those who just keep opening new tabs for all their work and don’t close the rest and later end up with uncountable tabs?
The open tabs actually take up a lot of your ram. You can install a few extensions that will suspend the tabs and put them in a low memory state.
You can use “Auto tab discard” extension for Google Chrome, As well as for Firefox. I was a fan of The Great Suspender at first, but its recent maintainer change has caused a lot of trust issues with it. If you are interested in its story, you can read it here.
Also, disable your unused extensions. E.g., If you use some extension for Youtube like Youtube Speed Control or some VPN, which you sometimes require but not often. You can disable it and enable it the next time it comes to use. This will help you decrease your ram usage a lot.
The un-cliche methods
Systemd-run
It may be used to create and start a transient .service or a .scope unit and run the specified COMMAND in it.
This helps me when I use Android Studio. Actually, Android Studio doesn’t respect user wishes of ram requirements. It is like a program with an infinite capacity for eating your ram (and I am not kidding). If any other application you feel takes more than the necessary ram, you can limit its usage with this.
With systemd-run, You can limit the ram or other resource of your computer at kernel level so that it is not visible to Android Studio or any other ram hungry application, which have NO SHAME FOR KEEPING RAM FOR OTHER APPLICATIONS.
systemd-run --scope -p MemoryLimit=1536M studio
Zram
For squeezing in more programs in ram, We will be using the swap partitions and take out a block of ram for using in the swap(wait, what?)
What is zram?
zram, formerly called compcache, is a Linux kernel module for creating a compressed block device in RAM, in other words a RAM disk, but with on-the-fly disk compression. The block device created with zram can then be used for swap or as general-purpose RAM disk.
Zram actually uses a part of your ram and changes it to swap (1+1=2?). No, Here, the part which goes to swap is actually compressed by utilizing your CPU processing power. So basically, we have 1+compressed(x)=2.
Now how much you can squeeze in x depends on the compression algorithms available.
In Debian systems, you can use zram-tools and tweak /etc/default/zramswap for optimal performance based on your computer.
For Arch users(Why are you here anyway?), You can use systemd-swap. This will help you in the configuration of zswap also(next section).
One counterintuitive thing that you’ll need in this case is setting these configurations in /etc/sysctl.conf.
vm.swappiness=100
By setting swappiness as 100, you are actually delaying the inevitable and starting to use zram as soon as required rather than delaying it to the point when your ram usage is full
Zswap
Zswap is a kernel feature that provides a compressed RAM cache for swap pages. Pages which would otherwise be swapped out to disk are instead compressed and stored into a memory pool in RAM. Once the pool is full or the RAM is exhausted, the least recently used (LRU) page is decompressed and written to disk, as if it had not been intercepted. After the page has been decompressed into the swap cache, the compressed version in the pool can be freed.
The difference compared to zram is that zswap works in conjunction with a swap device while zram is a swap device in RAM that does not require a backing swap device. (Did not understand? Nevermind)
People mostly confuse between zram and zswap. Zswap basically tries to decrease write operations in swap devices so that the disc drives stay healthy in the long run when swaps are being used.
We can use zram and zswap together; zswap will help you when your zram is exhausted, and your disc swaps are in action. It can also help you with zram as the pages which aren’t being used will be dumped in the zram device, causing fewer performance issues due to compression and decompression.
Note that, these will come at the expense of your cpu power. Though in my computer(i5 8th Gen CPU), there weren’t any noticiable effects
For Debian systems, you will need to add a kernel parameter zswap.enabled=1 in /etc/default/grub.
For Arch users(again, why were you here?), You need to edit the configuration file of systemd-swap for enabling zram as well as zswap.
vramfs
Unused RAM is wasted RAM, so why not put some of that VRAM in your graphics card to work?
vramfs is a utility that uses the FUSE library to create a file system in VRAM. The idea is pretty much the same as a ramdisk, except that it uses the video RAM of a discrete graphics card to store files. It is not intented for serious use, but it does actually work fairly well, especially since consumer GPUs with 4GB or more VRAM are now available.
If you have configured your graphics card to work on your Linux machine, and your graphics device has OpenCL capabilities, You can actually utilize your graphics ram and convert it to RAM.
On my Linux machine with Nvidia MX150, I could actually get a decent write speed of 650MB/s with graphics card and 1GB/s in my DDR4 ram when writing a 1GB file. Hell yeah! Ram is really fast! But vram ain't that bad. Vram’s were also better than SSD write speed(Around 300MB/s) in my case.
For Debian users, You really don’t have any pre-built solution. You will have to build this from source using https://github.com/Overv/vramfs.
Arch users have an AUR package named vramfs-git, which will install vramfs in your machine.
After installing vramfs, you can see ArchWiki for the next steps (Even Debian users.)
Configure your swap priority of vramfs more than your Disk Drives otherwise it won’t be of much help
nohang
nohang
package provides a highly configurable daemon for Linux which is able to correctly prevent out of memory (OOM) and keep system responsiveness in low memory conditions.
This is just a precautionary measure that will help you when your computer hangs.
The OOM killer of Linux ain’t very efficient by default. It won’t kick in if you have a swap device, and the swap device ain’t full.
With this, you can configure and fine-tune it with your current zram and vramfs settings to reach an optimal point when your computer actually starts to write in real swap space(HDD or SSD) and invoke the OOM killer at that point to prevent hangs in your computer.
For Debian users, This should be available in the apt repository. If not, You can follow the steps here.
For Arch users, there is an AUR package named nohang-git which you can install and then configure here.
Adios
I hope with all these methods; you can use squeeze more programs in your Linux machine. You will have to tweak all the programs manually to find the optimal configuration for your computer, But I hope you won’t need to download any more ram after this post.
If the above methods don’t suffice, you still have a way out of this btw…
BUY MORE RAM!!