1

Is there a way to get out more out of your limited Ram on a VM?

I have a VM running on a cloud hoster and try to optimize a quite low on RAM mashine.

I heard, there is a way to compress parts in the memory if all free memory is in use called zram

How do I get this running?

1

As explained on the Zram Wiki:

zram (previously called compcache) can create RAM based block devices. It is an experimental (staging) module of the Linux kernel since 3.2.

So If you are using a kernel before 3.2 you need to copy the following script (taken from here) to /etc/init.d/zram:

### BEGIN INIT INFO
# Provides:          zram
# Required-Start:    $local_fs
# Required-Stop:     $local_fs
# Default-Start:     S
# Default-Stop:      0 1 6
# Short-Description: Use compressed RAM as in-memory swap
# Description:       Use compressed RAM as in-memory swap
### END INIT INFO

# Author: Antonio Galea <antonio.galea@gmail.com>
# Thanks to Przemysław Tomczyk for suggesting swapoff parallelization

FRACTION=75

MEMORY=`perl -ne'/^MemTotal:\s+(\d+)/ && print $1*1024;' < /proc/meminfo`
CPUS=`grep -c processor /proc/cpuinfo`
SIZE=$(( MEMORY * FRACTION / 100 / CPUS ))

case "$1" in
  "start")
    param=`modinfo zram|grep num_devices|cut -f2 -d:|tr -d ' '`
    modprobe zram $param=$CPUS
    for n in `seq $CPUS`; do
      i=$((n - 1))
      echo $SIZE > /sys/block/zram$i/disksize
      mkswap /dev/zram$i
      swapon /dev/zram$i -p 10
    done
    ;;
  "stop")
    for n in `seq $CPUS`; do
      i=$((n - 1))
      swapoff /dev/zram$i && echo "disabled disk $n of $CPUS" &
    done
    wait
    sleep .5
    modprobe -r zram
    ;;
  *)
    echo "Usage: `basename $0` (start | stop)"
    exit 1
    ;;
esac

give it executable rights with

chmod +x /etc/init.d/zram

then instruct you system to start it at boot time, with the command

insserv zram

After the next reboot you will see the swap with

swapon -s

which will look like:

Filename                Type        Size    Used    Priority
/dev/zram0                              partition   381668  380716  10
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.