Enable zram on Centos-6.x

This About zram from wikipedia

zram (also called zRAM and, initially, compcache) is a Linux kernel feature that provides a form of virtual memory compression. zram increases performance by avoiding paging to disk and using a compressed block device in RAM instead, inside which paging takes place until it is necessary to use the swap space on a hard disk drive. Since using RAM is an alternative way to provide swapping on RAM, zram allows Linux to make more use of RAM when swapping/paging is required, especially on older computers with less RAM installed.

Even when the cost of RAM is low, zram still offers advantages for low-end hardware devices such as embedded devices and netbooks. Such devices usually use flash-based storage that has limited lifespan due to its nature, which is also used to provide swap space. The reduction in swap usage as a result of using zram effectively reduces the amount of wear placed on such flash-based storage, resulting in prolonging its usable life. Also, using zram results in a significantly reduced I/O for Linux systems that require swapping.

zram was merged into the Linux kernel mainline in kernel version 3.14, released on 30 March 2014. As of Linux kernel version 3.15, released on 8 June 2014, zram supports LZ4 compression algorithm, while LZO remains as the default compression backend. Changes in kernel 3.15 also provide performance improvements, as well as the ability to switch the compression algorithm via sysfs.

Google uses zram in Chrome OS and it is also available as an option for Android 4.4 devices. Also, Lubuntu started using zram with version 13.10. As of December 2012, Ubuntu has considered enabling zram by default on computers with small amounts of installed RAM.

on the first install of your linux system don’t create swap space, because swap will generate by zram after installation

if you install with minimal package, please install bc as requirement of zram

# yum install bc

zram system script (fix)

create file with name zram /etc/init.d/ and paste the script like so :

# vim /etc/init.d/zram

# Provides: zram
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Increased Performance In Linux With zRam (Virtual Swap Compressed in RAM)
# Description: Adapted from systemd scripts at https://github.com/mystilleef/FedoraZram
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=$( $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}"


save changes and give execute mode to zram

# chmod 755 /etc/init.d/zram

make zram start at boot

# chkconfig --add  zram  && chkconfig zram on

now reboot your computer/server , to check zram is running use the folowing command

# service zram status

source :


2 thoughts on “Enable zram on Centos-6.x

  1. Since then the number of compression streams was changed to auto for all cases, do we still need to define one device per core? or can it be simplified for CentOS 7+?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.