问题
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