'Networkx graph memory usage?

How to check the amount of memory used by networkx graph ?

There is method to check the number of nodes and edges, but I could not find one for memory usage ?



Solution 1:[1]

You can get an estimate by adding up the size of the edge list and the size of the node list:

 sys.getsizeof(G.edge) + sys.getsizeof(G.node)

Solution 2:[2]

In NetworkX 2.0 and later, G.edges and G.nodes return iterators, so taking the size of those directly doesn't work, it just gives you the size of the iterator objects. You need it iterate over those objects and get the size of each edge and node and sum those to estimate the memory used by the graph.

import networkx as nx
import sys

G = nx.Graph()
# populate the graph with nodes and edges

edge_mem = sum([sys.getsizeof(e) for e in G.edges])
node_mem = sum([sys.getsizeof(n) for e in G.nodes])

print("Edge memory:", edge_mem)
print("Node memory:", node_mem)
print("Total memory:", edge_mem + node_mem)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DYZ
Solution 2 Bill the Lizard