go获取内存监控

1. runtime.MemStats包含字段解析

type MemStats struct {
        // 已分配的对象的字节数.
        //
        // 和HeapAlloc相同.
        Alloc uint64
        // 分配的字节数累积之和.
        //
        // 所以对象释放的时候这个值不会减少.
        TotalAlloc uint64
        // 从操作系统获得的内存总数.
        //
        // Sys是下面的XXXSys字段的数值的和, 是为堆、栈、其它内部数据保留的虚拟内存空间. 
        // 注意虚拟内存空间和物理内存的区别.
        Sys uint64
        // 运行时地址查找的次数,主要用在运行时内部调试上.
        Lookups uint64
        // 堆对象分配的次数累积和.
        // 活动对象的数量等于`Mallocs - Frees`.
        Mallocs uint64
        // 释放的对象数.
        Frees uint64
        // 分配的堆对象的字节数.
        //
        // 包括所有可访问的对象以及还未被垃圾回收的不可访问的对象.
        // 所以这个值是变化的,分配对象时会增加,垃圾回收对象时会减少.
        HeapAlloc uint64
        // 从操作系统获得的堆内存大小.
        //
        // 虚拟内存空间为堆保留的大小,包括还没有被使用的.
        // HeapSys 可被估算为堆已有的最大尺寸.
        HeapSys uint64
        // HeapIdle是idle(未被使用的) span中的字节数.
        //
        // Idle span是指没有任何对象的span,这些span **可以**返还给操作系统,或者它们可以被重用,
        // 或者它们可以用做栈内存.
        //
        // HeapIdle 减去 HeapReleased 的值可以当作"可以返回到操作系统但由运行时保留的内存量".
        // 以便在不向操作系统请求更多内存的情况下增加堆,也就是运行时的"小金库".
        //
        // 如果这个差值明显比堆的大小大很多,说明最近在活动堆的上有一次尖峰.
        HeapIdle uint64
        // 正在使用的span的字节大小.
        //
        // 正在使用的span是值它至少包含一个对象在其中.
        // HeapInuse 减去 HeapAlloc的值是为特殊大小保留的内存,但是当前还没有被使用.
        HeapInuse uint64
        // HeapReleased 是返还给操作系统的物理内存的字节数.
        //
        // 它统计了从idle span中返还给操作系统,没有被重新获取的内存大小.
        HeapReleased uint64
        // HeapObjects 实时统计的分配的堆对象的数量,类似HeapAlloc.
        HeapObjects uint64
        // 栈span使用的字节数。
        // 正在使用的栈span是指至少有一个栈在其中.
        //
        // 注意并没有idle的栈span,因为未使用的栈span会被返还给堆(HeapIdle).
        StackInuse uint64
        // 从操作系统取得的栈内存大小.
        // 等于StackInuse 再加上为操作系统线程栈获得的内存.
        StackSys uint64
        // 分配的mspan数据结构的字节数.
        MSpanInuse uint64
        // 从操作系统为mspan获取的内存字节数.
        MSpanSys uint64
        // 分配的mcache数据结构的字节数.
        MCacheInuse uint64
        // 从操作系统为mcache获取的内存字节数.
        MCacheSys uint64
        // 在profiling bucket hash tables中的内存字节数.
        BuckHashSys uint64
        // 垃圾回收元数据使用的内存字节数.
        GCSys uint64 // Go 1.2
        // off-heap的杂项内存字节数.
        OtherSys uint64 // Go 1.2
        // 下一次垃圾回收的目标大小,保证 HeapAlloc ≤ NextGC.
        // 基于当前可访问的数据和GOGC的值计算而得.
        NextGC uint64
        // 上一次垃圾回收的时间.
        LastGC uint64
        // 自程序开始 STW 暂停的累积纳秒数.
        // STW的时候除了垃圾回收器之外所有的goroutine都会暂停.
        PauseTotalNs uint64
        // 一个循环buffer,用来记录最近的256个GC STW的暂停时间.
        PauseNs [256]uint64
        // 最近256个GC暂停截止的时间.
        PauseEnd [256]uint64 // Go 1.4
        // GC的总次数.
        NumGC uint32
        // 强制GC的次数.
        NumForcedGC uint32 // Go 1.8
        // 自程序启动后由GC占用的CPU可用时间,数值在 0 到 1 之间.
        // 0代表GC没有消耗程序的CPU. GOMAXPROCS * 程序运行时间等于程序的CPU可用时间.
        GCCPUFraction float64 // Go 1.5
        // 是否允许GC.
        EnableGC bool
        // 未使用.
        DebugGC bool
        // 按照大小进行的内存分配的统计,具体可以看Go内存分配的文章介绍.
        BySize [61]struct {
                // Size is the maximum byte size of an object in this
                // size class.
                Size uint32
                // Mallocs is the cumulative count of heap objects
                // allocated in this size class. The cumulative bytes
                // of allocation is Size*Mallocs. The number of live
                // objects in this size class is Mallocs - Frees.
                Mallocs uint64
                // Frees is the cumulative count of heap objects freed
                // in this size class.
                Frees uint64
        }
}

2. 其他监控方法

import (
	"github.com/shirou/gopsutil/cpu"
	"github.com/shirou/gopsutil/disk"
	"github.com/shirou/gopsutil/mem"
	"runtime"
	"time"
)

const (
	B  = 1
	KB = 1024 * B
	MB = 1024 * KB
	GB = 1024 * MB
)

type Server struct {
	Os   Os   `json:"os"`
	Cpu  Cpu  `json:"cpu"`
	Ram  Ram  `json:"ram"`
	Disk Disk `json:"disk"`
}

type Os struct {
	GOOS      string `json:"goos"`
	NumCPU    int    `json:"numCpu"`
	Compiler  string `json:"compiler"`
	GoVersion string `json:"goVersion"`
}

type Cpu struct {
	Cpus  []float64 `json:"cpus"`
	Cores int       `json:"cores"`
}

type Ram struct {
	UsedMB  int `json:"usedMb"`
	TotalMB int `json:"totalMb"`
}

type Disk struct {
	UsedMB  int `json:"usedMb"`
	UsedGB  int `json:"usedGb"`
	TotalMB int `json:"totalMb"`
	TotalGB int `json:"totalGb"`
}

//@function: GetOS
//@description: OS信息
//@return: o Os, err error

func GetOS() (o Os) {
	o.GOOS = runtime.GOOS
	o.NumCPU = runtime.NumCPU()
	o.Compiler = runtime.Compiler
	o.GoVersion = runtime.Version()
	return o
}

//@function: GetCPU
//@description: CPU信息
//@return: c Cpu, err error

func GetCPU() (c Cpu, err error) {
	if cores, err := cpu.Counts(false); err != nil {
		return c, err
	} else {
		c.Cores = cores
	}
	if cpus, err := cpu.Percent(time.Duration(200)*time.Millisecond, true); err != nil {
		return c, err
	} else {
		c.Cpus = cpus
	}
	return c, nil
}

//@function: InitRAM
//@description: RAM信息
//@return: r Ram, err error

func GetRAM() (r Ram, err error) {
	if u, err := mem.VirtualMemory(); err != nil {
		return r, err
	} else {
		r.UsedMB = int(u.Used) / MB
		r.TotalMB = int(u.Total) / MB
	}
	return r, nil
}

//@function: InitDisk
//@description: 硬盘信息
//@return: d Disk, err error

func GetDisk() (d Disk, err error) {
	if u, err := disk.Usage("/"); err != nil {
		return d, err
	} else {
		d.UsedMB = int(u.Used) / MB
		d.UsedGB = int(u.Used) / GB
		d.TotalMB = int(u.Total) / MB
		d.TotalGB = int(u.Total) / GB
	}
	return d, nil
}

3. 测试

func TestMem(t *testing.T) {
	var (
		err error
		o   Os
		c   Cpu
		r   Ram
		d   Disk
	)
	ms := new(runtime.MemStats)
	runtime.ReadMemStats(ms)
	fmt.Printf("堆空间分配的字节数%dm\n", ms.Alloc/MB)
	fmt.Printf("从系统获取到的总内存%dm\n", ms.Sys/MB)
	fmt.Printf("堆空闲(申请未使用)%dm\n", ms.HeapIdle/MB)
	fmt.Printf("使用中堆内存%dm\n", ms.HeapInuse/MB)
	fmt.Printf("系统分配作为运行栈的内存%dm\n", ms.StackSys/MB)
	fmt.Printf("正在使用栈内存%dm\n", ms.StackInuse/MB)
	fmt.Printf("活动的对象%d\n", ms.Mallocs-ms.Frees)
	fmt.Printf("GC调用次数%d\n", ms.NumGC)
	fmt.Printf("当前goroutines数量%d\n", runtime.NumGoroutine())
	// 获取本机Os情况
	o = GetOS()
	fmt.Printf("GOOS:%s Compiler:%s,cpu count:%d,go version:%s\n", o.GOOS, o.Compiler, o.NumCPU, o.GoVersion)
	// 获取cpu情况
	c, err = GetCPU()
	fmt.Printf("Cores:%d,Cpus:%v\n", c.Cores, c.Cpus)
	// 获取机器内存信息
	r, err = GetRAM()
	fmt.Printf("已经使用:%d,总内存:%d\n", r.UsedMB, r.TotalMB)
	// 获取机器磁盘信息
	d, err = GetDisk()
	fmt.Printf("使用磁盘:%d,总磁盘:%d\n", d.UsedMB, d.TotalMB)
	if err != nil {

	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值