fix: high memory usage & potential memory leaks #386
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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#5807I 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.