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.

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

报告相同问题?

悬赏问题

  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型