How to change swapfile size on LibreELEC?

  • I have follow this post to set swap file on my raspberry pi zero and work

    Iridium
    April 14, 2019 at 6:09 PM

    however, I have not changed the SWAPFILESIZE="128" at first, then I closed the swap and reboot and then modified SWAPFILESIZE="other" and reboot

    the swap file recreated still is 128MB, and I follow this guide(https://linuxhint.com/change_swap_size_ubuntu/) try to modify, but it not work:

    swapon -s

    swapoff -a

    dd if=/dev/zero of=/swapfile bs=1M count=256 (can not use dd, say swapfile readonly)

    so, how to change swapfile size?

  • SWAPFILESIZE value is a number. If you put "other", you would just make swap creation command to error out.

    If you need more memory on your box, get more memory. Don't treat swap as "temporally" memory that you will be using permanently by ignoring performance hit that you get there.

    Instructions that you linked tell you where that swap file is. Technically they are probably for older LibreElec as current swapfile location is configurable. Remember that LibreElec is not regular Linux, you can't use Ubuntu instructions on LibreElec without understanding what they do and you can't put random stuff in its root file system.

    If 256MB swap is improvement over 128MB, remember how much memory your cell phone has and find some dimes in your couch for real memory.

  • Hello.

    I have a problem creating a swap file on an external drive, on a similar board, OrangePi Lite with 512MB of RAM.

    Following the guide I tried all ways to replace the swap file location by editing /storage/.config/swap.conf without success.

    I don't know how to change the SWAPFILE="$HOME/.cache/swapfile" entry so that it creates the file on a USB drive connected to the board.

    If I try for example "SWAPFILE="$HOME/var/media/DISK_LABEL/swapfile", which is the location where the USB drive is mounted from LE, actually the swapfile is created in a new folder on the SD card and not on the external drive.

    Can anyone help me find where I'm wrong?

  • You describe situation with USB media not mounted when swap file is created.

    Disk IO performance is about 100 times slower than memory performance. You would not want to have any memory read or write operations in file based swap.

    Please note that you are not running out of memory if free column in Linux free command has very small number. Your actual free memory is in last 'available' column. You will be running out of memory only when Linux starts killing running processes.

    Original poster was almost right about the way to activate swap manually. They only missed mkswap command and did not realize that swapon can activate individual swaps.

    1. dd to create file in writable location. Preferably not on SD card or SSD with limited write counters.

    dd if=/dev/zero of=/var/media/DISK_LABEL/swapfile bs=1M count=256

    2. format that file with mkswap

    mkswap /var/media/DISK_LABEL/swapfile

    3. swapon that file

    swapon /var/media/DISK_LABEL/swapfile

    those are main steps from LibreElec code responsible for stuff in swap.conf. Other steps only ensure that parent directory exists and secure swap file permissions.

    Edited 5 times, last by tokul (May 24, 2022 at 5:56 PM).

  • Hi tokul, thanks for your help.

    I imagined the problem was that when the swap file is created at startup the drive is not yet mounted and is mounted later.

    I want to try to create an external swap because randomly the system freezes and reboots by itself, the probable symptom is that the RAM memory is full.

    Trying the free command I see that about 100MB is left available and I think this is causing the random reboots.

    I will try the method you recommended as soon as possible, but I did not understand only one thing.

    Do I still have to create the /storage/.config/swap.conf file or not?

    If so, what should I enter in it?

    Thanks a lot!

  • swap.conf is for making persistent configuration change which survives reboot.

    If you continue to use swap.conf, you would have to select swap file location which is accessible at the time when script is executed or change swap.service priorities. I am not the fan of debugging systemd and not the fan of using swap files, if it is not obvious at this point.

    Edited once, last by tokul (May 25, 2022 at 5:21 AM).

  • I want to try to create an external swap because randomly the system freezes and reboots by itself, the probable symptom is that the RAM memory is full. Trying the free command I see that about 100MB is left available and I think this is causing the random reboots

    If the system randomly freezes something like power or disk corruption or an actual code bug are more likely causes. The kernel will actively manage RAM use and if there is ever an Out Of Memory (OOM) scenario the kernel oom-killer function will proactively kill idl/active processes to free space. If that happens it's fairly obvious from logs.

    Users are forever seeking some magic cure for "my cheap board doesn't have enough RAM" and the ONLY solution to that problem is buying a less cheap board with more actual RAM. Swap and Zram and such can fake more; but at the cost of reduced I/O performance and high wear on the SD cards that most LE systems run from and/or reduced CPU performance (due to compression) on an already low-end device.

    NB: RPi Zero was on the edge of acceptable performance with the older RPi codebase (LE 9.x) which uses highly optimised media drivers. The newer standards-based codebase (LE10/LE11) is in our opinion too heavy for acceptable performance on 512MB devices which is why we have formally dropped support for all boards/devices with 512MB.

  • Hi guys, thank you again for all the information you have given me.

    I would like to say, if anyone needed it, that with the indications of tokul I was able perfectly to make work the swap on USB external disk on OrangePi Lite 512MB.

    Thanks chewitt, I had read your warnings about swap and zram before I even asked for help, and I want to confirm that unfortunately it is not a miracle system and it changes things very little.

    But above all I wanted to try to learn something new, whether it solved my problem or not.

    For LE work properly, 512MB of ram is too little, know this.

    I want to conclude by saying just one thing to chewitt.

    Since there is also the OrangePi Lite 1GB model can I ask you to continue the support for this board model too?

    I will try one of this model in the next few days, using the image compiled and attached by Deoptim, and I will also try to increase the ram memory of my OPi 512MB following the advice of piernov at the link http://www.youtube.com/watch?v=ux2U4yvx_Po.

    I was stuck at OpenElec on my board 512MB, but I must say that the image created by Deoptim seems to work well!

    Thanks again to everyone for your help, good continuation on the forum and with LE.

  • The decision about what to support (or not) belongs to individual maintainers, not me, although there is general agreement among the team to avoid boards with less than 1GB RAM for LE10 and above to avoid a certain category of support issues.