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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么