Fixing a Tough Memory Leak in Python
info.cloudquant.com
info.cloudquant.com
The solution at the time was to process the large lists in smaller chunks at a time to make it less likely that a small longer lived object would be allocated in the middle, preventing later cleanup. However, a better solution would probably have been to rewrite parts of the app in a less allocation-happy language.
Something they could have done inside Python is manually implement a short-lived objects space that lives entirely in pre-allocated memory, preventing the repeated allocations.
I should have mentioned that the app in particular was running python2.7 - some preliminary tests on porting to python3 seemed to indicate that the issue was significantly less severe there, though the problem only occurred very infrequently that restarting the affected processes when they started exceeding a memory threshold was a more cost-effective solution that spending ages debugging the interactions.
And other option might have been to switch to a malloc using mmap'ed size-class arenas like jemalloc or somesuch?
Using jemalloc we were able to reduce our memory pressure over time dramatically.