opencv_contrib icon indicating copy to clipboard operation
opencv_contrib copied to clipboard

Memory leak like situation in Julia Bindings

Open archit120 opened this issue 5 years ago • 3 comments

System information (version)
  • OpenCV => 4.4
  • Operating System / Platform => Ubuntu 18.04
  • Compiler => G++
Detailed description

Tracking issue based on question at https://stackoverflow.com/questions/64978891/how-do-i-fix-a-julia-opencv-cxx-memory-leak-in-image-capturing

A memory leak-like situation can happen in the Julia bindings as the Julia garbage collector is not aware of the actual size of the objects allocated by OpenCV. When the GC does run, objects are freed properly so the only problem is that GC does not run automatically when needed. A temporary fix is to force GC run when necessary using GC.gc()

Steps to reproduce

The following Julia code can cause garbage to keep getting accumulated till the loop ends at which point all the memory is freed at once. A temporary fix is to call GC.gc() inside the loop.

using OpenCV
using Printf
cv = OpenCV

function main( )
    vidDevice="/dev/video0"
    cap = cv.VideoCapture(vidDevice)
    count = 1000
    while (cv.isOpened(cap) && count > 0)
        @printf("Capturing %4d\r", count)
        ret,img = cv.read(cap)
        count = count - 1
        
    end
    cv.release(cap)
    cv.destroyAllWindows()

end

main()
Issue submission checklist
  • [x] I report the issue, it's not a question
  • [x] I checked the problem with documentation, FAQ, open issues, answers.opencv.org, Stack Overflow, etc and have not found solution
  • [x] I updated to latest OpenCV version and the issue is still there
  • [x] There is reproducer code and related data files: videos, images, onnx, etc

archit120 avatar Nov 26 '20 19:11 archit120

The related issue at JuliaInterop/libcxxwrap-julia#73 Should be easy to fix once the issue in libcxxwrap-julia is resolved

archit120 avatar Nov 26 '20 19:11 archit120

BTW, Java (JVM) has the similar problem (unresolved): https://github.com/opencv/opencv/issues/8683#issuecomment-390336737

alalek avatar Nov 26 '20 20:11 alalek

We cant work with camera as this is. It will never be solved '-_-

jgardona avatar Aug 24 '25 12:08 jgardona