How to increase the swap file size on Home Assistant OS

Hi, no. I only restarted the terminal.
As the swap file was already there, the terminal started without the init command and everything was fine.

My question is, what will happen with this swap file in case of a ha host restart?
Will the swap file be dropped and new initiated with basic size or will the swap file be kept as it is.
In the last case, I can empty the init_command and only get it back, when I have to create a new file used (new installation).
Obviously I havent understood the command completely :thinking:

I think my point was no clear… take a host reboot and let me know if all is okay or if you still get the error.

If the swap file is there it will not create it but it will try to “use” it. In your case it may already be used. When in doubt just take a host reboot and see if the problem is still there.

OK, thank you

Wow! My little RPi3B can breathe again while I wait for my HA Blue to arrive. Thank you!

Hello,

I have 240GB SSD drive can I set 2G to something bigger like 4G or 8G? Will this help? I am getting out of memory errors.

You definitely can. However it would be good to see the actual error along with screenshot of Glances or htop command. Either of those will highlight which memory pressures you’re dealing with.

Thanks for response @JZhass,
I received out of memory error 2 times yesterday. After that i followed your instructions and have not gotten that error again. But I can see memory usage is always more than 70- 80%.

htop output is attached.

Glances screenshoot.

Here is another output after 10-15 minutes of earlier screenshot.

Disable VSCode and InfluxDB and see how that goes (obviously disable writing from HA to InfluxDB as well)… The fact that your homeassistant is increasing memory suggests that one of the components you use (that could be stock and comes with HA) may have a memory leak.

@JZhass Thanks again for your response.
I have disabled VSCode and will try to disable InfluxDB for some time. But the out of memory errors i got yesterday was before installing influxDB. That suggests that issue is somewhere else.

Is there any other way to diagnose memory leak?

Only by disabling a bunch integrations and re-enabling in a controlled manner.

Thank you! I have been testing a couple of dedicated thin clients for haos+Frigate and they would eventually crash with memory errors. A quick search brought me here, and so far so good - it’s been running for a couple of days with everything running perfectly.

Thanks for this. I am running a RasPi3 and I eventually hooked it up to a monitor to see why it was hanging, with no response from anything SSH, Samba etc. Just locked. The monitor revealed, that it was out of memory space, and killed the process Python3. I added your swap space command and BINGO. No more hanging.
So thanks so much for this. I think a lot of RasPi3 users could do with this.

Is this workaround still up2date or obsolete because new HA updates? I switched from rpi3 to 4, do I have to use this again?

Paste your screenshot of Glances for me to assess what swap space looks like.

I dont know but only htop works. My system (RPI4) is new (restored snapshot from rpi3).

Yeah I’d still do it… Notice how your Mem/RAM is almost at max. Once it is that’s when swap is used.

1 Like

I know this has been a while, first of all, thanks for that piece of code, my HA on a RPi3 went from lagging out every few hours to working absolutely fine.

Is it possible to integrate a swapoff /backup/_swap.swap in case it already exists in the logic? I had the plugin failing to launch even after a reboot. Quick removal of the code, starting the plugin, run the swapoff and then re-add the code and restart it fixed it. Is there a way to add to the logic to do a swapoff in case the device busy error appears (See Pirol62 output with the error he posted in this thread, that was the error i faced)

Try this and LMK if it works…

init_commands:
  - if [ ! -f /backup/_swap.swap ]; then fallocate -l 2G /backup/_swap.swap && mkswap /backup/_swap.swap && chmod 0600 /backup/_swap.swap && swapoff /backup/_swap.swap && swapon /backup/_swap.swap ; elif [[ ! $(dmesg|grep _swap.swap) = *swap\ on* ]] ; then swapoff /backup/_swap.swap && swapon /backup/_swap.swap ; fi

This saved my raspberry.
Thanks!
I have a Pi 4, 1gb Ram, and it got constantly slugish, especially with InfluxDB installed.
Changed to SSD; changed different USB Sata adapters, but still the same.

Then i found this! Now my raspberry runs with no problems, only logs take some minutes to load. InfluxDB is still not installed, will use it it some weeks without changing much for testing.

This should be mentioned in the Docs, e.g. here:

I have a Rpi4 1Gb ram model. I recently changed to 64 bit OS and you could see HA struggling to survive. Without this change I would have rolled back to 32 bit OS. This should definitively be part of the HA OS parameters.

1 Like