-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
Custom pickler memory leak #89988
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The following script exhibits the memory leak. It only happens if "dispatch_table" is set _before_ calling super().__init__, which is pretty unexpected. import pickle
import io
import gc
import tracemalloc
tracemalloc.start(10)
snap = tracemalloc.take_snapshot()
class MyPickler(pickle.Pickler):
def __init__(self, *args, **kwargs):
# Swapping the next 2 lines "solves" the memory leak for some reason
self.dispatch_table = dict()
super().__init__(*args, **kwargs)
l=[]
for i in range(10000):
if i % 1000 == 0:
print('='*80)
snap2 = tracemalloc.take_snapshot()
stats=snap2.compare_to(snap, 'lineno')
for s in stats[:10]:
print(s)
snap = snap2
f = io.BytesIO()
MyPickler(f)
gc.collect() The output of the last iteration is as follow. The leak of 562 kiB is apparent:
If "dispatch_table" is set after calling super().__init__, there is no leak anymore:
|
I can verify this also happens on Python 3.9.2 I also have a demonstration of this memory leak at https://github.com/tigerinus/pickle_memory_leak_with_msgspec |
The issue I found turns out to be an issue of msgspec.Struct and should be fixed by jcrist/msgspec#117 |
…up (pythonGH-94298) (cherry picked from commit 01ef1f9) Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: