drxt70655 2016-08-09 13:29
浏览 78

Golang:json.Decode / Unmarshal之后分析器中有很多堆项

I've got a sample golang script: it gets page response and unmarshalles its body in infinitive loop as a goroutine.

package main


import (
    "time"
    "net/http"
    "encoding/json"
    "fmt"
    _ "net/http/pprof"
)

func init() {
    go http.ListenAndServe(":8087", http.DefaultServeMux)
}


func DoTask()  {
    resp, _ := http.Get("https://www.citibikenyc.com/stations/json")
    defer resp.Body.Close()
    var answerInterface map[string]interface{}
    json.NewDecoder(resp.Body).Decode(&answerInterface)
}



func main() {
    for {
        go func() {
            time.Sleep(1*time.Second)
            DoTask()
            fmt.Println("goroutine finished")
        }()
        time.Sleep(1*time.Second)
    }
}

But when I open profiler the number of heap items is increasing all the time. Example of heap item:

#   0x6b7699    compress/flate.NewReader+0x49           /usr/local/go/src/compress/flate/inflate.go:828
#   0x5f339d    compress/gzip.(*Reader).readHeader+0x7ad    /usr/local/go/src/compress/gzip/gunzip.go:254
#   0x5f1da7    compress/gzip.NewReader+0x207           /usr/local/go/src/compress/gzip/gunzip.go:95
#   0x4d5117    net/http.(*gzipReader).Read+0x97        /usr/local/go/src/net/http/transport.go:1658
#   0x4d4c6a    net/http.(*bodyEOFSignal).Read+0x26a        /usr/local/go/src/net/http/transport.go:1613
#   0x502617    encoding/json.(*Decoder).refill+0x287       /usr/local/go/src/encoding/json/stream.go:152
#   0x5022a3    encoding/json.(*Decoder).readValue+0x413    /usr/local/go/src/encoding/json/stream.go:128
#   0x501ba9    encoding/json.(*Decoder).Decode+0x159               /usr/local/go/src/encoding/json/stream.go:57
#   0x401219    main.DoTask+0x169               /root/test.go:21
#   0x4012f9    main.main.func1+0x29                /root/test.go:30

Couldn't you explain, what is the problem? Thank you anyway

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度