How do I enable zram and disable the swap partition?
How do I use zram instead of a swap partition to gain some speed and hard disk?
How do I configure zram to my needs?
Why do these things?
How do I enable zram and disable the swap partition?
How do I use zram instead of a swap partition to gain some speed and hard disk?
How do I configure zram to my needs?
Why do these things?
This is a guide to enable zram. Edit it the way you like it, and remove the swap partition to make Ubuntu faster.
You actually don't need to create a swap partition as Ubuntu suggests on installation. You only need a swap partition in case an application needs more physical RAM than the available.
So we only need swap to use applications that need more physical RAM. Also we need swap when we want many active processes at the same time and the active RAM isn't enough. But swap is extremely slow; the hard disk is running 1 million times slower than RAM. What if we could swap in RAM? How? By compressing RAM. That's 1 million times faster than hard disk.
We sacrifice a very tiny amount of CPU load and a very tiny small amount of RAM to get more GB of RAM, but RAM is 1 million times faster than the hard disk, so trust me it's worth it (this is the easiest explanation for everyone to understand).
So:
Don't create a swap partition on installation and if you already have, just boot a live session from USB stick and remove the swap partition using GParted or any other partition manager and do what you like with the extra hard disk space you just got. Then reboot (that will take time as it tries to mount a partition that's not there. Don't be scared; it will boot or I wouldn't be here writing it). When you boot open terminal and do:
sudo nano /etc/fstab
delete the line referencing the swap partition from the file, save and reboot.
Now you boot again fast, even faster than before since you boot a partition less. So we just increased our disk size, and we gained boot time.
Let's now install zram and customise it to our needs.
Open the terminal again and do:
sudo apt-get install zram-config
Reboot
Open the terminal again and do:
sudo cat /etc/init/zram-config.conf
What does that file say? That zram is created on boot using the file
pre-start exec /usr/bin/init-zram-swapping
and that's exactly what we are going to edit:
sudo nano /usr/bin/init-zram-swapping
at line
totalmem=`LC_ALL=C free | grep -e "^Mem:" | sed -e 's/^Mem: *//' -e 's/ *.*//'`
mem=$(((totalmem / 2 / ${NRDEVICES}) * 1024))
change 1024 to whatever you like. I put 512, but it can be any number, anything! It doesn't need to be in byte form 128, 512, 1024, etc. It can be anything!
Then edit the line
swapon -p 5 /dev/zram${DEVNUMBER}
and change 5 to whatever priority you want. That's your swappiness.
Press Ctrl + O to save and Ctrl + X to exit.
Reboot.
Check your swap. Open the terminal and do
cat /proc/swaps
Isn't your pc faster or what?
I came up with this idea when I made an old Android phone run fast as hell with Android 7.1.1 when the official last update it got was years ago Android 4.0 (Ice Cream Sandwich), and I needed more RAM to run newer apps, but I didn't want to lose speed. Well, OK we lose some speed , but that's nothing compared to the speed we lose when swapping to a slow hard disk, or a hard disk generally. Those with newer machines will not notice big difference, but they DO get more speed, and older machines will be happy faster again.