1

In os package there is Getpagesize() to get the current os memory page size. Is there any way to get the number of unallocated (free) pages / unallocated memory in bytes and the total (unallocated+allocated)?

Mana Koi
  • 11
  • 3
  • 3
    There's no platform-independent way of doing this. You'll be stuck relying on whatever information you can get from the operating system. – Jonathan Hall Jul 19 '19 at 08:15

1 Answers1

0

Collate some results here for available memory

as per comment it is platform dependent

For Windows

from this answer its possible to call a windows dll

in the answer this line

golang var proc = mod.NewProc("GetPhysicallyInstalledSystemMemory")

using 'GetPhysicallyInstalledSystemMemory' will return the installed ram,

if we change this line to

golang mod.NewProc("GlobalMemoryStatusEx")

'GlobalMemoryStatusEx' in the answers code will return the available memory

then a possible expression for memory used is

installed - available

For Linux

A scan of the file "/proc/meminfo"
the first 3 lines will possibly show tabulated fields with values

MemTotal:    24515792 kB
MemFree:        13053468 kB
MemAvailable:   19627908 kB

then memory usage is MemTotal - MemAvailable

there can be an issue where the MemAvailable field is missing

in that case the following fields will probably exist

Buffers:          349596 kB
Cached:          7306116 kB

possible expression for memory used using the available fields in 'meminfo' would be

MemTotal - MemFree - Buffers - Cached

the issue goes into the roles of the Buffers and cached fields

For Darwin
A similar approach as windows using the command vm_stat

Nigel Savage
  • 991
  • 13
  • 26