Reducing numpy memory footprint in long-running application

后端 未结 2 1137
無奈伤痛
無奈伤痛 2021-02-09 00:07

In my application one hundred numpy arrays (1000 complex elements each) are generated and filled with data. Then over many iterations, the array elements are modified over and o

2条回答
  •  爱一瞬间的悲伤
    2021-02-09 00:34

    I had the same kind of issue. Unfortunately I found no workaround. The only thing that worked for me was to refactor the code into small isolated function. Those functions should be made so that I can convince myself I keep no references to arrays that keep the garbage collector from collecting arrays. You should take special care to array views which are generated by slicing, etc ...

    In order to make code more memory efficient and less prone to memory leaks, I often found usefull to use the out= keyword argument provided by many numpy functions.

提交回复
热议问题