ZRAM is a Linux kernel module that can create compressed block devices in RAM to optimize memory usage by compressing data, especially for devices with limited RAM, which can improve system performance and support more applications. Install and configure ZRAM on Ubuntu for you to share.
Check the current RAM usage before installation:
free -h
You can find total memory, used memory, and available memory. This data is useful for later understanding how ZRAM improves system performance. Installing ZRAM just requires loading the ZRAM module in the Linux kernel and verifying:
sudo modprobe zram
lsmod | grep zram
If you see ZRAM in the output, the module has been loaded successfully.
To configure ZRAM after loading, you need to use the script:
sudo nano /etc/systemd/system/zram.service
Copy and paste the following configuration into the file:
[Unit]
Description=ZRAM Configuration
After=local-fs.target
[Service]
Type=oneshot
ExecStart=/usr/sbin/modprobe zram
ExecStart=/bin/sh -c 'echo 2G > /sys/block/zram0/disksize'
ExecStart=/bin/sh -c 'mkswap /dev/zram0'
ExecStart=/bin/sh -c 'swapon /dev/zram0'
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
Replace 2G in the disksize row with the ZRAM size you expect. You can adjust according to the RAM capacity of the system, and it is usually recommended to set it to 50%-100% of the system RAM. Enable the ZRAM service:
sudo systemctl daemon-reload
sudo systemctl enable zram.service
sudo systemctl start zram.service
Check ZRAM usage status after starting the service:
swapon --show
You can see the contents listed in /dev/zram0, indicating that ZRAM is in normal use and used as swap space. To see if the use of ZRAM affects memory usage, use the following command again:
free -h
It is obvious that the amount of available memory has increased because ZRAM allows your system to use compressed memory efficiently.
If the system is not performing as expected, you will also need to adjust the ZRAM size. Stop ZRAM service first:
sudo systemctl stop zram.service
To open the configuration file:
sudo nano /etc/systemd/system/zram.service
Change the value to disksize to the desired size, and then restart the ZRAM service.
sudo systemctl start zram.service
In summary, it can be seen that in the case of limited RAM, the use of ZRAM in Ubuntu system can significantly improve system performance.