[Tutor] How to debug a memory leak in a wsgi application?

James Chapman james at uplinkzero.com
Thu Dec 14 05:46:23 EST 2017


​Ah OK, now I understand why you mentioned pymalloc to begin with.

I'm not familiar with uWSGI or cython. That said, why do you think it's
uWSGI causing a leak? It seems unlikely.
Python projects can grow in size if you're not dereferencing objects...
(see https://f0rki.at/hunting-memory-leaks-in-python.html)

If you use valgrind combined with python memory_profiler you should
hopefully be able to get an idea as to where the leak is coming from. It's
probably in your own code and leaks can be incredibly difficult to track
down. Typically while reviewing your own code you end up skipping over the
error time and time again because you become blind to your errors, so it
might help to have someone else peer review it.

These 2 links are a good starting point.
https://github.com/KratosMultiphysics/Kratos/wiki/Checking-memory-usage-with-Valgrind
https://github.com/pythonprofilers/memory_profiler

One last note, if you are doing any of your own memory allocations, then
make sure you're also freeing them:
https://cython.readthedocs.io/en/latest/src/tutorial/memory_allocation.html

But note, if you did this in cython:

    cdef double* data
    data = <double*> PyMem_Malloc(100 * sizeof(double))
    data = <double*> PyMem_Malloc(100 * sizeof(double))
    PyMem_Free(data)

You would (probably, you would in C/C++) end up with a leak because
you've changed the pointer. When you go to free it, only the 2nd
allocation will be freed and you'll have no way of freeing the first.

HTH


More information about the Tutor mailing list