helios741 / myblog

觉得好请点小星星,欢迎有问题交流(issue/email)
109 stars 21 forks source link

内存泄漏的问题 复制重试却不一样的结果,求助! #81

Open shcw opened 2 years ago

shcw commented 2 years ago

大佬,我看了这篇帖子 https://github.com/helios741/myblog/tree/new/learn_go/src/2021/08/go_feak_memory_lark 当我也想复现这个问题的时候出现了奇怪的情况, image内存一直不下来。。。

go版本:1.17.8 环境centos7 执行代码如下

package main

import (
    "fmt"
    "github.com/prometheus/client_golang/prometheus"
    "github.com/prometheus/client_golang/prometheus/promhttp"
    log "github.com/sirupsen/logrus"
    "net/http"
    _ "net/http/pprof"
    "time"
)

func allocate(s int) {
    a := make([]byte, s*1024*1024)
    for i := 0; i < len(a); i += 4096 {
        a[i] = 'x'
    }
    fmt.Printf("alloc%c %d\n", a[0], s)
    a = nil
}

func main() {
    allocate(100)
    allocate(200)
    allocate(500)

    go func() {
        // Expose the registered metrics via HTTP.
        http.Handle("/metrics", promhttp.HandlerFor(
            prometheus.DefaultGatherer,
            promhttp.HandlerOpts{
                // Opt into OpenMetrics to support exemplars.
                EnableOpenMetrics: true,
            },
        ))

        err := http.ListenAndServe(":8166", nil)
        if err != nil {
            log.Errorf("ListenAndServe: %+v", err)
        }
    }()

    time.Sleep(300 * time.Second)
}

希望大佬可以解答下