HA RAM usage optimization

EDIT: I’ve finally identified the memory hog, see below.

Hello,
I run HA on Raspberry Pi 4B, 1GiB RAM in a Docker container under Raspberry Pi OS. It has been fine until recently (maybe 2023.2). At this moment, it works fine most of the time, but in some cases (e.g., docker pull), I exprience significant thrashing. According to htop, Home Assistant’s main process is the main memory hog (it uses something like 65 % of RAM, sometimes even over 70 % of RAM). Zigbee2mqtt is the second, with something like 5 % or 6 % of the total RAM.

My HA instance has 21 ZigBee devices (connected via Zigbee2MQTT), one BT device and few Wi-Fi devices. There are 191 template sensors, few dozens of derivative sensors and ~188 automations.

I know I can upgrade the HW. The easiest way is to buy a Raspberry Pi with more RAM, but you know the issue with Raspberry Pi… I know there are some other ways. I can go this way, but I prefer not doing this.

Potential solutions:

a. Increase swap (currently 100MiB) – I’ll probably do this, but I am not enthusiastic to do that. A 128GiB (mostly empty) A2 microSD has probably good enough controller to handle this without too much wear, though.

b. Change DB – by default, HA uses SQLite. I can try using MySQL/MariaDB/PostgreSQL, but I have no idea if this would get better or worse.

c. Optimize number of templates. I’ve considered using a Rust-based daemon in order to offload computation of some templates from HA, but my simple experiment (adding 1000 extra sensors doesn’t look like a huge change to memory consumption) hasn’t shown a significant memory usage increase. So, maybe it isn’t the problem. I can consider offloading some automations the same way, but it would be more complex than just offloading the templates. And I am still not convinced that they are the memory hog…

d. Anything else?

EDIT: It seems I’ve found a significant memory hog. Some time ago, I had enabled BLE tracker (and hoped it to be useful for some reason). By 1. disabling the BLE tracker and 2. purging the list of tracked devices by renaming home-assistant/config/known_devices.yaml (~3.5MiB), I’ve halved the memory usage reported by htop. Cool.

1 Like

How exactly did you disable the BLE tracker. My memory usage is up to 65% on average. I’d like to decrease it without moving to a different installation with more RAM.

Have you enabled the tracker in the first place? If not, maybe it isn’t your issue.

I also don’t know your setup and what you mean by “memory usage”. If you have 1GiB of RAM and you have 65% total memory usage* (not just the main process), it might be OK.

Also, do you have any issues with low memory?

Last but not least, if you use microSD card, what card do you have? While it might look unrelated, card read performance might be crucial when you are tight on RAM, because of caches. When you are tight on RAM, Linux will discard caches and you will need more reads from the microSD. I’ve experienced the performance hit especially with updating – with a slow card, the system was unusable for dozens of minutes.


*) Well, there might be some philosophical debates what actually “memory usage” means in Linux; When you include caches, you might want to use all the available memory. When you exclude caches, you might underestimate the total RAM need.