bgslibrary icon indicating copy to clipboard operation
bgslibrary copied to clipboard

Memory leak - Python Wrapper

Open rlifz opened this issue 7 years ago • 11 comments

I tested the python wrapper with Demo.py, but change the video source and noticed that the memory doesn't stop growing. Even with the example video there is a memory leak, I tested it with differents algorithms and some results. Any suggestion to fix this? I used openCV 3.1, python 2.7 and Ubuntu 14.04 for these tests.

Thanks.

rlifz avatar May 19 '17 12:05 rlifz

Hi @rlifz , Sorry for my long delayed feedback, I was in a very busy time. Thanks for reporting this issue. I will check this, and I let you updated if I have something new in my side. Best

andrewssobral avatar Jul 11 '17 09:07 andrewssobral

I am also noticing a memory leak. Testing with SubSENSE. What information can I provide to help diagnose? I was running a 30 second test video. Low quality, so 500MB is a lot of data.

image

bw4sz avatar Jul 12 '17 21:07 bw4sz

I am still investigating this, I actually think its in the opencv passing, not the bgslibrary, will confirm.

bw4sz avatar Jul 17 '17 21:07 bw4sz

I've looked this a couple different ways, I think it is BGSLibrary. It appears that it is the .apply method for the background subtractor classes. I can confirm it happens with both SubSense and LOBSTER. Memory grows as the loop continues, not releasing previous frames? Compared to cv2.createBackgroundSubtractorMOG2, the .apply method in libbgs.SuBSENSE() uses 4x memory very quickly and continues to rise until I kill it.

bw4sz avatar Jul 18 '17 21:07 bw4sz

I also noticed the memory leak and I don't think it is related to specific models. The input frame is not released and the memory grows by frame sizes.

mttucl avatar Aug 29 '17 04:08 mttucl

Same here. This is unusable in its current form - I quickly run out of memory (16GB) and start swapping. If it helps, I'm running boost from 11/19 (commit 81dcc8f) and the most recet bgs library code from 8/27 (commit e246604).

If it makes testing easier, I've attached my test script. Change the input file name in line 12 and run -> no memory leak. Uncomment line 30 *bgs.apply) and run -> memory leak.

#!/usr/bin/env python3

import cv2 import sys import numpy as np import libbgs

if name == 'main':

bgs = libbgs.FrameDifference()

camera = cv2.VideoCapture('fixed.mp4')
if not camera.isOpened():
    print("Couldn't open video")
    sys.exit()

count = 0
out = None
while True:
    ok, output = camera.read()
    if not ok: break
    print(count)
    count += 1

    if out is None:
        h, w = output.shape[:2]
        out = cv2.VideoWriter('out.mp4', 0x00000021, camera.get(cv2.CAP_PROP_FPS), (w, h), True)
        mask = np.empty(output.shape[:2], np.uint8)

    #mask = bgs.apply(output)
    # model = bgs.getBackgroundModel()
    #output[mask == 0] = 0

    out.write(output)

if out: out.release()
camera.release()
cv2.destroyAllWindows()

headdab avatar Nov 30 '17 21:11 headdab

This change fixes the memory leak in the python wrapper:

https://github.com/lgov/bgslibrary/commit/76693035e5db2f26f5c3f264f5811300b0c8c168

I have tested it on hours of video and it certainly solves this particular problem, but I don't know boost::python well know to be sure that it's complete.

lgov avatar Jan 27 '18 21:01 lgov

Thanks!

On Sat, Jan 27, 2018 at 1:29 PM, Lieven Govaerts [email protected] wrote:

This change fixes the memory leak in the python wrapper:

lgov/bgslibrary@7669303 https://github.com/lgov/bgslibrary/commit/76693035e5db2f26f5c3f264f5811300b0c8c168

I have tested it on hours of video and it certainly solves this particular problem, but I don't know boost::python well know to be sure that it's complete.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/andrewssobral/bgslibrary/issues/72#issuecomment-361016660, or mute the thread https://github.com/notifications/unsubscribe-auth/ABJwrBDqOCId5lwF13AVV_g4iU4kRIBbks5tO5UcgaJpZM4Ngd9M .

-- Ben Weinstein, Ph.D. Postdoctoral Fellow Oregon State University http://benweinstein.weebly.com/

bw4sz avatar Jan 27 '18 23:01 bw4sz

Can confirm. No more memory leak. Tested with openCV3 and Python3. Thank you!

mttucl avatar Jan 28 '18 06:01 mttucl

Thank you very much @lgov ! I applied a patch from your branch to fix the memory leak. I let this issue open to check for OpenCV 2.

andrewssobral avatar Jan 28 '18 12:01 andrewssobral

Thank you very much @lgov the device is TX2 with OpenCV3.3 + OpenCV-contrib

jzyztzn avatar Apr 14 '18 18:04 jzyztzn