Memory leak when array contains circular references
See original GitHub issueMemory is leaked when an array contains a circular reference:
>>> import gc
>>> import sys
>>> import numpy as np
>>> class Circular(object): pass
...
>>> c = Circular()
>>> c.arr = np.array([c])
>>> del c
>>> gc.collect()
0
>>> gc.collect()
0
>>> print [ repr(o) for o in gc.get_objects() if type(o) == Circular ]
['<__main__.Circular object at 0x100e97f10>']
>>> print [ sys.getrefcount(o) for o in gc.get_objects() if type(o) == Circular ]
[2]
This is because PyArray
doesn’t implement tp_traverse
(for fairly reasonable reasons)… but also leads to hard-to-track-down memory leaks.
At first pass it seems reasonable to implement a tp_traverse
which only traverses if dtype=object
… but that does have a performance tradeoff.
Issue Analytics
- State:
- Created 8 years ago
- Reactions:1
- Comments:8 (5 by maintainers)
Top Results From Across the Web
How can circular references cause memory leakage in ...
Circular reference In hybrid system, where reference counting and garbage collection are used, memory leaks will occur because the system fails ...
Read more >Resolving Circular Reference Related Memory Leaks ... - DZone
Fortunately, there are some solutions to circular reference related memory leaks. The most straightforward approach is to assign the null value ...
Read more >Do circular references in C# cause memory leaks? [duplicate]
I want to create C# classes to represent this data objects, but it'll result in circular references. Here is a simple example class...
Read more >Event Handlers, Circular References, and Alleged Memory ...
So as you can see, the presence of a circular reference is not sufficient to cause a leak in a garbage collected environment....
Read more >Memory leaks due to circular referencing in PHP - Educative.io
The most reliable way to resolve circular referencing is to restructure your code. For example, if your classes depend on one another of...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I came here because I tracked down a memory leak in NumPy, then found out that it’s a known issue. At least I can leave my reproducer and start watching this issue.
Using pympler:
Not using pympler, just watching system memory (with
free
) interactively:Within about a minute, you end up using a GB of RAM, and it keeps going until you stop it, then resets to baseline when you shut down Python.
The PR has stalled for a while, although I think it is a bug to not implement circular refcounting. I think it is still relevant, getting someone outside of the core team in to confirm (or remove) the untracking behaviour might help it along a large bit I think. There was once the question about speed impact. I honestly don’t think it should matter, but if anyone is concerned trying to get a check could help as well.