Zram on Centos 7 / Nethserver 7


(Thorsten) #1


here is a tiny little howto on installing zram as an alternative to swap:

I run Nethserver as a guest on Proxmox VE. Nethserver installs a swap disk within the virtual disk provided. In my case, I do have quite a lot of processors and overall CPUs load is still low. Additionally, CPUs are not dedicated to a guest within Proxmox but a shared resource. In contrast to this, a minimum amount of RAM has to be dedicated to each Proxmox guest. I am quite limited with ram and Proxmox uses a lot of ram for ZFS.


> yum install bc

> nano /etc/init.d/zram
# Provides: zram
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Virtual Swap Compressed in RAM
# Description: Virtual Swap Compressed in RAM
start() {
    # get the number of CPUs
    num_cpus=$(grep -c processor /proc/cpuinfo)
    # if something goes wrong, assume we have 1
    [ "$num_cpus" != 0 ] || num_cpus=1
    # set decremented number of CPUs
    decr_num_cpus=$((num_cpus - 1))
    # get the amount of memory in the machine
    mem_total_kb=$(grep MemTotal /proc/meminfo | grep -E --only-matching '[[:digit:]]+')
    #we will only assign 50% of system memory to zram
    mem_total_kb=$((mem_total_kb / 2))
    mem_total=$((mem_total_kb * 1024))
    # load dependency modules
    modprobe zram num_devices=$num_cpus
    # initialize the devices
    for i in $(seq 0 $decr_num_cpus); do
    echo $((mem_total / num_cpus)) > /sys/block/zram$i/disksize
    # Creating swap filesystems
    for i in $(seq 0 $decr_num_cpus); do
    mkswap /dev/zram$i
    # Switch the swaps on
    for i in $(seq 0 $decr_num_cpus); do
    swapon -p 100 /dev/zram$i
stop() {
    for i in $(grep '^/dev/zram' /proc/swaps | awk '{ print $1 }'); do
        swapoff "$i"
    if grep -q "^zram " /proc/modules; then
        sleep 1
        rmmod zram
status() {
        ls /sys/block/zram* > /dev/null 2>&1 || exit 0
        echo -e "-------\nzram Compression Stats:\n-------"
        for i in /sys/block/zram*; do
            compr=$(< $i/compr_data_size)
        orig=$(< $i/orig_data_size)
        if [ $compr -gt 0 ]; then
            ratio=$(echo "scale=2; $orig*100/$compr" | bc -q)
        echo -e "/dev/${i/*\/}:\t$ratio% ($orig -> $compr)"
        echo -e "-------\nSWAP Stats:\n-------"
        swapon -s | grep zram
        echo -e "-------\nMemory Stats:\n-------"
        free -m -l -t
case "$1" in
        sleep 3
        echo "Usage: $0 {start|stop|restart|status}"

> chmod 755 /etc/init.d/zram

> chkconfig --add  zram  && chkconfig zram on

> service zram status

[1] oliviertoupin/zram on https://codegists.com/code/zram-centos-7/
[2] rasyiqul on https://rasyiqul.wordpress.com/2015/08/15/enable-zram-on-centos-6-x/

To Do:

  • Define priority against swap and/or turn of swap disk
  • Define reserverd memory blocks

(fpausp) #2

Hi thorsten, thanks for the howto. Please give us a config-example, to get an idea for which vm-size it coud be a benefit, to use zRAM or not…


(Thorsten) #3

Hi Frank,

I did not track SWAP size so far continously. However, to my basic knowledge, zram is fine for computers “with limited RAM resources”. To take advantage, I assume 2, maybe 4 GB, of ram, a slow HDD and / or large utilisation of Swap.

swap can be tested by

swapon -s

Generally my system is configured as follows:
Host: Dell Precision T3610 Workstation with overall 32 GB of RAM (average usage: 80 %), 12 cores (average usage < 1%), 3 x 2 TB WD Red (ZFS), 1 x 128 GB SanDisk SSD (32 GB as ZFS Cache, 32 GB as SWAP, rest unused by now)

Virtual machines:

  • CentOS Server, Nethserver: 8 GB (average usage: 50 %) / 4 cores (average usage 3 %)
  • Ubuntu Server, Dedicated to EcoDMS: 4 GB RAM (average usage: 46 %) / 4 cores (average usage: 0.5 %)
  • Ubuntu Server, Dedicated to Calibre: 2 GB RAM (average usage: 38 %), 4 cores (average usage: 0,2 %)
  • Some spare and trail machines not running continuously, e.g. Nethserver backup, Nethserver trail & error, FreeNAS, ecodms backup)

So far, I do notice a remarkably effect for the Ubuntu / Calibre System, while I installed zram on Nethserver just 1 h ago with standard settings.

I will have a look on machine data, but by now I just got only few results after reboot. I will also need to reconfigre, e.g. remove the swap disk itself…

(fpausp) #4

Thank you Thorsten, I will give it a go…

(André Wismer) #5


Most of my NethServers (Mine & Clients) run in ProxMox. During Setup, I provide only 2 GB RAM for the Guest (NethServer). This gives me a 2 GB Swapfile (Partition).After initial Install, I upgrade the RAM during a short shutdown to 4 or 8 GB RAM.
-> I only have a few clients where RAM is plentiful…
This works rather well…

My 2 cents

(Juan Carlos Fernandez) #6

Seen @thorsten script and rpodgorny zramswap script on AUR, I decided to make a python script which allows you to customize ZRAM parameters, allowing you to create a service that fit your needs.

First lets start with requirements, I decided to do the script using python 3, so first thing first:

  1. Install python3, pip, and virtualenv:
    yum install python34 python34-devel python34-virtualenv python34-pip

  2. Create a virtual enviroment, I decided to set it on /opt/zram:
    virtualenv-3 /opt/zram/env/

  3. Activate virtual enviroment and install sh and psutil using pip
    source /opt/zram/env/bin/activate
    pip install psutil sh

  4. Disable partition swap:
    swapoff -a
    Comment the swap line on /etc/fstab

  5. Create folder and script files:
    mkdir /opt/zram/src/
    Using an editor create zramctrl.py and procsmem.py inside /opt/zram/src/. Please notice that procsmem.py is mostly a copy from giampaolo and is used to know which process is been swapped. You can check if the script works by running this:
    /opt/zram/env/bin/python /opt/zram/src/zramctrl.py start -A lzo
    This will create a ZRAM swap using all CPU cores and 20% of total RAM.
    To see how to use the script to adapt configuration to your needs run:
    /opt/zram/env/bin/python /opt/zram/src/zramctrl.py -h

  6. Create, install, load, enable and start a service:
    Using an editor create zramswap.service inside /opt/zram/src/ and install the service by running:
    install -Dm644 opt/zram/src/zramswap.service /lib/systemd/system/zramswap.service
    Load the service by running:
    systemctl daemon-reload
    Enable the service by running:
    systemctl enable zramswap.service
    Start the service by running:
    systemctl start zramswap.service

That’s it, you now have a ZRAM swap online, here is some stuff you can do:
For those who use Proxmox ballooning, it’s advisable to set ZRAM to the minimum RAM assigned, to accomplish that, change line 8 from this:

ExecStart=/opt/zram/env/bin/python /opt/zram/src/zramctrl.py start -A lzo

To this:

ExecStart=/opt/zram/env/bin/python /opt/zram/src/zramctrl.py start -A lzo -P 40 -M 2147483648

Where 2147483648 = 2 *1024^3 = 2G, so this new setup would create a ZRAM of 819M (40% of 2G). Any question or suggestions are welcomed.

(Thorsten) #7

What a f***ing nice job. Pretty cool. Will try that soon.