Skip to content

Just importing networkx makes python process commit 4GB of memory #4857

Closed

Description

@Ark-kun

Related to vscode-restructuredtext/snooty-parser#24 and numpy/numpy#19145

Current Behavior

import networkx commits 4GB of memory

Expected Behavior

I expect networkx to consume ~0 memory when just being imported

Steps to Reproduce

  1. python3
  2. import networkx
  3. Looks at task manager

image

Environment

Python version: 3.7.4
NetworkX version: 2.4

Additional context

Activity

  1. rossbar commented on Jun 1, 2021

    @rossbar
    Contributor

    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.

  2. AbdealiLoKo commented on Jun 1, 2021

    @AbdealiLoKo

    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 0swaps
  3. Ark-kun commented on Jun 2, 2021

    @Ark-kun
    Author

    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
    
    
  4. rossbar commented on Jun 2, 2021

    @rossbar
    Contributor

    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.4

    Then evaluate the total memory on import.

  5. Ark-kun commented on Jun 3, 2021

    @Ark-kun
    Author

    Can you try importing networkx in an environment in which numpy hasn't been installed?

    I've verified that without numpy import networkx does 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.

  6. rossbar commented on Jun 3, 2021

    @rossbar
    Contributor

    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.

  7. Ark-kun commented on Aug 26, 2021

    @Ark-kun
    Author

    Is it possible to lazy-load the community generators?
    Why would networkx always load everything including OpenBLAS?

  8. MridulS commented on Aug 27, 2021

    @MridulS
    Member

    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?

  9. rossbar commented on Aug 27, 2021

    @rossbar
    Contributor

    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

  10. dschult commented on Aug 29, 2021

    @dschult
    Member

    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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions