The server configuration includes 8 cores, 15.6 GB of RAM, and 8 GB of swap space. It hosts multiple long-running services that occasionally trigger resource-intensive tasks like video processing, image manipulation, or audio transcription. These tasks often cause memory spikes that lead to system instability.
A two-layer protection system was implemented to manage memory usage. The first layer is a bash script running via cron every minute, which acts as a hard limit. The second layer is a Python-based daemon that monitors memory usage every second, providing a more responsive mechanism to terminate processes when available memory drops below 10% of the total.
The Python daemon uses /proc to identify processes and selects victims based on their Resident Set Size (RSS). It ignores oom_score, as system services are assigned an oom_score_adj of -900, which is inherited by child processes. The daemon also writes termination reasons directly to the process's stderr stream to aid in debugging.
Analysis of the logs over a 1.5-month period revealed 57 process terminations, including a specific incident where 26 ffmpeg processes were terminated within three minutes. The system successfully maintained stability during these spikes, ensuring that the primary service processes remained unaffected by the memory management actions.





