登录
首页 >  Golang >  Go问答

For range 循环变量通过引用传递给 go 例程会导致内存泄漏

来源:stackoverflow

时间:2024-03-07 08:42:26 352浏览 收藏

亲爱的编程学习爱好者,如果你点开了这篇文章,说明你对《For range 循环变量通过引用传递给 go 例程会导致内存泄漏》很感兴趣。本篇文章就来给大家详细解析一下,主要介绍一下,希望所有认真读完的童鞋们,都有实质性的提高。

问题内容

重现问题的代码

go版本go1.19 darwin/amd64

package main

import (
    "bytes"
    "log"
    "net/http"
    _ "net/http/pprof"
    "runtime"
    "strconv"
    "sync"
)

type teststruct struct {
    m []byte
}

var lkmap sync.mutex
var tmap map[string]*teststruct

const objectcount = 1024 * 100

func main() {

    tmap = make(map[string]*teststruct)

    wg := &sync.waitgroup{}
    wg.add(objectcount)
    func1(wg)
    wg.add(objectcount)
    func2(wg)

    wg.wait()
    runtime.gc()
    log.println(http.listenandserve("localhost:6060", nil))
}

//go:noinline
func func1(wg *sync.waitgroup) {
    ts := []*teststruct{}
    for i := 0; i < objectcount; i++ {
        t := &teststruct{}
        ts = append(ts, t)
        lkmap.lock()
        tmap[strconv.itoa(i)] = t
        lkmap.unlock()
    }

    for i, t := range ts {
        go func(t *teststruct, idx int) {
            t.m = bytes.repeat([]byte{byte(32)}, 1024)

            lkmap.lock()
            delete(tmap, strconv.itoa(idx))
            lkmap.unlock()
            wg.done()
        }(t, i) //pass by reference
    }
}

//go:noinline
func func2(wg *sync.waitgroup) {
    ts := []*teststruct{}
    for i := 0; i < objectcount; i++ {
        t := &teststruct{}
        ts = append(ts, t)

        lkmap.lock()
        tmap[strconv.itoa(i+objectcount)] = t
        lkmap.unlock()
    }
    for i, t := range ts {
        tmp := t //capture here
        idx := i
        go func() {
            tmp.m = bytes.repeat([]byte{byte(32)}, 1024)

            lkmap.lock()
            delete(tmap, strconv.itoa(idx+objectcount))
            lkmap.unlock()
            wg.done()
        }()
    }
}


运行此程序并使用 pprof 通过命令行获取堆:

go tool pprof http://localhost:6060/debug/pprof/heap

将生成如下图:

正如我所见,func1 明显泄漏。 func1 和 func2 之间的唯一区别是 func2 使用局部变量来捕获循环变量。

但是,如果我删除全局地图,两个功能都很好,没有人泄漏。

那么全局地图操作有什么关系吗?

感谢您的任何意见

编辑: 感谢@nipuna 指出,更改 main 函数中 func1 和 func2 的顺序将使 func2 显示在报告中。然而,func1 仍然占用最多的内存,即 51.32%,而 func2 则保留 19.33%


正确答案


我不认为分析器图是内存泄漏试验中的任何强有力的证据。

go 运行时可以比分析器更多地了解其内存分配。尝试使用 runtime.ReadMemStats 输出堆读数。

这是我的实验:https://go.dev/play/p/279du2yB3BZ

func printallocinfo(message string) {
    var memstats runtime.memstats
    runtime.readmemstats(&memstats)
    fmt.println(message,
        "heap alloc:", memstats.heapalloc,
        "heap objects:", memstats.heapobjects)
}

func main() {

    tmap = make(map[string]*teststruct)

    wg := &sync.waitgroup{}

    for i := 0; i < 10; i++ {
        wg.add(objectcount)
        printallocinfo("before func2:")
        func2(wg)
        wg.wait()
        runtime.gc()
        printallocinfo("after func2: ")
    }
    println("----------------------------")
    for i := 0; i < 10; i++ {
        wg.add(objectcount)
        printallocinfo("before func1:")
        func1(wg)
        wg.wait()
        runtime.gc()
        printallocinfo("after func1: ")
    }
    println("----------------------------")
    runtime.gc()
    printallocinfo("after gc: ")
    println("----------------------------")
}

输出为

before func1: heap alloc: 260880 heap objects: 1224
after func1:  heap alloc: 1317904 heap objects: 2749
before func1: heap alloc: 1319288 heap objects: 2757
after func1:  heap alloc: 1576656 heap objects: 3425
before func1: heap alloc: 1578040 heap objects: 3433
after func1:  heap alloc: 1858408 heap objects: 4197
before func1: heap alloc: 1859792 heap objects: 4205
after func1:  heap alloc: 2210760 heap objects: 4909
before func1: heap alloc: 2212144 heap objects: 4917
after func1:  heap alloc: 2213200 heap objects: 4799
before func1: heap alloc: 2214584 heap objects: 4807
after func1:  heap alloc: 2247480 heap objects: 5045
before func1: heap alloc: 2248864 heap objects: 5053
after func1:  heap alloc: 2298840 heap objects: 5158
before func1: heap alloc: 2300224 heap objects: 5166
after func1:  heap alloc: 2297144 heap objects: 5047
before func1: heap alloc: 2298528 heap objects: 5055
after func1:  heap alloc: 2316712 heap objects: 5154
before func1: heap alloc: 2318096 heap objects: 5162
after func1:  heap alloc: 3083256 heap objects: 7208
----------------------------
before func2: heap alloc: 3084640 heap objects: 7216
after func2:  heap alloc: 3075816 heap objects: 7057
before func2: heap alloc: 3077200 heap objects: 7065
after func2:  heap alloc: 3102896 heap objects: 7270
before func2: heap alloc: 3104280 heap objects: 7278
after func2:  heap alloc: 3127728 heap objects: 7461
before func2: heap alloc: 3129112 heap objects: 7469
after func2:  heap alloc: 3128352 heap objects: 7401
before func2: heap alloc: 3129736 heap objects: 7409
after func2:  heap alloc: 3121152 heap objects: 7263
before func2: heap alloc: 3122536 heap objects: 7271
after func2:  heap alloc: 3167872 heap objects: 7703
before func2: heap alloc: 3169256 heap objects: 7711
after func2:  heap alloc: 3163120 heap objects: 7595
before func2: heap alloc: 3164504 heap objects: 7603
after func2:  heap alloc: 3157376 heap objects: 7470
before func2: heap alloc: 3158760 heap objects: 7478
after func2:  heap alloc: 3160496 heap objects: 7450
before func2: heap alloc: 3161880 heap objects: 7458
after func2:  heap alloc: 3221024 heap objects: 7754
----------------------------
after gc:  heap alloc: 3222200 heap objects: 7750
----------------------------

看起来你是对的 - 每次迭代时堆中的对象数量都在不断增长。

但是如果我们交换 func1func2 会怎样?让我们在 func1 之前调用 func2https://go.dev/play/p/ABFq1O11bIl

Before func2: Heap Alloc: 261456 Heap objects: 1227
After func2:  Heap Alloc: 1330584 Heap objects: 2529
Before func2: Heap Alloc: 1331968 Heap objects: 2537
After func2:  Heap Alloc: 1739280 Heap objects: 3755
Before func2: Heap Alloc: 1740664 Heap objects: 3763
After func2:  Heap Alloc: 2251824 Heap objects: 5017
Before func2: Heap Alloc: 2253208 Heap objects: 5025
After func2:  Heap Alloc: 2244400 Heap objects: 4802
Before func2: Heap Alloc: 2245784 Heap objects: 4810
After func2:  Heap Alloc: 2287816 Heap objects: 5135
Before func2: Heap Alloc: 2289200 Heap objects: 5143
After func2:  Heap Alloc: 2726136 Heap objects: 6375
Before func2: Heap Alloc: 2727520 Heap objects: 6383
After func2:  Heap Alloc: 2834520 Heap objects: 6276
Before func2: Heap Alloc: 2835904 Heap objects: 6284
After func2:  Heap Alloc: 2855496 Heap objects: 6393
Before func2: Heap Alloc: 2856880 Heap objects: 6401
After func2:  Heap Alloc: 2873064 Heap objects: 6478
Before func2: Heap Alloc: 2874448 Heap objects: 6486
After func2:  Heap Alloc: 2923560 Heap objects: 6913
----------------------------
Before func1: Heap Alloc: 2924944 Heap objects: 6921
After func1:  Heap Alloc: 2933416 Heap objects: 6934
Before func1: Heap Alloc: 2934800 Heap objects: 6942
After func1:  Heap Alloc: 2916520 Heap objects: 6676
Before func1: Heap Alloc: 2917904 Heap objects: 6684
After func1:  Heap Alloc: 2941816 Heap objects: 6864
Before func1: Heap Alloc: 2943200 Heap objects: 6872
After func1:  Heap Alloc: 2968184 Heap objects: 7078
Before func1: Heap Alloc: 2969568 Heap objects: 7086
After func1:  Heap Alloc: 2955056 Heap objects: 6885
Before func1: Heap Alloc: 2956440 Heap objects: 6893
After func1:  Heap Alloc: 2961056 Heap objects: 6893
Before func1: Heap Alloc: 2962440 Heap objects: 6901
After func1:  Heap Alloc: 2967680 Heap objects: 6903
Before func1: Heap Alloc: 2969064 Heap objects: 6911
After func1:  Heap Alloc: 3005856 Heap objects: 7266
Before func1: Heap Alloc: 3007240 Heap objects: 7274
After func1:  Heap Alloc: 3033696 Heap objects: 7514
Before func1: Heap Alloc: 3035080 Heap objects: 7522
After func1:  Heap Alloc: 3028432 Heap objects: 7423
----------------------------
after GC:  Heap Alloc: 3029608 Heap objects: 7419
----------------------------

func2 堆的增长速度几乎与 func1 一样快,而位于第二位的 func1 的行为与 func2 完全相同 - 仅向堆添加 500 个对象。

从这两个例子来看,我不同意 func1 正在泄漏,而 func2 没有泄漏。我想说,它们的泄漏程度相同。

终于介绍完啦!小伙伴们,这篇关于《For range 循环变量通过引用传递给 go 例程会导致内存泄漏》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>