Strange CPU and RAM Usage Increase in Python Docker on AWS – Help!

0
16
Asked By TechieCat77 On

Hey folks, I'm having a strange issue with a Python script running in a Docker container on an AWS EC2 instance. Over hours to a day, the container slowly consumes more CPU and RAM until it maxes out system resources, and the only fix is restarting it. Here's some context: The app runs 24/7, I've added `gc.collect()` in crucial parts of the code, but memory continues to rise. CPU usage also creeps up without a clear reason. There are no errors or crashes, just a slow degradation of performance. Currently, the container doesn't have any set memory or CPU limits, but that's something I plan to implement. Logging is minimal, disk I/O is low, and the Docker image is based on `python:3.11-slim`, which is pretty lightweight. Any thoughts on this "slow resource leak"? Thanks!

5 Answers

Answered By LocalRunner On

So far, when I run the relevant parts of the code locally, I've not encountered any problems. The memory usage seems stable for me.

Answered By CodeWatcher On

Double-check your I/O operations too! Sometimes, if an I/O reader isn't properly closed, it can lead to resource leaks. For instance, if you do something like `f = response.Body.Read()` but forget to follow it with `response.Body.Close()`, that could cause issues.

Answered By MemoryGuru88 On

Have you tried reproducing the issue locally? The fact that it's running on AWS shouldn't change anything significantly. It might be a problem with your code not managing memory properly. For example, check if you have any variables that are being reused instead of overridden, which could lead to memory build-up.

Answered By AppSleuth On

This seems like a specific application issue. Just calling gc.collect() won't fix reference cycles. Make sure you're not holding onto objects in ways that prevent them from being garbage collected. Enable debugging for the garbage collector with gc.set_debug(gc.DEBUG_LEAK) to track object creation. Also, look out for unreleased references, global variables, and any open file or socket handlers that could be causing leaks.

Answered By ProfileMaster On

Using a memory profiler could be a great next step. Python has a built-in tool called [tracemalloc](https://docs.python.org/3/library/tracemalloc.html#compute-differences) that can help you monitor memory usage over time. While memory_profiler is another option, it seems it's no longer maintained, so keep an eye out for alternatives. With these tools, you might identify growing structures or leaks in your code.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.