Help with finding the cause of RAM and processor spikes

I have a computer intended to be a dedicated server running homeassistant.
Dell Optiplex 3020 with an i5-4570 @ 3.2GHz x 4
16GB RAM
running ubuntu 22.04.1 LTS

I’m using VirtualBox to run HA OS (because I couldn’t figure out how to get KVM to run with a wifi connection)
It’s got 4 CPUs and 10GB RAM allocated to it. Should be more than enough, right?

Most of the time processor use is less than 2% and memory usage is around 7%, but at random (as far as I can tell) times processor and memory usage start spiking. Processor usage jumps to around 10-13% while memory usage continues to climb up up up. I created an automation to “call service: Home Assistant Supervisor: Reboot the host system” if memory usage stays above 70% for 6 minutes because before it the memory usage would simply rise and stay at 100% until the system froze up/crashed and I would have to reset it whenever I found out.

Typically the system would reboot itself a few times a day which isn’t ideal but it was a band-aid that kept everything working, but it’s happened 9 times in the last 12 hours and it’s messing with my automations.

Has anybody had a similar issue or know which logs I can share in order for somebody to diagnose and hopefully fix this?

Thank you so much for taking the time to read this and hopefully help me fix this headache

Turns out I had a poorly built automation that was causing the issue.

The automation was simple enough:

  • If a sensor reads under an amount for 2 minutes, run a script.
  • Repeat until the sensor reads above the desired amount.

I thought the system would check the reading after the script had finished, but instead it would immediately try to run the script thousands upon thousands of times, creating log errors, until the system resources are used up and it crashes.

Removing the “Repeat” step solved the problem and my system has been running between 0-2% processor usage and 13-14% memory usage with no errors since.

I believe I could of solved the issue by changing the automation too. If, instead of the “Repeat” step, I added a “Delay” for the length of the script, and changed the mode of the automation to “Queued”… that would have worked too.