objgraph.by_type('file', untracked=True)
mgedmin opened this issue · 5 comments
So, not all objects are tracked by the Python garbage collector, and therefore not all show up in gc.get_objects()
. For example: file objecs don't, so objgraph.by_type('file') will always return an empty list.
Implement a workaround that does
return [r for o in objects for r in gc.get_referents(o) if type(r).__name__ == typename]
if the user asks for it.
Thanks! This line helped me track down some massive leaky numpy arrays! However, it was initially confusing why it didn't work by default.
Would it be possible to add this feature without the untracked
argument and use not gc.is_tracked
instead?
This would be a huge help to people looking for numpy arrays, which I think is a common use case since they can often consume a very large amount of memory.
I'm not sure what you mean by not gc.is_tracked
? Could you elaborate?
I don't think this is the best solution, but maybe something like:
import importlib
...
if '.' in typename:
tracked = [o for o in objects if _long_typename(o) == typename]
if tracked:
return tracked
module = importlib.import_module('.'.join(typename.split('.')[:-1]))
cls = getattr(module, typename.split('.')[-1])
if gc.is_tracked(cls):
return tracked
return [r for o in objects for r in gc.get_referents(o) if type(r).__name__ == typename]
else:
...
for fully-qualified type names at least.
Hm, it's an interesting idea! I'm afraid it wouldn't work: gc.is_tracked()
works on objects, not types.
Ah, my mistake - I thought it could handle types as well.