How can I explicitly free memory in Python?

前端 未结 10 2574
青春惊慌失措
青春惊慌失措 2020-11-21 13:25

I wrote a Python program that acts on a large input file to create a few million objects representing triangles. The algorithm is:

  1. read an input file
10条回答
  •  时光说笑
    2020-11-21 14:05

    As other answers already say, Python can keep from releasing memory to the OS even if it's no longer in use by Python code (so gc.collect() doesn't free anything) especially in a long-running program. Anyway if you're on Linux you can try to release memory by invoking directly the libc function malloc_trim (man page). Something like:

    import ctypes
    libc = ctypes.CDLL("libc.so.6")
    libc.malloc_trim(0)
    

提交回复
热议问题