Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django memory leak: possible causes?

Tags:

I've a Django application that every so often is getting into memory leak.

I am not using large data that could overload the memory, in fact the application 'eats' memory incrementally (in a week the memory goes from ~ 70 MB to 4GB), that is why I suspect the garbage collector is missing something, I am not sure though. Also, it seems as this increment is not dependant of the number of requests.

Obvious things like DEBUG=True, leaving open files, etc... no apply here.

I'm using uWSGI 2.0.3 (+ nginx) and Django 1.4.5

I could set up wsgi so that restart the server when the memory exceeds certain limit, but I wouldn't like to do that since that is not a solution really.

Are there any well know situations where the garbage collector "doesn't do its work properly"? Could it provide some code examples?

Is there any configuration of uWSGI + Django that could cause this?

like image 926
trinchet Avatar asked Aug 02 '14 00:08

trinchet


People also ask

Why is Django leaking memory?

Memory leaks in Python typically happen in module-level variables that grow unbounded. This might be an lru_cache with an infinite maxsize , or a simple list accidentally declared in the wrong scope. Leaks don't need to happen in your own code to affect you either.

What could be the possible cause of memory leak?

In computer science, a memory leak is a type of resource leak that occurs when a computer program incorrectly manages memory allocations in a way that memory which is no longer needed is not released. A memory leak may also happen when an object is stored in memory but cannot be accessed by the running code.

What can cause memory leak Python?

What causes memory leaks in Python? The Python program, just like other programming languages, experiences memory leaks. Memory leaks in Python happen if the garbage collector doesn't clean and eliminate the unreferenced or unused data from Python.

What does a possible memory leak mean?

Memory leaks are when programs on the computer incorrectly manage memory allocations. This is not uncommon on modern software and can cause performance drags on the system. The easiest way to fix this issue is to close and reopen the program with the leak, as it will reset the allocations.


1 Answers

I haven't found the exact stuff I'm looking for (each project is a world!), but following some clues and advices I managed to solve the issue. I share with you a few links that could help if you are facing a similar problem.

django memory leaks, part I, django memory leaks, part II and Finding and fixing memory leaks in Python

Some useful SO answers/questions: Which Python memory profiler is recommended?, Is there any working memory profiler for Python3, Python memory leaks and Python: Memory leak debugging

Update

pyuwsgimemhog is a new tool that helps to find out where the leak is.

like image 113
trinchet Avatar answered Oct 02 '22 04:10

trinchet