-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
Potential memory leak from TextIOWrapper which would hold unnecessary number of arenas
#104954
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
|
I was wondering if this is an inaccurate(delayed) report from memray. Can you run this program for a long time and see if the memory it actually allocated is increasing over time? Using |
|
Could you also try |
|
And I tried another way: |
Ah that was my fault! Thanks! |
Well actually no. Like I said, memory management in CPython is complicated. If I understand correctly, CPython has an extra layer of internal memory management system for small objects(I think the threashold is 512 bytes but I might be wrong). Instead of allocating/freeing memory frequently on small objects, CPython allocates a large chunk of memory from the OS and distribute them internally. For This is a bit over-complicated than what we need here - the point is, if you load a large json file vs a small one, it's expected that even after all the objects are "freed", the process with the large one takes more memory than the one with the small one. However, it should not still take the same amount of memory before it deallocates the json object. I'll do some experiment myself and see if I can find anything interesting. |
Well, I see what you mean. The memory applied for in the early stage is maintained in the memory pool. If this is normal, is there any method to forcibly release the memory? |
There's still something wrong with it, I'm trying to investigate now - but it's not about To answer your question - there's no way to forcibly free the memory, but if everything works properly, the memory residue should be significantly smaller. |
|
Indeed, if I open a larger file and put open out of the loop, the memory does drop off a bit. However, if I open a different file each time, I can't put it outside the loop, and the memory is still high. |
|
I spent a couple of hours on this and hit a dead end - I'm out of ideas now but I do have some findings. I'll share my findings and hopefully help others to solve this. First of all, this is not gaslighting, it's a real issue. If you track the arenas left after the code ends(when it hits the infinite loop), you'll find that the number of arenas will be increasing about linearly with the number of loops above(where it opens a file and does a (json.loads)). This (I think) is an indicator that a very small piece of memory is leaked every loop. To load the json object, we need a lot of arenas, and one of them has the leaked(not untracked) piece of memory and can't be freed when the json object is deallocated. The problem is not def generate():
members = []
with open("./test.json", 'r') as fb:
content = fb.read()
for i in range(50):
# Notice that without TextIOWrapper, the problem won't reproduce
a = io.TextIOWrapper(io.BufferedReader(io.StringIO("abc")))
members.append(json.loads(content))
del membersI digged into the code of If you set the error and return Unfortunately, this indicates that one of the following things are happening:
The only remotely interesting fact about Even though I'd like to solve this issue, I ran out of ideas. It's hard for me to even come up with a theory. So, any help is welcome! |
|
From the comment above, it seems the title should be changed. |
|
I tested it, and if you add libc = ctypes.CDLL("libc.so.6")
return libc.malloc_trim(0)The memory can be forcibly reclaimed, and the residual memory is reduced to a relatively low level. |
I'm not 100% sure but I believe that equivalently reorganized the memory - the leaked memory is still there, but the impact is reduced. So you did not forcibly reclaim the memory, you just squeezed some potential available memory out from the heap(which might be there because of the leaking memory of course). I can't tell about the risk either, it's a pretty low level API. In theory it should be safe. We still have the mystery to solve though, but this might relieve your practical issue. |
TextIOWrapper which would hold unnecessary number of arenas
What if memory is needed elsewhere in the program? Also, what are in your guy's JSON files? I cannot reproduce with an empty JSON file. |
|
I randomly created one - I believe the essential thing is to create a JSON file that will generate objects with different sizes, so I just created a JSON file with random size strings as keys and values and it worked. It's possible that this is a corner case issue for arenas - I should confirm whether the left arenas are empty. |







sample code:
Use

python -m memray run json_rep.pyto generate flamefile:Bug report
Theoretically, the del variables should not occupy any memory. However, the printed flame chart shows that the memory applied for by json.loads is not released, which seems to cause memory leakage.
Your environment
env:
└─$ uname -a
Linux DESKTOP-IEKI9L0 5.15.90.1-microsoft-standard-WSL2 #1 SMP Fri Jan 27 02:56:13 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
└─$ python --version
Python 3.10.6
The text was updated successfully, but these errors were encountered: