home-assistant / operating-system

:beginner: Home Assistant Operating System
Apache License 2.0
5.02k stars 982 forks source link

HA OS seems to use way to much ram for cache and swaps too much -> slower performance #1472

Closed FaserF closed 3 years ago

FaserF commented 3 years ago

Hardware Environment

Home Assistant OS release:

agners commented 3 years ago

HA OS seems to cache way too much stuff

Yellow is probably mostly page cache, and is very important to make reads faster. Essentially Linux tries to keep files (code or data) in memory as much as possible to speed up subsequent reads.

Usually things only get swapped out when its really not used for a long time (but needs to be in memory).

Imagine Home Assistant reads data (e.g. from database) every time you open your dashboard. You want your Raspberry Pi to keep that in memory. Whereas some program code from a service started in the background which is used very seldom, should be written out to swap to not take up valuable memory.

HAOS is built on Linux, which in terms has a very sophisticated algorithms to manage memory. We don't tune memory management special, and it has served well so far.

Btw: In HAOS, swap is also in memory, but compressed using zram. This is done to avoid wearing out SD cards. One might ask why use swap then even? Its a trade off: Compressed swap allows to store more in memory at the cost of CPU time. We basically trade more memory with some CPU cycles.

Is there anything to improve this? Is this a bug or a "feature"?

There are tunables in Linux, and in theory you can minimize caching. But your system is pretty much guaranteed to feel slower. In the end just "tune" for the sake of moving a bar is not helpful. You need to dig into why your Raspberry Pi 4 feels slow exactly. I don't have a straight forward recipe how to do that. Monitoring CPU/memory load, and trying different configurations might help to find what improves the situation.

I am closing this issue as its not really an issue.

FaserF commented 3 years ago

Wow, thank you so much for such a detailed answer!