dousha7904 2017-12-03 05:25
浏览 83
已采纳

将byte []转换为golang中的字符串奇怪占据堆

I found strange occupy heap when convert byte[] to string with below code

package main

import (
  "bytes"
  "fmt"
  "net/http"
  _ "net/http/pprof"
  "strings"
  "time"
)

var (
  c = make(chan int, 500000)
)

func main() {
  go func() {
    http.ListenAndServe(":8080", nil)
  }()
  f := func(ss []string) {
    fmt.Println(ss)
    time.Sleep(time.Millisecond)
    <-c
  }
  for {
    c <- 1
    bs := bytes.NewBufferString("A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z").Bytes()
    fmt.Println(bs) // will raise memory leak after marked as comment???
    s := string(bs)
    ss := strings.Split(s, ",")
    go f(ss)
  }
}
  1. without fmt.Println(bs) will gradually exhausting memory.

  2. with fmt.Println(bs) work fine. i can't understand what happened? i'm worked with version go1.9.2 darwin/amd64

  • 写回答

1条回答 默认 最新

  • dounan9070 2017-12-03 07:25
    关注

    No, there is no memory leak:
    You are using 500000 concurrent goroutines, you just need to limit (reduce) the number of concurrent goroutines, e.g.:

    c := make(chan int, runtime.NumCPU())
    

    Try this ( and see the end of this edit):

    package main
    
    import (
        "bytes"
        "fmt"
        "runtime"
        "strings"
        "time"
    )
    
    func main() {
        c := make(chan int, runtime.NumCPU())
        for {
            c <- 1
            bs := bytes.NewBufferString("A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z").Bytes()
            s := string(bs)
            ss := strings.Split(s, ",")
            go func(ss []string) {
                fmt.Println(ss)
                time.Sleep(time.Millisecond)
                <-c
            }(ss)
        }
    }
    

    Your Code:

    package main
    
    import (
        "bytes"
        "fmt"
        "net/http"
        _ "net/http/pprof"
        "strings"
        "time"
    )
    
    var (
        c = make(chan int, 500000)
    )
    
    func main() {
        go func() {
            http.ListenAndServe(":8080", nil)
        }()
        f := func(ss []string) {
            fmt.Println(ss)
            time.Sleep(time.Millisecond)
            <-c
        }
        for {
            c <- 1
            bs := bytes.NewBufferString("A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z").Bytes()
            // fmt.Println(bs) // will raise memory leak after marked as comment???
            s := string(bs)
            ss := strings.Split(s, ",")
            go f(ss)
        }
    }
    

    It reaches steady state after a while and even reduced Memory usage:

    // Mem          CPU time:
    // 5,464,208K 0:1:20
    // 5,468,208K 0:2:20
    // 5,469,608K 0:3:20
    // 5,469,844K 0:4:20
    // 5,469,844K 0:5:20
    // 5,469,848K 0:6:20
    // 5,469,848K 0:7:20 fixed
    // 5,469,848K 0:8:20 fixed
    // 5,469,616K 0:9:20 reduced
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?