文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

memstat HeapInuse 和 TotalAlloc 需要解释

2024-04-04 23:53

关注

本篇文章向大家介绍《memstat HeapInuse 和 TotalAlloc 需要解释》,主要包括,具有一定的参考价值,需要的朋友可以参考一下。

问题内容

我想要转储 golang memstat

package main

import (
    "fmt"
    "github.com/dustin/go-humanize"
    "runtime"
    "time"
)

func main() {
    for {
        var mem = new(runtime.memstats)
        runtime.readmemstats(mem)
        fmt.println("heapinuse:", humanize.bytes(mem.heapinuse), "totalalloc:", humanize.bytes(mem.totalalloc))

        allocate()
        time.sleep(100 * time.millisecond)
    }

}

func allocate() {
    //  0.25mb
    _ = make([]byte, int((1<<20)*0.25))
}

当我转储内存信息时

HeapInuse: 418 kB TotalAlloc: 154 kB ? why  HeapInuse bigger than TotalAlloc
HeapInuse: 688 kB TotalAlloc: 417 kB
HeapInuse: 950 kB TotalAlloc: 680 kB
HeapInuse: 1.2 MB TotalAlloc: 942 kB
HeapInuse: 1.5 MB TotalAlloc: 1.2 MB
HeapInuse: 1.8 MB TotalAlloc: 1.5 MB
HeapInuse: 2.0 MB TotalAlloc: 1.7 MB
HeapInuse: 2.3 MB TotalAlloc: 2.0 MB
HeapInuse: 2.5 MB TotalAlloc: 2.3 MB
HeapInuse: 2.8 MB TotalAlloc: 2.5 MB
HeapInuse: 3.1 MB TotalAlloc: 2.8 MB
HeapInuse: 3.3 MB TotalAlloc: 3.0 MB
HeapInuse: 3.6 MB TotalAlloc: 3.3 MB
HeapInuse: 3.9 MB TotalAlloc: 3.6 MB
HeapInuse: 4.1 MB TotalAlloc: 3.8 MB
HeapInuse: 4.4 MB TotalAlloc: 4.1 MB
HeapInuse: 410 kB TotalAlloc: 4.4 MB

我发现 heapinuse 比 totalalloc 大?但我不明白原因?我认为totalalloc应该包括heapinuse。


正确答案


我们来看看下面的官方评论:

总分配

// totalalloc is cumulative bytes allocated for heap objects.
//
// totalalloc increases as heap objects are allocated, but
// unlike alloc and heapalloc, it does not decrease when
// objects are freed.
totalalloc uint64

heapinuse

// heapinuse is bytes in in-use spans.
//
// in-use spans have at least one object in them. these spans
// can only be used for other objects of roughly the same
// size.
//
// heapinuse minus heapalloc estimates the amount of memory
// that has been dedicated to particular size classes, but is
// not currently being used. this is an upper bound on
// fragmentation, but in general this memory can be reused
// efficiently.
heapinuse uint64

让我们尝试一下heapalloc

// heapalloc is bytes of allocated heap objects.
//
// "allocated" heap objects include all reachable objects, as
// well as unreachable objects that the garbage collector has
// not yet freed. specifically, heapalloc increases as heap
// objects are allocated and decreases as the heap is swept
// and unreachable objects are freed. sweeping occurs
// incrementally between gc cycles, so these two processes
// occur simultaneously, and as a result heapalloc tends to
// change smoothly (in contrast with the sawtooth that is
// typical of stop-the-world garbage collectors).
heapalloc uint64

结果

HeapAlloc: 148 kB HeapInuse: 401 kB TotalAlloc: 148 kB
HeapAlloc: 412 kB HeapInuse: 664 kB TotalAlloc: 412 kB
HeapAlloc: 675 kB HeapInuse: 926 kB TotalAlloc: 675 kB
HeapAlloc: 937 kB HeapInuse: 1.2 MB TotalAlloc: 937 kB
HeapAlloc: 1.2 MB HeapInuse: 1.5 MB TotalAlloc: 1.2 MB
HeapAlloc: 1.5 MB HeapInuse: 1.7 MB TotalAlloc: 1.5 MB
HeapAlloc: 1.7 MB HeapInuse: 2.0 MB TotalAlloc: 1.7 MB
HeapAlloc: 2.0 MB HeapInuse: 2.2 MB TotalAlloc: 2.0 MB
HeapAlloc: 2.2 MB HeapInuse: 2.5 MB TotalAlloc: 2.2 MB
HeapAlloc: 2.5 MB HeapInuse: 2.8 MB TotalAlloc: 2.5 MB
HeapAlloc: 2.8 MB HeapInuse: 3.0 MB TotalAlloc: 2.8 MB
HeapAlloc: 3.0 MB HeapInuse: 3.3 MB TotalAlloc: 3.0 MB
HeapAlloc: 3.3 MB HeapInuse: 3.6 MB TotalAlloc: 3.3 MB
HeapAlloc: 3.6 MB HeapInuse: 3.8 MB TotalAlloc: 3.6 MB
HeapAlloc: 3.8 MB HeapInuse: 4.1 MB TotalAlloc: 3.8 MB
HeapAlloc: 4.1 MB HeapInuse: 4.3 MB TotalAlloc: 4.1 MB
HeapAlloc: 134 kB HeapInuse: 393 kB TotalAlloc: 4.4 MB
HeapAlloc: 398 kB HeapInuse: 664 kB TotalAlloc: 4.6 MB
HeapAlloc: 660 kB HeapInuse: 926 kB TotalAlloc: 4.9 MB
HeapAlloc: 923 kB HeapInuse: 1.2 MB TotalAlloc: 5.1 MB
HeapAlloc: 1.2 MB HeapInuse: 1.4 MB TotalAlloc: 5.4 MB
HeapAlloc: 1.4 MB HeapInuse: 1.7 MB TotalAlloc: 5.7 MB
HeapAlloc: 1.7 MB HeapInuse: 2.0 MB TotalAlloc: 5.9 MB
HeapAlloc: 2.0 MB HeapInuse: 2.2 MB TotalAlloc: 6.2 MB
HeapAlloc: 2.2 MB HeapInuse: 2.5 MB TotalAlloc: 6.5 MB
HeapAlloc: 2.5 MB HeapInuse: 2.8 MB TotalAlloc: 6.7 MB
HeapAlloc: 2.8 MB HeapInuse: 3.0 MB TotalAlloc: 7.0 MB
HeapAlloc: 3.0 MB HeapInuse: 3.3 MB TotalAlloc: 7.2 MB
HeapAlloc: 3.3 MB HeapInuse: 3.5 MB TotalAlloc: 7.5 MB
HeapAlloc: 3.5 MB HeapInuse: 3.8 MB TotalAlloc: 7.8 MB
HeapAlloc: 3.8 MB HeapInuse: 4.1 MB TotalAlloc: 8.0 MB
HeapAlloc: 4.1 MB HeapInuse: 4.3 MB TotalAlloc: 8.3 MB
HeapAlloc: 136 kB HeapInuse: 401 kB TotalAlloc: 8.6 MB
HeapAlloc: 400 kB HeapInuse: 664 kB TotalAlloc: 8.8 MB
HeapAlloc: 662 kB HeapInuse: 926 kB TotalAlloc: 9.1 MB
HeapAlloc: 924 kB HeapInuse: 1.2 MB TotalAlloc: 9.3 MB
HeapAlloc: 1.2 MB HeapInuse: 1.4 MB TotalAlloc: 9.6 MB
HeapAlloc: 1.4 MB HeapInuse: 1.7 MB TotalAlloc: 9.9 MB
HeapAlloc: 1.7 MB HeapInuse: 2.0 MB TotalAlloc: 10 MB
HeapAlloc: 2.0 MB HeapInuse: 2.2 MB TotalAlloc: 10 MB
HeapAlloc: 2.2 MB HeapInuse: 2.5 MB TotalAlloc: 11 MB
HeapAlloc: 2.5 MB HeapInuse: 2.8 MB TotalAlloc: 11 MB
HeapAlloc: 2.8 MB HeapInuse: 3.0 MB TotalAlloc: 11 MB
HeapAlloc: 3.0 MB HeapInuse: 3.3 MB TotalAlloc: 11 MB
HeapAlloc: 3.3 MB HeapInuse: 3.5 MB TotalAlloc: 12 MB
HeapAlloc: 3.5 MB HeapInuse: 3.8 MB TotalAlloc: 12 MB
HeapAlloc: 3.8 MB HeapInuse: 4.1 MB TotalAlloc: 12 MB
HeapAlloc: 4.1 MB HeapInuse: 4.3 MB TotalAlloc: 12 MB
HeapAlloc: 136 kB HeapInuse: 401 kB TotalAlloc: 13 MB
HeapAlloc: 400 kB HeapInuse: 664 kB TotalAlloc: 13 MB
HeapAlloc: 663 kB HeapInuse: 926 kB TotalAlloc: 13 MB
HeapAlloc: 925 kB HeapInuse: 1.2 MB TotalAlloc: 14 MB
HeapAlloc: 1.2 MB HeapInuse: 1.4 MB TotalAlloc: 14 MB
HeapAlloc: 1.4 MB HeapInuse: 1.7 MB TotalAlloc: 14 MB

今天关于《memstat HeapInuse 和 TotalAlloc 需要解释》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注编程网公众号!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯