# Julia Opencv memory leak

**URL:** https://discourse.julialang.org/t/julia-opencv-memory-leak/125839
**Category:** General Usage
**Tags:** opencv
**Created:** [February 12, 2025, 6:38pm UTC](https://discourse.julialang.org/t/julia-opencv-memory-leak/125839 "2025-02-12T18:38:02Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)
#### Post date: [February 12, 2025, 6:38pm UTC](https://discourse.julialang.org/t/julia-opencv-memory-leak/125839/1 "2025-02-12T18:38:02Z")

</div>

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

![image](https://global.discourse-cdn.com/julialang/optimized/3X/5/8/58b55c0263ac75f1af70a9e9983d9648827c3548_2_690x37.png)

```julia
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

```

---

<div class="post-metadata">

### Author: ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)
#### Post date: [February 13, 2025, 9:14am UTC](https://discourse.julialang.org/t/julia-opencv-memory-leak/125839/2 "2025-02-13T09:14:19Z")

</div>

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 `frame`s 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?

---

<div class="post-metadata">

### Author: ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)
#### Post date: [February 13, 2025, 1:40pm UTC](https://discourse.julialang.org/t/julia-opencv-memory-leak/125839/3 "2025-02-13T13:40:05Z")

</div>

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

---

<div class="post-metadata">

### Author: ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)
#### Post date: [February 13, 2025, 1:58pm UTC](https://discourse.julialang.org/t/julia-opencv-memory-leak/125839/4 "2025-02-13T13:58:50Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)
#### Post date: [February 13, 2025, 2:06pm UTC](https://discourse.julialang.org/t/julia-opencv-memory-leak/125839/5 "2025-02-13T14:06:49Z")

</div>

> What do you mean, is that julia will let memory increase more than 5Gb?

My knowledge about GC is very limited. The [documentation](https://docs.julialang.org/en/v1/devdocs/gc/#Heuristics) 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.

---

<div class="post-metadata">

### Author: ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)
#### Post date: [February 13, 2025, 2:07pm UTC](https://discourse.julialang.org/t/julia-opencv-memory-leak/125839/6 "2025-02-13T14:07:43Z")

</div>

> [@jcbritobr](#):
>
> 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.

I will try this test

---

<div class="post-metadata">

### Author: ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)
#### Post date: [February 13, 2025, 2:14pm UTC](https://discourse.julialang.org/t/julia-opencv-memory-leak/125839/7 "2025-02-13T14:14:25Z")

</div>

Dont stop

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/1/71ce20a3f7bd178276f5e64161f3e6614b41b55d.png)

According this, its an old issue

> <https://github.com/opencv/opencv_contrib/issues/2766>
>
> \##### System information (version)
> 
> 
> \- OpenCV =\> 4.4
> \- Operating System / Pl…atform =\> 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

---

<div class="post-metadata">

### Author: ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)
#### Post date: [February 13, 2025, 3:36pm UTC](https://discourse.julialang.org/t/julia-opencv-memory-leak/125839/8 "2025-02-13T15:36:05Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)
#### Post date: [February 13, 2025, 6:11pm UTC](https://discourse.julialang.org/t/julia-opencv-memory-leak/125839/9 "2025-02-13T18:11:31Z")

</div>

Yep. If I call it every time, will make the program perform very bad. Buts ok. I can do the job only using static images for a while.
