Why Is My Python Docker Container on AWS Gradually Using More CPU and RAM?

0
0
Asked By TechieTurtle42 On

Hey folks, I'm encountering a weird issue with a Python script that's running inside a Docker container on my AWS EC2 instance. I've noticed that over a period of several hours to a day, the container's CPU and RAM usage slowly creeps up until it maxes out system resources. I have to restart the container to get it back to normal. Here's a bit more context: the app runs continuously 24/7, I've added `gc.collect()` in key parts of the code, but memory usage keeps rising. CPU load increases too, without any clear cause. There are no crash reports or error messages, just a gradual decline in performance. Currently, there are no memory or CPU limits set for the container, but that's something I'm looking to implement. The logging is minimal and disk I/O is low. The Docker image is lean, based on `python:3.11-slim`, and I don't have any heavy libraries like pandas or OpenCV in use. Has anyone experienced this slow resource leak before? Any insights would be appreciated.

4 Answers

Answered By PythonGuru88 On

From my experience, the problem seems to be within your Python code rather than the image itself. Keep an eye on your code for resource management; that’s likely where the issue lies.

Answered By CleverCoder99 On

It sounds like you might have some unclosed resources or caching going on in your code. When you call garbage collection and it doesn't free up memory, that often points to object scoping issues. Are you sure you're not caching objects that could be causing the increase? Also, check if you have any threads that are lingering and not terminating properly. Have you done any profiling on your code to pinpoint the leak?

Answered By DebuggingDiva On

I’d suggest testing your base image without any scripts, just to rule that out. However, I really think the script must be the culprit. You might want to run it through a service like Gemini to detect any resource leaks.

Answered By DockerDude77 On

Have you tried running a clean container without your Python script to see if the issue persists? It's a good way to figure out if it's your code or the base image causing the problem.

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.