import os import cv2 import logging import datetime from tqdm import tqdm from sys import getsizeof, stderr from itertools import chain from collections import deque from reprlib import repr logging.basicConfig(filename=f"{datetime.datetime.now().strftime('%Y%m%d-%HH%MM%SS')}-test-load-image", format='[%(asctime)s][%(levelname)s] %(message)s', level=logging.INFO) def load_images(input_path, num_frame): images = [] for i in tqdm(range(num_frame)): iframe = i%2000 image = cv2.imread(os.path.join(input_path, '%05d.png' % iframe)) images.append(image) # option - 1 (del images) # del images[0] # option - 2 (writer image) # cv2.imwrite("temp.jpg", image) if i%100 == 0: # logging.info(f"iter={i:06d}, [only append], images memory addr=0x{id(images):08x}, size={total_size(images)/1024/1024:0.2f} MB") # logging.info(f"iter={i:06d}, [append + del images[0]], images memory addr=0x{id(images):08x}, size={total_size(images)/1024/1024:0.2f} MB") logging.info(f"iter={i:06d}, [append + write], images memory addr=0x{id(images):08x}, size={total_size(images)/1024/1024:0.2f} MB") return images def total_size(o, handlers={}, verbose=False): """ Returns the approximate memory footprint an object and all of its contents. Automatically finds the contents of the following builtin containers and their subclasses: tuple, list, deque, dict, set and frozenset. To search other containers, add handlers to iterate over their contents: handlers = {SomeContainerClass: iter, OtherContainerClass: OtherContainerClass.get_elements} """ dict_handler = lambda d: chain.from_iterable(d.items()) all_handlers = {tuple: iter, list: iter, deque: iter, dict: dict_handler, set: iter, frozenset: iter, } all_handlers.update(handlers) # user handlers take precedence seen = set() # track which object id's have already been seen default_size = getsizeof(0) # estimate sizeof object without __sizeof__ def sizeof(o): if id(o) in seen: # do not double count the same object return 0 seen.add(id(o)) s = getsizeof(o, default_size) if verbose: print(s, type(o), repr(o), file=stderr) for typ, handler in all_handlers.items(): if isinstance(o, typ): s += sum(map(sizeof, handler(o))) break return s return sizeof(o) bg = load_images('/ssdata2/chenfeng/data/render/a5/t01/0-ori/ori_frames', 100000)