Hello. Good afternoon.
Im using OpenCV.jl and Im facing a memory leak using video capture in this example. May someone help?
julia 1.11.3 Win x64
using OpenCV
cap = OpenCV.VideoCapture(0)
if !OpenCV.isOpened(cap)
error("camera not found")
end
OpenCV.namedWindow("Webcam")
try
while true
ret, frame = OpenCV.read(cap)
if !ret
println("Não foi possível capturar o frame")
break
end
OpenCV.imshow("Webcam", frame)
if OpenCV.waitKey(33) == Int('q')
break
end
end
finally
OpenCV.release(cap)
OpenCV.destroyAllWindows()
end
What do you observe that makes you think you are facing a memory leak? You run the code, see the window with an image from your webcam, and overtime the memory usage grows? Does the amount of used memory increase until you get an out-of-memory error?
I suspect that there is no memory leak and that the GC just hasn’t collected the frames that are no longer referenced. To test that hypothesis – if you put GC.gc() right at the end of the while loop, does that fix it?
Hello, good morning. Yes. Using gc it is collected. What do you mean, is that julia will let memory increase more than 5Gb? I will do the tests e let it run for thirthy minutes
yep. It frozen my machine at 96% total of memory. Its a leak. Using GC. gc() really collects the memory, but, julia dont know how to collect without calling gc explicitly.
What do you mean, is that julia will let memory increase more than 5Gb?
My knowledge about GC is very limited. The documentation says that
The GC will do full collections when the heap size reaches 80% of the maximum allowed size.
I am not sure what the maximum allowed size is, but it is certainly no more than the amount of physical memory, so I am not sure why the GC does not run in your case.
What if you you pass --heap-size-hint=1GB (or another value) to julia when you start your script? That should force garbage collection earlier.
Oh wow, that’s unfortunate. I see that opencv provides Mat::release but it is not available in the wrapper. I guess you are stuck with calling GC.gc() and the occasional delays that come with it.