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