You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Just importing networkx makes python process commit 4GB of memory #4857
The fact that you are seeing similar behavior when importing multiple projects (thanks for providing links) would make me think that this may be something specific to your system configuration rather than the packaged libraries themselves. Charris' explanation in the related numpy issue is a good thing to try, though numpy shouldn't be imported by default with networkx 2.4.
The fact that you are seeing similar behavior when importing multiple projects (thanks for providing links) would make me think that this may be something specific to your system configuration rather than the packaged libraries themselves.
I'm not sure about this. The only "interesting" thing about my system is 32 CPU cores which amplifies the issue.
I've created the bugs in different projects as I was going through the investigation: vscode -> snooty -> networkx -> numpy
There is a ~1GB delta between numpy and networkx, so there might be something to improve when the numpy issue is fixed.
numpy shouldn't be imported by default with networkx 2.4.
The stack trace in the snooty issue shows the import stack:
File "C:\Users\Ark\AppData\Local\Programs\Python\Python37\lib\site-packages\networkx\__init__.py", line 112, in <module>
import networkx.generators
File "C:\Users\Ark\AppData\Local\Programs\Python\Python37\lib\site-packages\networkx\generators\__init__.py", line 8, in <module>
from networkx.generators.community import *
File "C:\Users\Ark\AppData\Local\Programs\Python\Python37\lib\site-packages\networkx\generators\community.py", line 20, in <module>
from scipy.special import zeta as _zeta
File "C:\Users\Ark\AppData\Local\Programs\Python\Python37\lib\site-packages\scipy\__init__.py", line 63, in <module>
from numpy import show_config as show_numpy_config
File "C:\Users\Ark\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\__init__.py", line 140, in <module>
from . import core
Can you try importing networkx in an environment in which numpy hasn't been installed? This should help isolate where the problem may be originating. e.g. something like:
I've verified that without numpy import networkx does not commit any significant amount of memory.
Great, thanks for checking that. Since this isn't directly related to NetworkX I will close this issue.
Re: the numpy issue - whenever I have trouble with multiple threads the first thing I try is suppressing openblas threading via the OMP_NUM_THREADS environment variable, i.e. export OMP_NUM_THREADS=1. You may want to try this then profile the memory usage at import time for your system. If you no longer see the memory spike, you may want to consider opening an issue upstream with whichever openblas provider you are using.
Why would networkx always load everything including OpenBLAS?
NetworkX shouldn't be loading OpenBLAS (numpy brings that in), we don't have numpy as a base dependency anymore. Can you give some more information about the environment?
Yeah this has more to do with the specific environment and not NetworkX itself --- NetworkX doesn't directly depend on OpenBLAS, so any problems related to that should be reported to the relevant project (e.g. OpenBLAS or maybe the project that pulls it in, e.g. NumPy or SciPy).
That being said, see #4909 for the current thoughts/ideas on how to implement lazy loading in NetworkX
The current release of NetworkX does lazy load numpy -- in the sense that it only imports numpy if you are using a function that requires numpy. Pull Requests like #4909 discuss hopefully better ways to lazy-load. But the tried and true -- don't import until the function that needs numpy is called -- should work.
If you see that importing networkx causes numpy to be imported, please report that as a new issue for NetworkX.
That means some part of networkx is actually importing numpy before it needs to. That should not be happening.
Activity
rossbar commentedon Jun 1, 2021
The fact that you are seeing similar behavior when importing multiple projects (thanks for providing links) would make me think that this may be something specific to your system configuration rather than the packaged libraries themselves. Charris' explanation in the related numpy issue is a good thing to try, though numpy shouldn't be imported by default with networkx 2.4.
AbdealiLoKo commentedon Jun 1, 2021
For what it's worth - its 37kb for me on Ubuntu 18.04 WSL in Windows 10
Python 3.9 / networkx 2.5.1
$ /usr/bin/time python -c 'import networkx' 0.39user 0.40system 0:00.43elapsed 181%CPU (0avgtext+0avgdata 37928maxresident)k 24376inputs+0outputs (67major+6273minor)pagefaults 0swapsArk-kun commentedon Jun 2, 2021
I'm not sure about this. The only "interesting" thing about my system is 32 CPU cores which amplifies the issue.
I've created the bugs in different projects as I was going through the investigation: vscode -> snooty -> networkx -> numpy
There is a ~1GB delta between numpy and networkx, so there might be something to improve when the numpy issue is fixed.
The stack trace in the snooty issue shows the import stack:
rossbar commentedon Jun 2, 2021
Can you try importing networkx in an environment in which numpy hasn't been installed? This should help isolate where the problem may be originating. e.g. something like:
python -m venv nx-testenv source nx-testenv/bin/activate python -m pip install networkx==2.4Then evaluate the total memory on import.
Ark-kun commentedon Jun 3, 2021
I've verified that without numpy
import networkxdoes not commit any significant amount of memory.Importing scipy/numpy has been fixed here: https://github.com/networkx/networkx/pull/4296/files#diff-40c874139c1a50f71fec27aa9572d4e3615fbf372be5d1e7bbdbaa25547e0b2aL9 although it's not released yet.
rossbar commentedon Jun 3, 2021
Great, thanks for checking that. Since this isn't directly related to NetworkX I will close this issue.
Re: the numpy issue - whenever I have trouble with multiple threads the first thing I try is suppressing openblas threading via the
OMP_NUM_THREADSenvironment variable, i.e.export OMP_NUM_THREADS=1. You may want to try this then profile the memory usage at import time for your system. If you no longer see the memory spike, you may want to consider opening an issue upstream with whichever openblas provider you are using.Ark-kun commentedon Aug 26, 2021
Is it possible to lazy-load the community generators?
Why would networkx always load everything including OpenBLAS?
MridulS commentedon Aug 27, 2021
NetworkX shouldn't be loading OpenBLAS (numpy brings that in), we don't have numpy as a base dependency anymore. Can you give some more information about the environment?
rossbar commentedon Aug 27, 2021
Yeah this has more to do with the specific environment and not NetworkX itself --- NetworkX doesn't directly depend on OpenBLAS, so any problems related to that should be reported to the relevant project (e.g. OpenBLAS or maybe the project that pulls it in, e.g. NumPy or SciPy).
That being said, see #4909 for the current thoughts/ideas on how to implement lazy loading in NetworkX
dschult commentedon Aug 29, 2021
The current release of NetworkX does lazy load numpy -- in the sense that it only imports numpy if you are using a function that requires numpy. Pull Requests like #4909 discuss hopefully better ways to lazy-load. But the tried and true -- don't import until the function that needs numpy is called -- should work.
If you see that importing networkx causes numpy to be imported, please report that as a new issue for NetworkX.
That means some part of networkx is actually importing numpy before it needs to. That should not be happening.