-
Hi! I'm not really expert with CPython internal implementation, nor with Pillow internals, so I hope my question is not too silly 😊 I'm currently hunting for some unexpected memory usage in
I checked an old PyCon talk by Dave Malcolm on CPython memory usage,
I checked Pillow C sources and currently none of the Python types defined provide an implementation for Given that none of the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Ah, such as Line 3702 in 3166901
This is referring to https://docs.python.org/3/c-api/gcsupport.html
I'm certainly not an expert in the C side of CPython, but my tentative theory is that this isn't necessary for us because the structure of our C objects might not interact with each other to ever create circular references. I wonder if @wiredfool can provide a better answer? Setting aside your specific question, if you have a self-contained Pillow script with unexpected memory usage, feel free to post it. |
Beta Was this translation helpful? Give feedback.
-
As far as I recall, we don't have any possible cycles in our C python objects. There are a couple of bits that might make it a little difficult to reason about the memory usage -- Pillow uses a cached memory allocator so that we're not malloc/freeing for every image. This can lead to memory growth proportional to the max size of all concurrent images in the process. This is in Storage.c: https://github.com/python-pillow/Pillow/blob/main/src/libImaging/Storage.c#L320 There are a couple of functions exposed on the internal im object, get_stats https://github.com/python-pillow/Pillow/blob/main/src/_imaging.c#L4159 . These might help you quantify how much memory is in the cached allocator. Valgrind's memory leaks tool doesn't seem to be able to track python memory allocations very well, so there tend to be a lot of leaks from core python. I don't generally track leaks because of that. We do use valgrind to find memory leaks that increase via iterations, because tracking allocation locations and peak memory does get us a long way towards finding the leaks in C code. I'd be surprised if there are many significant leaks in Pillow (at least in code that's been around for a year or more). There's certainly nothing that will commonly leak anything like O(ImageSize). I wouldn't be surprised if there are leaks of the O(Bytes) level. -edit-
|
Beta Was this translation helpful? Give feedback.
As far as I recall, we don't have any possible cycles in our C python objects.
There are a couple of bits that might make it a little difficult to reason about the memory usage --
Pillow uses a cached memory allocator so that we're not malloc/freeing for every image. This can lead to memory growth proportional to the max size of all concurrent images in the process. This is in Storage.c: https://github.com/python-pillow/Pillow/blob/main/src/libImaging/Storage.c#L320 There are a couple of functions exposed on the internal im object, get_stats https://github.com/python-pillow/Pillow/blob/main/src/_imaging.c#L4159 . These might help you quantify how much memory is in the cached allocator.
Va…