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

Work around memory leak in networkx #801

Merged
merged 3 commits into from
Feb 22, 2018
Merged

Work around memory leak in networkx #801

merged 3 commits into from
Feb 22, 2018

Conversation

fritzo
Copy link
Member

@fritzo fritzo commented Feb 22, 2018

Warning: NSFW

Fixes #798

@fritzo fritzo requested a review from eb8680 February 22, 2018 02:25
def __del__(self):
# Work around cyclic reference bugs in networkx.DiGraph
# See https://github.com/uber/pyro/issues/798
self._graph.__dict__.clear()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it!!! Great idea @fritzo

Copy link
Member

@eb8680 eb8680 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fritzo singlehandedly designed and implemented this!!! I refuse to take any credit for this!

@fritzo
Copy link
Member Author

fritzo commented Feb 22, 2018

@fritzo singlehandedly

for the record i am opposed to such kludges and eagerly await this code's deletion

@karalets
Copy link
Collaborator

karalets commented Feb 22, 2018 via email

Copy link
Member

@neerajprad neerajprad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great piece of sleuthing! I'm glad this is finally resolved.

graph_type = kwargs.pop("graph_type", "flat")
assert graph_type in ("flat", "dense"), \
"{} not a valid graph type".format(graph_type)
self.graph_type = graph_type
super(Trace, self).__init__(*args, **kwargs)

def __del__(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When is this override required? At least in the tests, it looks like the circular references are resolved with the other changes you made.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nx.DiGraph has a circular reference and would not be gc'd when the Trace is collected. This override is needed to cut the circular references in the nx.DiGraph object so that it can be immediately gc'd. If you were to remove this line, tests would still pass but DiGraph objects would leak. I guess we could strengthen the test to check counts for both Trace and nx.DiGraph objects, but @karalets already confirmed that things are working correctly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I suppose the gc will detect those cycles at some point, but this should immediately fix the issue. I'm still not sure why the Digraph object should have circular references though, will pick your brains on this later.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cycles arise because nx.DiGraph stores view objects and those view objects have references back to the graph, e.g. here. I've filed networkx/networkx#2885 with a suggested fix.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cycles arise because nx.DiGraph stores view objects and those view objects have references back to the graph

Kudos to discovering this, and filing the issue! :) Thanks for explaining.

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

Successfully merging this pull request may close these issues.

4 participants