python-igraph icon indicating copy to clipboard operation
python-igraph copied to clipboard

Memory Leak in graph.union

Open Maxyme opened this issue 4 years ago • 7 comments

There seems to be a memory leak in graph.union. When running the sample code below, I get the following memory profile: igraph-python-leak

import random

import numpy as np
from igraph import Graph

if __name__ == '__main__':
    for i in range(200):
        num_nodes = 100
        num_edges = 100000

        graph = Graph(num_nodes)

        edges = [
            random.choices(range(num_nodes), k=2)
            for _ in range(num_edges)
        ]

        # Create a new graph with the same edges
        attributes = {"test": np.arange(len(edges))}
        new_graph = Graph(edges=edges, edge_attrs={"test": np.arange(len(edges))})

        # Generate a union of the 2 graphs
        union_graph = graph.union(new_graph)

I'm not sure if this issue is related to the python side of igraph, or if it's in the c code. Note: this was tested with python-igraph==0.9.10

Maxyme avatar Apr 25 '22 16:04 Maxyme

Thanks, this is interesting as we have recently fixed a leak in Graph.union in 1593c14d8e9b25b876a04c2a110d12edda2211ae and this is already in 0.9.10. Let me try to reproduce this on my end.

ntamas avatar Apr 25 '22 17:04 ntamas

Let me know if you need me to try another LSan run with this code.

szhorvat avatar Apr 25 '22 17:04 szhorvat

For what it's worth, putting this through LSan reveals no problems (except for a small leak in numpy RNG, which is certainly not the cause of this).

szhorvat avatar Apr 26 '22 07:04 szhorvat

I think this will be a reference counting error, i.e. we are holding on to an object when we no longer need it, and it gets freed only when Python exits.

ntamas avatar Apr 26 '22 07:04 ntamas

Indeed it was a refcount error; it is now fixed in 043515f (develop) and 764de38 (master).

@Maxyme Can you install python-igraph from source and test whether it solves the problem for you? (See here for instructions). If you cannot compile things from source, that's okay, let me know what OS you are using and which Python version and I'll send you a precompiled wheel to test.

ntamas avatar Apr 26 '22 09:04 ntamas

@ntamas awesome, I just checked with the latest version and can confirm that the leak is fixed: no_leak

Maxyme avatar Apr 26 '22 16:04 Maxyme

Thanks for the feedback. This will be released in 0.9.11 or 0.10.0, whichever comes first.

ntamas avatar Apr 26 '22 18:04 ntamas