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.