Skip to content

cv2.VideoCapture leaking memory... #1151

@What-a-stupid-username

Description

@What-a-stupid-username
import psutil
import os
import random
import cv2
import gc

def logMem():
    uss = psutil.Process(os.getpid()).memory_full_info().uss / 1024 / 1024
    print(f"Memory usage: {uss} MB")

def Test(path):
    print("-"*100)
    print(path)
    cap = cv2.VideoCapture(path)
    total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    print("init")
    logMem()
    for i in range(20):
        k = random.randint(0, total_frames)
        _ = cap.set(cv2.CAP_PROP_POS_FRAMES, k)
        ret, frame = cap.read()
        del ret, frame
    print("finish")
    logMem()
    cap.release()
    del cap
    gc.collect()
    gc.collect()
    gc.collect()
    print("del")
    logMem()
    print("-"*100)

path = 'PATH_TO_VIDEOS_FOLDER'
videos = os.listdir(path)

for i in range(10):
    Test(path + random.choice(videos))

Memory keep growing after every loop iter.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions