mgedmin / objgraph

Visually explore Python object graphs
http://mg.pov.lt/objgraph/
MIT License
753 stars 72 forks source link

objgraph.by_type('file', untracked=True) #2

Open mgedmin opened 10 years ago

mgedmin commented 10 years ago

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.

nathanielatom commented 7 years ago

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.

mgedmin commented 7 years ago

I'm not sure what you mean by not gc.is_tracked? Could you elaborate?

nathanielatom commented 7 years ago

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.

mgedmin commented 7 years ago

Hm, it's an interesting idea! I'm afraid it wouldn't work: gc.is_tracked() works on objects, not types.

nathanielatom commented 7 years ago

Ah, my mistake - I thought it could handle types as well.