thomasmauerer / hassio-addons

My collection of addons for Home Assistant
MIT License
465 stars 54 forks source link

Large RAM consumption after triggered backup #156

Open aimoehi opened 1 year ago

aimoehi commented 1 year ago

i just noticed that my ram consumption pretty much doubled during a scheduled backup using this addon: image here you see the memory consumption increase from 1.4gb to 3.02 directly after the schedule triggered at 12 o'clock.

the second bump at around 12:42 is a manual full backup via HA Settings triggered by me for comparison -> Something eats memory like nothing and "forgets" to release it?

feel free to contact me for logs, but i might need assistance for what to provide...

edit: Running HA 2023.8.4 on Proxmox, 2 Cores, 6GB Ram Samba Backup 5.2.0 onto NAS directory

Nick2253 commented 10 months ago

I'm also seeing this same behavior, however this may be expected.

It's clear that the backup process takes up a bunch of memory. I'm not exactly sure how it calculates how much it can use, but it clearly restrains itself. If it was a leak, it would just keep eating up memory until it crashed.

Based on my theory, to understand what's going on, you need to understand a few things about how Memory Ballooning works in Proxmox and how memory management works in Linux.

First off, if you have any PCIe devices passed through to a VM, memory ballooning will never reduce the memory in a VM. Since the mapped hardware requires specific memory addresses, once those are allocated to the VM, the hypervisor can never take it away.

Secondly, ballooning will only kick in once the host is under 20% free memory. Even if the VM no longer needs the memory, the system will not demand it back. As such, it's up to the VM itself to return memory if it no longer needs it. In Linux-land, the system tends to be very memory greedy; not in the sense that it needs a lot of memory to run, but once it gets ahold of some memory, it never wants to let it go, unless it is explicitly requested by some other process. In other words, why let your memory sit idle? It does you no good if its just sitting there empty. As such, once memory is allocated in the VM, the system tends not to want to release it. Different systems will have different thresholds for this behavior; my guess is that the Home Assistant Supervisor tends to be especially greedy in order to maximize performance as things load in-and-out of memory.

To test this theory, from the Supervisor console, I ran the following command:

sync; echo 3 > /proc/sys/vm/drop_caches 

This tells the system to sync all filesystem writes to disk (to prevent corruption), then to clear (drop) the pagecache, dentries, and inodes. After running this command, my memory use dropped back to "normal".

TO BE CLEAR: this is not a command that you should run regularly on your system. All you are doing is taking away usefully allocated memory, which will ultimately decrease performance. The system should be capable of doing this on its own. And in any event, this isn't really a Samba Backup problem, but a Home Assistant Supervisor problem. You're just seeing it with Samba Backup because you are now regularly performing backups on the system.