How to change zram size?

Solution 1:

According to http://manpages.ubuntu.com/manpages/xenial/man8/zramctl.8.html, you can remove and recreate a zram swap like this:

# swapoff /dev/zram0
# zramctl --reset /dev/zram0
# zramctl --find --size 1024M
/dev/zram0
# mkswap /dev/zram0
# swapon /dev/zram0

To permanently change the size, you have to adapt the init script, where the swap files are created. Be aware, that this file may be overridden by future system updates.

To increase the size of the swapfile in Ubunutu 16.04 from 50% to 200% of your total memory size, change this line in /usr/bin/init-zram-swapping

mem=$(((totalmem / 2 / ${NRDEVICES}) * 1024))

to

mem=$(((totalmem * 2 / ${NRDEVICES}) * 1024))

Solution 2:

I'm a Fedora 33 user, but I came across this answer when looking for how to configure my zram. This is my /usr/lib/systemd/zram-generator.conf:

# This config file enables a /dev/zram0 device with the default settings:
# — size — half of available RAM or 4GB, whichever is less
# — compression — most likely lzo-rle
#
# To disable, uninstall zram-generator-defaults or create empty
# /etc/systemd/zram-generator.conf file.
[zram0]
zram-fraction = 2
max-zram-size=none

This is persistent across reboots and seems to be the recommended way of controlling zram.

See https://fedoraproject.org/wiki/Changes/SwapOnZRAM, espeically man 5 zram-generator.conf

Solution 3:

You need to change to "root" to do this.

sudo -i
# echo 128M > /sys/block/zram0/disksize

Solution 4:

There is no file at /usr/bin/init-zram-swapping. It seems since Xenial the file is now located at /sbin/zram-config-start. You can see this by yourself looking at $ cat /etc/init/zram-config.conf.

description "Initializes zram swaping and /tmp"
author      "Adam Conrad <[email protected]>"

start on runlevel [2345]

pre-start exec /sbin/zram-config-start

pre-stop exec /sbin/zram-config-stop

The file /sbin/zram-config-start is much more complex than before. I wonder what to do in order to increase the ram size?

Solution 5:

As an old-school technocrat, I prefer doing things at as low a level as possible. I therefore created a small script to enable or change the size of my zram swap file.

#!/bin/bash
# Based on https://github.com/ric96/zram
# ver: 30 dec 2018
# Installed in /usr/local/bin/my-zram.bash
#  And made executable " chmod a+x /usr/local/bin/my-zram.bash "
# I use an /etc/crontab entry of:
#  " @reboot root /usr/local/bin/my-zram.bash 256M ; # [ optionally use w/ "'size'M" for Mega ]
# Or for command-line: " sudo /usr/local/bin/my-zram.bash 256M "
# Note that you may want to change the '256M' parameter I am using.

logger -- $0 $$ zram $1 Start

## If $1 exists
test -n $1 && \
 {
  ZRAMSIZE=$1
  DOLONE=$1
 }
## And yes, invalid option input will create interesting, but apparently not hazardous results.

## If no $1, fallback and calculate a reasonable (to me) size.
test -z $1 && \
 {
  totalmem=`free | grep -e "^Mem:" | awk '{print $2}'`
  mem=$(( ($totalmem) * 1024 / 2 ))
  #echo $mem > /sys/block/zram0/disksize
  ZRAMSIZE=$mem
  DOLONE=NULL
  logger -- $0 $$ Using totalmem $totalmem \* 1024 /2 = $mem ZRAMSIZE=$ZRAMSIZE
 }

## Do we already have a /dev/zram0 ?? if so, swapoff and reset it.
test -b /dev/zram0 && swapoff -v /dev/zram0
test -b /dev/zram0 && echo 1 > /sys/block/zram0/reset

## If /dev/zram0 does NOT exist, but the 'zram' kernel module IS loaded, then remove it.
test -b /dev/zram0 || ( lsmod|grep -q zram && rmmod -v zram )

## (Re)Install the 'zram' kernel module. FYI: It doesn't hurt to 'reinstall'...
modprobe -v zram num_devices=1

## Build the zram swapfile.
echo $ZRAMSIZE > /sys/block/zram0/disksize
mkswap /dev/zram0
swapon -v -p 5 /dev/zram0

logger -- $0 $$ zram Done.  ZRAMSIZE=$ZRAMSIZE  \$1=$DOLONE

# The_End

Enjoy! It works for me. And you may want to review, understand, and edit to suit your situation. :-)

And be aware that if you run this script from cron, as written, it produces output which will (should) be mailed to you ( or root ).