Skip to content

Conversation

NickUfer
Copy link

@NickUfer NickUfer commented Sep 26, 2025

This PR fixes the out of the box high memory footprint of ofelia. Right now every execution reserves buffer of 10MB for Stdout and 10MB for Stderr. In the code it is called maxStreamSize, but this size gets allocated instantly. If you run multiple jobs at some point in parallel this reserves a lot of space on the heap without a reason. This causes needlessly high memory usage constantly as described here: mailcow/mailcow-dockerized#5807
I switched it out for bytes.Buffer{} which is dynamically growing. There is still potential for excessive memory usage if a job outputs extreme amounts of log, but dealing with this would need some refactoring of streaming the output line by line instead of buffering all of it to output at once.

Additionally this PR removes potential memory leaks around the usage of the return value of dockerClient.ListContainers. Currently the Labels map from each container is modified and that modified map is used to create a config object. This could cause the structure originally holding the label map to also be kept in memory.
I refactored that too so a copy of the relevant data of the map is created and returned.

Lastly I added code to close the label change watch goroutine when the app is closing.

@taraspos
Copy link
Collaborator

taraspos commented Sep 28, 2025

Hi @NickUfer,
thanks for your PR. Ofelia did use bytes.Buffer{} originally, however as you mentioned yourself, this lead to the extreme memory usage for jobs that produced a lot of logs. That's why circbuf was introduced instead:

We should probably try replacing it with kubernetes implementation of growing ring buffer instead:

Or maybe just pipe it into a temp file instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants