Out of swap space

Hi,
is there a way to increase the swap space? I keep running into swap usages of 100% and can’t figure out what to do. The system runs fine, but occasionally, I get system lock-ups and the SD card light on the Pi is flashing like mad. Seems like an out of space situation.
Thanks!

How big is your card?

It’s a 32Gb Sandisk SD card. And disk usage is below 20%.

Ok, that’s plenty big enough. What’s your RAM use like?

My RAM use is around 75%. I’m running the UniFi Controller so that consumes a large chunk of the RAM.

Yeah that’s your problem. You might want to move that to another pi or better yet get a Chinese mini pc to run hassio and unifi.

In answer to your question, you might be able to increase the swap size by connecting a keyboard and display to the pi and issuing the commands there. Hassio is rather locked down though. The commands required may not be included.

I just realized that swap is increasing by itself day by day and Hassio restarts makes it worse. I did the host reboot (Hass.io>Host system>Reboot) and after it swap went from 89% to 29%.
I think that there is something to do about it in the next Hassio update.
Now as a temporary solution I have my automation to reboot the host when swap usage is more than 80%.

I have the same issue on a Intel NUC also running hassio with unifi controller and some other addons. I assumed it was normal and just reboot the machine every few weeks, but would be happy to know if there is a fix to avoid this.

What components are you using?
Are you using the embedded MQTT broker? I racall there being a problem with memory leak and causing this problem.

I do not use MQTT myself, but the solution was to NOT use the embedded broker.

1 Like

I will try to follow advices from this article: https://www.tecmint.com/clear-ram-memory-cache-buffer-and-swap-space-on-linux/

The command to clear swap space is following: “swapoff -a && swapon -a”.

If I understand it correctly I will need to write a script to run with an automation when a swap usage is i.e. above 80%.

1 Like

Unfortunately “swap off -a” command is not permitted.
So, now as I can see only reboot of a host is solution to clear a swap.
My automation is following (in automations.yaml):

  • id: ‘xxxxxxxxxxxxxx’
    alias: SWAP reboot
    description: ‘’
    trigger:
    • above: ‘80’
      entity_id: sensor.coreos_swap_used_percent
      platform: numeric_state
      condition: []
      action:
    • data:
      message: SWAP more than 80%
      service: notify.ios_iphone_jm
    • service: hassio.host_reboot

To run this automation you have to have Glances running to use numeric state from entity_id: sensor.coreos_swap_used.

As you can read here https://www.home-assistant.io/docs/mqtt/broker/
There is a problem with the embedded broker.

There is an issue with the HBMQTT broker and the WebSocket connection that is causing a memory leak. If you experience this issue, consider using another broker like Mosquitto.

Just posting my version of this, slightly modified and also hopefully forum formatted for ease of copying and pasting!

Quick one: if I’m using

mqtt:
  broker: core-mosquitto
  discovery: true
  discovery_prefix: homeassistant 

as my MQTT settings, does that mean I’m running the HBMQTT (embedded) or RabbitMQ broker? I’m confused, as there seem to be a lot of potential built in brokers. I THINK I’m using the Eclipse Mosquitto broker Hass addon. Just trying to rule out the source of the memory leak. I AM running the Unifi addon…

  - alias: 'SWAP usage'
    trigger:
     - platform: time
       at: '03:00:00'
    condition:
      - condition: numeric_state
        entity_id: sensor.swap_use_percent
        above: 95
    action:
      - service: notify.telegram
        data:
          message: swap usage is over 95%, rebooting
      - service: hassio.host_reboot

Did you manage this?

I had problem with SWAP since few versiond of HA. For sure it was Google Backup Add-on. It lead me to restart HA by NodeRed every night as SWAP rach 80% during 24h. Then came Unifi controller I hosted on RPi. Thinks gets even worse.
Fortunatelly stretching SWAP to 2GB does the work. In standard it has 100MB. Now Swap is all the time at 15-25%

How did you achieve increasing your swap size to 2GB please? I’m currently running Hassio on a RPi, but have migrated it over to an SSD drive.

I also have the problem where the swap is sitting at 100%. I’ve only recently started monitoring this, so I’ve no idea whether it has anything to do with my recent additions of the InfulxDB + Grafana add-ons.

Drops to 75% after a server restart, then steadily rises back to 100%.
Drops to 8% when I restart the host system.

note: I have added includes to the InfluxDB, limiting it to only around 20 entities.

1 Like

here ist my workaround :slight_smile:

mac

I was in a similar situation: Possible memory leak when there are 20+ devices connected · Issue #475 · danielwelch/hassio-zigbee2mqtt (github.com)

I changed from boot to GUI to boot to CLI to save some RAM. I might also increase the SWAP in case the RAM surges, but for now that saved the day. You could also try that.

@ffm777 if you’re running HA OS then this may help you: