dongya2030 2014-09-08 01:12
浏览 22
已采纳

在Go 1.3.1中获取可变的内存使用情况

  1. Why this code can not found the memory usage (m2-m1) and (m4-m3)?
  2. Why (m3-m2) and (m5-m4) require to allocate extra memory?

Full code here

type T struct {
    B uint8
    S string
    I int
}

func memUsage(mOld, mNew *runtime.MemStats) {
    fmt.Println("Alloc:", mNew.Alloc-mOld.Alloc,
        "HeapAlloc:", mNew.HeapAlloc-mOld.HeapAlloc,
        "TotalAlloc:", mNew.TotalAlloc-mOld.TotalAlloc)
}
func main() {
    var m1, m2, m3, m4, m5, m6 runtime.MemStats
    runtime.ReadMemStats(&m1)
    t := T{}
    runtime.ReadMemStats(&m2)
    fmt.Println(t)
    memUsage(&m1, &m2)

    runtime.ReadMemStats(&m3)
    t2 := "abc"
    runtime.ReadMemStats(&m4)
    fmt.Println(t2)
    memUsage(&m3, &m4)

    runtime.ReadMemStats(&m5)
    t3 := map[int]string{1: "x"}
    runtime.ReadMemStats(&m6)
    fmt.Println(t3)
    memUsage(&m5, &m6)

    memUsage(&m2, &m3)
    memUsage(&m4, &m5)
}

Output

{0  0}
Alloc: 0 HeapAlloc: 0 TotalAlloc: 0
abc
Alloc: 0 HeapAlloc: 0 TotalAlloc: 0
map[1:x]
Alloc: 256 HeapAlloc: 256 TotalAlloc: 256
Alloc: 432 HeapAlloc: 432 TotalAlloc: 432
Alloc: 64 HeapAlloc: 64 TotalAlloc: 64
  • 写回答

1条回答 默认 最新

  • douya5194 2014-09-08 01:16
    关注

    Variable t is allocated on the stack.

    package main
    
    import (
        "fmt"
    )
    
    type T struct {
        B uint8
        S string
        I int
    }
    
    func main() {
        t := T{}
        fmt.Println(t)
    }
    

    Pseudo-assembler:

    0x0021 00033 (t.go:14)  LEAQ    "".statictmp_0002+0(SB),BX
    0x0028 00040 (t.go:14)  LEAQ    "".t+88(SP),BP
    0x002d 00045 (t.go:14)  MOVQ    BP,DI
    0x0030 00048 (t.go:14)  MOVQ    BX,SI
    0x0033 00051 (t.go:14)  DUFFCOPY    ,$
    

    References:

    A Manual for the Plan 9 assembler


    You have revised your question substantially. Next time, ask a new question.

    You muddy the waters by your calls to the fmt package. All we can see is that the fmt package does some heap allocations. We would have to trace through hundreds of lines of code in the fmt package to see what is happening. Also, the results are not reproducible.

    I removed all the clutter:

    package main
    
    import (
        "fmt"
        "runtime"
    )
    
    type T struct {
        B uint8
        S string
        I int
    }
    
    func memUsage(mOld, mNew *runtime.MemStats) {
        fmt.Println("Alloc:", mNew.Alloc-mOld.Alloc,
            "HeapAlloc:", mNew.HeapAlloc-mOld.HeapAlloc,
            "TotalAlloc:", mNew.TotalAlloc-mOld.TotalAlloc)
    }
    func main() {
        var m1, m2, m3, m4, m5, m6, m7 runtime.MemStats
        runtime.ReadMemStats(&m1)
        t := T{}
        runtime.ReadMemStats(&m2)
        _ = t
        runtime.ReadMemStats(&m3)
        t2 := "abc"
        runtime.ReadMemStats(&m4)
        _ = t2
        runtime.ReadMemStats(&m5)
        t3 := map[int]string{1: "x"}
        runtime.ReadMemStats(&m6)
        _ = t3
        runtime.ReadMemStats(&m7)
    
        memUsage(&m1, &m2)
        memUsage(&m2, &m3)
        memUsage(&m3, &m4)
        memUsage(&m4, &m5)
        memUsage(&m5, &m6)
        memUsage(&m6, &m7)
    }
    

    Output:

    Alloc: 0 HeapAlloc: 0 TotalAlloc: 0
    Alloc: 0 HeapAlloc: 0 TotalAlloc: 0
    Alloc: 0 HeapAlloc: 0 TotalAlloc: 0
    Alloc: 0 HeapAlloc: 0 TotalAlloc: 0
    Alloc: 256 HeapAlloc: 256 TotalAlloc: 256
    Alloc: 0 HeapAlloc: 0 TotalAlloc: 0
    

    It's easy to see that the stack is used except for creating the map which uses the stack and the heap.

    Note: Go memory management is implementation dependent. It's frequently improved and it's about to be completely rewritten.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站