Skip to main content

Overview

Gopsutil provides platform-specific extended memory information beyond the standard VirtualMemoryStat struct. These extensions offer deeper insights into memory management on Linux and Windows systems.

Linux Extended Memory

Linux systems have access to additional memory statistics through the ExLinux interface.

ExVirtualMemory Struct

type ExVirtualMemory struct {
    ActiveFile   uint64 `json:"activefile"`
    InactiveFile uint64 `json:"inactivefile"`
    ActiveAnon   uint64 `json:"activeanon"`
    InactiveAnon uint64 `json:"inactiveanon"`
    Unevictable  uint64 `json:"unevictable"`
    Percpu       uint64 `json:"percpu"`
    KernelStack  uint64 `json:"kernelstack"`
}

Fields

ActiveFile

  • Type: uint64
  • Description: File-backed pages that are actively used (hot file cache)
  • Source: /proc/meminfo Active(file)
  • Use case: Pages recently accessed from files, likely to be accessed again

InactiveFile

  • Type: uint64
  • Description: File-backed pages that are not actively used (cold file cache)
  • Source: /proc/meminfo Inactive(file)
  • Use case: Pages that can be reclaimed when memory pressure occurs

ActiveAnon

  • Type: uint64
  • Description: Anonymous pages that are actively used (process memory, not file-backed)
  • Source: /proc/meminfo Active(anon)
  • Use case: Recently used process heap, stack, and anonymous mmap regions

InactiveAnon

  • Type: uint64
  • Description: Anonymous pages that are not actively used
  • Source: /proc/meminfo Inactive(anon)
  • Use case: Process memory that hasn’t been accessed recently, candidates for swapping

Unevictable

  • Type: uint64
  • Description: Memory that cannot be evicted (mlock’d pages, ramdisks)
  • Source: /proc/meminfo Unevictable
  • Use case: Pages locked in memory by mlock(), SHM_LOCK, or other mechanisms

Percpu

  • Type: uint64
  • Description: Memory allocated for per-CPU structures
  • Source: /proc/meminfo Percpu
  • Use case: Kernel per-CPU data structures for performance optimization

KernelStack

  • Type: uint64
  • Description: Memory used by kernel stacks
  • Source: /proc/meminfo KernelStack
  • Use case: Stack space for kernel threads and syscall execution

Methods

String

func (v ExVirtualMemory) String() string
Returns a JSON string representation of the struct.

Usage

The ExLinux type provides access to extended Linux memory statistics:
package main

import (
    "fmt"
    "log"
    "runtime"
    
    "github.com/shirou/gopsutil/v4/mem"
)

func main() {
    // Only available on Linux
    if runtime.GOOS != "linux" {
        log.Fatal("This example requires Linux")
    }

    // Create ExLinux instance
    ex := mem.NewExLinux()
    
    // Get extended memory statistics
    exMem, err := ex.VirtualMemory()
    if err != nil {
        log.Fatal(err)
    }

    // Display extended statistics
    fmt.Printf("Extended Linux Memory Statistics:\n")
    fmt.Printf("  Active File: %v MB\n", exMem.ActiveFile/1024/1024)
    fmt.Printf("  Inactive File: %v MB\n", exMem.InactiveFile/1024/1024)
    fmt.Printf("  Active Anon: %v MB\n", exMem.ActiveAnon/1024/1024)
    fmt.Printf("  Inactive Anon: %v MB\n", exMem.InactiveAnon/1024/1024)
    fmt.Printf("  Unevictable: %v MB\n", exMem.Unevictable/1024/1024)
    fmt.Printf("  Per-CPU: %v KB\n", exMem.Percpu/1024)
    fmt.Printf("  Kernel Stack: %v KB\n", exMem.KernelStack/1024)

    // Calculate total reclaimable memory
    reclaimable := exMem.InactiveFile + exMem.InactiveAnon
    fmt.Printf("\nReclaimable Memory: %v MB\n", reclaimable/1024/1024)
    
    // JSON output
    fmt.Printf("\nJSON: %s\n", exMem.String())
}

With Context

func (ex *ExLinux) VirtualMemoryWithContext(ctx context.Context) (*ExVirtualMemory, error)
Context-aware version for cancellation and timeout support:
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

ex := mem.NewExLinux()
exMem, err := ex.VirtualMemoryWithContext(ctx)
if err != nil {
    log.Fatal(err)
}

Windows Extended Memory

Windows systems have access to additional memory information through the ExWindows interface.

ExVirtualMemory Struct

type ExVirtualMemory struct {
    CommitLimit   uint64 `json:"commitLimit"`
    CommitTotal   uint64 `json:"commitTotal"`
    VirtualTotal  uint64 `json:"virtualTotal"`
    VirtualAvail  uint64 `json:"virtualAvail"`
    PhysTotal     uint64 `json:"physTotal"`
    PhysAvail     uint64 `json:"physAvail"`
    PageFileTotal uint64 `json:"pageFileTotal"`
    PageFileAvail uint64 `json:"pageFileAvail"`
}

Fields

CommitLimit

  • Type: uint64
  • Description: Maximum amount of memory the system can commit (physical RAM + page files)
  • Source: Windows API GetPerformanceInfo - commitLimit * pageSize
  • Reference: PERFORMANCE_INFORMATION structure

CommitTotal

  • Type: uint64
  • Description: Amount of memory currently committed by the system
  • Source: Windows API GetPerformanceInfo - commitTotal * pageSize
  • Note: Includes both physical RAM and page file usage

VirtualTotal

  • Type: uint64
  • Description: Total size of user-mode virtual address space
  • Source: Windows API GlobalMemoryStatusEx - ullTotalVirtual
  • Reference: MEMORYSTATUSEX structure
  • Typical value: 2 GB on 32-bit, 8 TB on 64-bit Windows

VirtualAvail

  • Type: uint64
  • Description: Available size of user-mode virtual address space
  • Source: Windows API GlobalMemoryStatusEx - ullAvailVirtual
  • Use case: Unreserved and uncommitted memory in the user-mode address space

PhysTotal

  • Type: uint64
  • Description: Total physical RAM installed
  • Source: Windows API GlobalMemoryStatusEx - ullTotalPhys

PhysAvail

  • Type: uint64
  • Description: Available physical RAM
  • Source: Windows API GlobalMemoryStatusEx - ullAvailPhys

PageFileTotal

  • Type: uint64
  • Description: Total size of page file (swap)
  • Source: Windows API GlobalMemoryStatusEx - ullTotalPageFile
  • Note: Includes physical RAM + page file total

PageFileAvail

  • Type: uint64
  • Description: Available page file space
  • Source: Windows API GlobalMemoryStatusEx - ullAvailPageFile

Usage

The ExWindows type provides access to extended Windows memory statistics:
package main

import (
    "fmt"
    "log"
    "runtime"
    
    "github.com/shirou/gopsutil/v4/mem"
)

func main() {
    // Only available on Windows
    if runtime.GOOS != "windows" {
        log.Fatal("This example requires Windows")
    }

    // Create ExWindows instance
    ex := mem.NewExWindows()
    
    // Get extended memory statistics
    exMem, err := ex.VirtualMemory()
    if err != nil {
        log.Fatal(err)
    }

    // Display extended statistics
    fmt.Printf("Extended Windows Memory Statistics:\n")
    fmt.Printf("  Physical Total: %v GB\n", exMem.PhysTotal/1024/1024/1024)
    fmt.Printf("  Physical Available: %v GB\n", exMem.PhysAvail/1024/1024/1024)
    fmt.Printf("  Page File Total: %v GB\n", exMem.PageFileTotal/1024/1024/1024)
    fmt.Printf("  Page File Available: %v GB\n", exMem.PageFileAvail/1024/1024/1024)
    fmt.Printf("  Virtual Address Space Total: %v TB\n", exMem.VirtualTotal/1024/1024/1024/1024)
    fmt.Printf("  Virtual Address Space Available: %v TB\n", exMem.VirtualAvail/1024/1024/1024/1024)
    fmt.Printf("  Commit Limit: %v GB\n", exMem.CommitLimit/1024/1024/1024)
    fmt.Printf("  Commit Total: %v GB\n", exMem.CommitTotal/1024/1024/1024)

    // Calculate commit usage percentage
    commitPercent := float64(exMem.CommitTotal) / float64(exMem.CommitLimit) * 100
    fmt.Printf("\nCommit Charge: %.2f%%\n", commitPercent)
    
    if commitPercent > 90 {
        fmt.Println("WARNING: High commit charge! Consider increasing page file size or adding RAM.")
    }
}

Platform Detection

Since extended functions are platform-specific, use runtime checks:
package main

import (
    "fmt"
    "runtime"
    
    "github.com/shirou/gopsutil/v4/mem"
)

func getExtendedMemory() error {
    switch runtime.GOOS {
    case "linux":
        ex := mem.NewExLinux()
        exMem, err := ex.VirtualMemory()
        if err != nil {
            return err
        }
        fmt.Printf("Linux Extended Memory: %s\n", exMem.String())
        
    case "windows":
        ex := mem.NewExWindows()
        exMem, err := ex.VirtualMemory()
        if err != nil {
            return err
        }
        fmt.Printf("Windows Extended Memory:\n")
        fmt.Printf("  Commit Total: %v MB\n", exMem.CommitTotal/1024/1024)
        fmt.Printf("  Commit Limit: %v MB\n", exMem.CommitLimit/1024/1024)
        
    default:
        fmt.Printf("Extended memory stats not available on %s\n", runtime.GOOS)
    }
    
    return nil
}

Use Cases

Linux Memory Analysis

Cache Analysis

Distinguish between active and inactive file cache to understand memory reclaim potential

Swap Candidates

Identify inactive anonymous memory that’s likely to be swapped

Locked Memory

Monitor unevictable pages from real-time processes or shared memory

Kernel Overhead

Track kernel stack and per-CPU memory usage

Windows Memory Analysis

Commit Charge

Monitor commit charge to prevent out-of-memory conditions

Virtual Address Space

Track address space exhaustion on 32-bit applications

Page File Usage

Monitor page file usage and availability

Physical vs Virtual

Distinguish between physical RAM and virtual memory usage

References

Linux

Windows

See Also

Build docs developers (and LLMs) love