memory

package module
v0.0.0-...-7b4eea6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 28, 2021 License: BSD-3-Clause Imports: 1 Imported by: 185

README

memory

Package memory provides two methods reporting total physical system memory accessible to the kernel, and free memory available to the running application.

This package has no external dependency besides the standard library and default operating system tools.

Documentation: GoDoc

This is useful for dynamic code to minimize thrashing and other contention, similar to the stdlib runtime.NumCPU See some history of the proposal at https://github.com/golang/go/issues/21816

Example

fmt.Printf("Total system memory: %d\n", memory.TotalMemory())
fmt.Printf("Free memory: %d\n", memory.FreeMemory())

Testing

Tested/working on:

  • macOS 10.12.6 (16G29), 10.15.7 (19H2)
  • Windows 10 1511 (10586.1045)
  • Linux RHEL (3.10.0-327.3.1.el7.x86_64)
  • Raspberry Pi 3 (ARMv8) on Raspbian, ODROID-C1+ (ARMv7) on Ubuntu, C.H.I.P (ARMv7).
  • Amazon Linux 2 aarch64 (m6a.large, 4.14.203-156.332.amzn2.aarch64)

Tested on virtual machines:

  • Windows 7 SP1 386
  • Debian stretch 386
  • NetBSD 7.1 amd64 + 386
  • OpenBSD 6.1 amd64 + 386
  • FreeBSD 11.1 amd64 + 386
  • DragonFly BSD 4.8.1 amd64

If you have access to untested systems please test and report success/bugs.

Documentation

Overview

Package memory provides a single method reporting total system memory accessible to the kernel.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FreeMemory

func FreeMemory() uint64

FreeMemory returns the total free system memory in bytes.

The total free memory is installed physical memory size minus reserved areas for other applications running on the same system.

If free memory size could not be determined, then 0 is returned.

Example
package main

import (
	"fmt"

	"github.com/pbnjay/memory"
)

func main() {
	fmt.Printf("Free system memory: %d\n", memory.FreeMemory())
}
Output:

func TotalMemory

func TotalMemory() uint64

TotalMemory returns the total accessible system memory in bytes.

The total accessible memory is installed physical memory size minus reserved areas for the kernel and hardware, if such reservations are reported by the operating system.

If accessible memory size could not be determined, then 0 is returned.

Example
package main

import (
	"fmt"

	"github.com/pbnjay/memory"
)

func main() {
	fmt.Printf("Total system memory: %d\n", memory.TotalMemory())
}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL