Skip to content
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

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

Open
mgedmin opened this issue Nov 21, 2013 · 5 comments
Open

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

mgedmin opened this issue Nov 21, 2013 · 5 comments

Comments

@mgedmin
Copy link
Owner

mgedmin commented Nov 21, 2013

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
Copy link

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
Copy link
Owner Author

mgedmin commented Jan 17, 2017

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

@nathanielatom
Copy link

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
Copy link
Owner Author

mgedmin commented Jan 18, 2017

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

@nathanielatom
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants