golang ---获取内存信息

package main

import (
	"fmt"
	"syscall"
	"unsafe"
)

var kernel = syscall.NewLazyDLL("Kernel32.dll")

type memoryStatusEx struct {
	cbSize                  uint32
	dwMemoryLoad            uint32
	ullTotalPhys            uint64 // in bytes
	ullAvailPhys            uint64
	ullTotalPageFile        uint64
	ullAvailPageFile        uint64
	ullTotalVirtual         uint64
	ullAvailVirtual         uint64
	ullAvailExtendedVirtual uint64
}

func main() {

	GlobalMemoryStatusEx := kernel.NewProc("GlobalMemoryStatusEx")
	var memInfo memoryStatusEx
	memInfo.cbSize = uint32(unsafe.Sizeof(memInfo))
	mem, _, _ := GlobalMemoryStatusEx.Call(uintptr(unsafe.Pointer(&memInfo)))
	if mem == 0 {
		return
	}
	fmt.Println("total :=", memInfo.ullTotalPhys)
	fmt.Println("free=:", memInfo.ullAvailPhys)

}

  输出结果:

total := 17054044160

free=: 12900540416

posted on 2019-07-01 14:23  清明-心若淡定  阅读(2901)  评论(0编辑  收藏  举报