How can I explicitly free memory in Python?

前端 未结 10 2578
青春惊慌失措
青春惊慌失措 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:23

    You can't explicitly free memory. What you need to do is to make sure you don't keep references to objects. They will then be garbage collected, freeing the memory.

    In your case, when you need large lists, you typically need to reorganize the code, typically using generators/iterators instead. That way you don't need to have the large lists in memory at all.

    http://www.prasannatech.net/2009/07/introduction-python-generators.html

提交回复
热议问题