doulu6160 2019-01-31 08:39
浏览 291
已采纳

垃圾收集不适用于Golang中的[] gocv.Mat

Assume that the img is a 3 channel Mat in the code. There is a memory leak with the code. I guess that the pointers(reference) in the slice "matsplits" are not deleted with garbage-collection in go. How can I fix it?

for{
    matsplits := gocv.Split(img)
    matsplits[0].Close()
    matsplits[1].Close()
    matsplits[2].Close()
}

Kind of above codes cause memory leak. I am sure that the Mat objects in imgarr are closed, but the memory usage is still growing up. Why?

Update: part of codes in my project

processed := 0
for processed < proc.imgNumber {
    grayhconcatImg := <-proc.processedImg[0][chindex]
    var roiList roilist
    var numStartPosList numStartPos
    for x := 0; x < 11520-w; x++ {
        test := gocv.NewMat()
        testRegion := grayhconcatImg.img.Region(image.Rect(x, 0, x+w, h))
        gocv.BitwiseXor(chimg, testRegion, &test)
        testRegion.Close()
        //testsplit := gocv.Split(test)
        test.Close()
        //testsplit[0].Close()
        //testsplit[1].Close()

    processed++
}

The memory leak occurs if "testsplit"'s are unmarked. len(testsplit) is 2. I have checked that testsplit[0] and testsplit[1] have been closed correctly after testsplit[i].Close().

  • 写回答

1条回答 默认 最新

  • duan198811 2019-01-31 09:14
    关注

    After Close(), the memory will be clear partly after the gc comes. Check it like

        matsplits := gocv.Split(img)
        matsplits[0].Close()
        matsplits[1].Close()
        matsplits[2].Close()
        runtime.GC()
    
    

    exec like GODEBUG=gctrace=1 go run main.go 2>xx.log and you can find the what gc actual do in xx.log

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器