Python and memory consumption when opening TIFF images

自古美人都是妖i 提交于 2019-12-11 14:13:49

问题


I am writing a program that deals with several 16-bit TIFF images that consumes a lot of RAM.

If I am opening images like this:

import cv2
for i in range(1000):
    img = cv2.imread(file[i])

Does each image array go to memory, or does it get deleted when I assign it to the new value? If it does not get deleted, how can I delete it before opening a new one?

Thanks!


回答1:


For each iteration, img gets reassigned, so the old frames will get deleted automatically by the garbage collector.




回答2:


If, for some reason the interpreter is not garbage collecting the data, you can force its hand with this:

import gc
gc.collect()

Only do this if you know there's a memory leak, however, as running it (especially looping it!) can slow your program down, and it's usually best to leave memory management to the interpreter.



来源:https://stackoverflow.com/questions/19414289/python-and-memory-consumption-when-opening-tiff-images

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!