Linux Swap file in RAM
Many of us use Linux machines as our primary workstations. I personally use Linux Mint, and this post reflects my current installation.
I opted for the default installation that came out of the box. I’m currently running Linux Mint Cinnamon 21. While I have removed some pre-installed packages, such as LibreOffice (since I use the .AppImage file instead), I haven’t made any other significant changes.
After the installation, as expected, I had a swap disk. This is a disk that is used when there is a need to swap data from memory to disk when the available memory is insufficient.
Swap Disk
To improve performance, I decided to switch to a RAM-based swap disk.
You might wonder why. The swap disk comes into play when the system’s memory is full, and an operation requires more RAM. In such cases, the operating system moves data from memory to the swap disk and retrieves it when needed. However, creating a swap disk in RAM may not be the most efficient approach since it essentially means moving data from one part of the RAM to another when the system requires more memory.
This is where zRAM
comes in.
zRAM is a Linux module that allows you to create a swap disk in memory, and the data stored in it is already compressed. Depending on the compression algorithm used, you can achieve compression ratios ranging from 1:2 to 1:3, or even higher. Since the swap disk is in memory, it’s incredibly fast, and the compression operation has minimal impact on performance.
zRAM is particularly beneficial when your machine has limited memory. Constantly swapping data on a disk-based swap device can significantly slow down operations. However, with zRAM
, you can achieve the same result but much faster, given that the data is compressed.
There are various data compression algorithms available, such as lz4
, zlib
, zstd
, and more. For my implementation, I chose zstd
.
Current Swap Disk
First, we need to identify our current swap disk:
cat /proc/swaps
This command will produce output similar to this:
$ cat /proc/swaps
Filename Type Size Used Priority
/dev/mapper/vgmint-swap_1 partition 5000000 0 -2
We need to disable this device first. To do so, we should edit the /etc/fstab
file:
sudo nano /etc/fstab
Then, comment out the line that sets up the swap disk:
#/dev/mapper/vgmint-swap_1 none swap sw 0 0
Now, you can either reboot your system or turn off the swap disk:
sudo swapoff /dev/mapper/vgmint-swap_1
Installing zRAM
To install zRAM, follow these steps:
sudo apt install zram-config
A reboot is required for the changes to take effect.
Once your system restarts, check the status of the swap disk:
cat /proc/swaps
The output should be something like this:
Filename Type Size Used Priority
/dev/zram0 partition 32886300 0 5
By default, zRAM
will allocate half of your memory for the swap disk. In my case, it picked up 32GB.
Fine Tuning
If you’re not satisfied with zRAM using half of your RAM or want to change the compression algorithm, here’s how to do it:
To determine the compression algorithm in use, issue this command:
cat /sys/block/zram0/comp_algorithm
This will display something like this (with the enabled algorithm in brackets):
lzo [lzo-rle] lz4 lz4hc 842 zstd
The configuration options are stored in the /usr/bin/init-zram-swapping
file. The file contents are similar to this:
$ sudo nano /usr/bin/init-zram-swapping
#!/bin/sh
modprobe zram
# Calculate memory to use for zram (1/2 of ram)
totalmem=`LC_ALL=C free | grep -e "^Mem:" | sed -e 's/^Mem: *//' -e 's/ *.*//'`
mem=$((totalmem / 2 * 1024))
# initialize the devices
echo $mem > /sys/block/zram0/disksize
mkswap /dev/zram0
swapon -p 5 /dev/zram0
To adjust the disk size, modify this line:
mem=$((totalmem / 2 * 1024))
To change the compression algorithm, replace this:
mem=$((totalmem / 2 * 1024))
with this:
mem=$((totalmem / 2 * 1024))
echo zstd > /sys/block/zram0/comp_algorithm
Reboot the system.
After the system restarts, your new swap disk will be a zRAM one, and it will use the compression mechanism you’ve selected. In my case, it looked like this:
$ cat /sys/block/zram0/comp_algorithm
lzo lzo-rle lz4 lz4hc 842 [zstd]
Enjoy!
-
Nikolaos Dimopoulos
Boldly goes where no other coder has gone before.... and other ramblings
Recent Posts
-
Setting up Docker for Qubes OS
2024-10-05 -
PhpStorm cannot create scratch files
2023-12-07 -
PHP 8.2 Deprecation of Dynamic Properties
2023-07-18 -
New Look
2023-06-12 -
Linux Swap file in RAM
2023-04-17
Tag Cloud
-
amazon (3)
android (1)
angularjs (7)
apps (1)
aurora (1)
aws (1)
backup (2)
bash (1)
bitbucket (1)
blog (2)
books (1)
bootstrap (1)
buzz (1)
cPanel (1)
cache (1)
celebrations (4)
chromium (3)
chromium os (3)
cloud computing (3)
codacy (1)
codecov (1)
communications (1)
composer (1)
conversion (1)
copy (1)
degoogle (5)
design (1)
design patterns (3)
discord (1)
docker (1)
docs (3)
documentation (1)
ec2 (3)
emerge (1)
encoding (1)
factory (1)
froyo (1)
fujitsu (1)
gentoo (7)
git (3)
github (2)
gmail (3)
google (16)
google apps (4)
google maps (1)
gource (1)
ha (1)
hosting (2)
how to (36)
igbinary (1)
information (5)
input (1)
installation (6)
internet (1)
iphone (1)
json (2)
libreoffice (1)
linux (13)
localization (1)
lts (1)
mariadb (1)
memorial day (1)
metrics (1)
migration (1)
mod_rewrite (1)
mov (1)
mp4 (1)
mysql (6)
nas (1)
netlify (1)
new look (1)
nexus one (2)
nfs (1)
notebook (1)
online storage (1)
openoffice (1)
opinion (1)
oracle (1)
patterns (1)
payroll (1)
performance (3)
personal (9)
phalcon (12)
php (23)
php8 (2)
php82 (1)
phpstorm (1)
phpunit (2)
picasa (2)
portage (1)
privacy (1)
programming (9)
proxy (1)
qubes os (1)
rant (5)
rdbms (1)
rds (1)
relationships (1)
release (1)
remove (1)
replication (1)
review (9)
rsync (2)
s1300 (1)
scan (1)
scratch (1)
serialize (1)
series (9)
singleton (1)
sorting (1)
spaceship (1)
spam (1)
ssl (1)
static (1)
storage (6)
submodules (1)
subversion (2)
svn (1)
swap (1)
tdd (1)
technorati (1)
test driven development (1)
testability (1)
testing (2)
titles (1)
traits (1)
ua (1)
ubuntu (1)
update (6)
upgrade (1)
usa (2)
usort (1)
utf8 (1)
video (1)
visualization (1)
vps (1)
webm (1)
website (1)
wget (1)
zend framework (4)
zram (1)
zstd (1)