Upgrade swap file

Not sure if this is the right spot to post this.

I found that my 2GB RPI running NEON often gets awfully close to the 1GB limit of swap (virtual memory)
So I upgraded my NEON image to a 2GB swapfile

I wrote this down as an example how others could do this as well.
I am doing that using ssh to have a CLI (command line interpreter) on the RPI it self

***UPDATE ***
It turns out ZRAM is a much more ellegant/efficient solution than the brute force method of adding more virtual memory/SWAP. See next 2 postings in this thread and try that instead of the following.

#to upgrade mycroft mark2 swap from 1GB -> 2GB
 
#use minimum amount of ram by temporary stopping neon
sudo systemctl stop neon
 
#turn off old swapfile
sudo swapoff -a
 
#move it out of the way
sudo mv /swapfile /swapfile.old
 
#create new 2G swapfile
sudo fallocate -l 2G /swapfile
 
#set permissions right
sudo chmod 600 /swapfile
 
#create swapfile content/signature
sudo mkswap /swapfile
 
#turn it on using fstab reference
sudo swapon -a
 
#check
free -h
 
#start neon
sudo systemctl start neon
 
#cleanup
sudo rm /swapfile.old

My unit has not run out of memory yet
Currently running the latest alpha:

up 3 days, 19:15
(venv) neon@neon:/$ free -h
               total        used        free      shared  buff/cache   available
Mem:           1.8Gi       1.6Gi        53Mi       113Mi       315Mi       205Mi
Swap:          2.0Gi       410Mi       1.6Gi

You could check as well ZRAM.

Raspberry Pi Performance: Add ZRAM and these Kernel Parameters.

2 Likes

installed, now running zram. was time to reboot since 23.6.21alpha10 was released :sunglasses:

In my enlarged conventional swapfile setup, about 460Mb swap was being used.
With Zram I only see 150Mb being used. That must be the compression doing it’s magic.

Thanks for pointing this out.
Installing zram looks like a much more elegant solution !

For those who want to try this is as well , here by an overview of commands
You should cut&paste these lines to your ssh CLI interface

**UPDATED **
One command was giving an error

#switch to root mode, prompt should go from $ -> #
sudo -s


#install debian zram-tools package
apt install zram-tools

#standard is 256MB compressed swap, I prefer a bit more and would like to double that

echo "SIZE=512" >> /etc/default/zramswap


#add parameters to sysctl.conf file
cat <<EOF >> /etc/sysctl.conf

#zram
vm.vfs_cache_pressure=500
vm.swappiness=100
vm.dirty_background_ratio=1
vm.dirty_ratio=50
EOF

#activate systctl with new parameters
sysctl --system

#restart zram with new configuration parameters
service zramswap reload

#go back to normal neon user again
exit

The current setup prefers zram over conventional swap file.
Will update with my experience when I have used it for a while.

2 Likes

FWIW, the step to add parameters to the sysctl.conf file failed for me until I ran sudo su to become root. Also, I tried PERCENT=50 instead of SIZE=512 to have the zram-tools set that dynamically based on the amount of RAM. That’s 1GB instead of 512MB, so on small disks that may not be ideal, but worth trying!

Thanks for sharing this easy-to-follow guide! I’ll also post updates on my experience.

1 Like

Keep in mind that ZRAM could generate a little CPU overhead (because of the compression) compare to the swap file.

1 Like
(venv) neon@neon:~$ sudo zramctl 
NAME       ALGORITHM DISKSIZE  DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 zstd            1G 97.4M 20.9M 22.1M       4 [SWAP]
(venv) neon@neon:~$ sudo cat /proc/swaps 
Filename                                Type            Size            Used            Priority
/dev/zram0                              partition       1048572         99780           100

Works great so far!
Cpu idle: 68%

1 Like

venv) neon@neon:~$ sudo zramctl
NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 lz4 899.1M 4K 64B 4K 4 [SWAP]
(venv) neon@neon:~$ cat /proc/swaps
Filename Type Size Used Priority
/swapfile file 1048572 534500 -2
/dev/zram0 partition 920716 0 100

from GGGump Thanks this is what my system is showing just after following your installation instructions though I too followed mikes use of PERCENT=50 option.

Update on my side: love this! Makes my Mark 2s perform so much better!

1 Like