opencv / opencv/opencv-python

cv2.VideoCapture leaking memory...

Open
#1,151 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

incomplete
Dominant language
Python
Stars
5.4k
Forks
1k
Avg merge
22h 17m
Merged PRs (30d)
3

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided Python reproduction with representative video files and record the Python, OpenCV, and platform details, which the issue does not specify. Trace memory across repeated VideoCapture creation, seeking, reading, release, and garbage collection; done means isolating a reproducible leak and identifying the relevant component or a minimal fix.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
computer-vision
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.