Overview
TheSwapMemoryStat struct contains swap space usage statistics and paging activity metrics. Swap memory is disk space used as virtual memory when physical RAM is full.
Struct Definition
Fields
Cross-Platform Fields
Total
- Type:
uint64 - Description: Total swap space size in bytes
- Example:
8589934592(8 GB) - Note: Will be 0 if no swap is configured
Used
- Type:
uint64 - Description: Amount of swap space currently in use in bytes
- Calculation:
Total - Free
Free
- Type:
uint64 - Description: Amount of unused swap space in bytes
- Note: Equal to
Totalwhen no swap is being used
UsedPercent
- Type:
float64 - Description: Percentage of swap space in use (0.0 to 100.0)
- Calculation:
(Used / Total) * 100 - Example:
23.5means 23.5% of swap is in use - Performance indicator: High values may indicate memory pressure
Paging Activity Fields
These fields track memory page movement between RAM and swap. Higher values indicate more paging activity.Sin
- Type:
uint64 - Description: Number of bytes swapped in from disk to memory since boot
- Platform availability: Linux, BSD, some Unix systems
- Performance note: High values indicate the system is reading from swap frequently
Sout
- Type:
uint64 - Description: Number of bytes swapped out from memory to disk since boot
- Platform availability: Linux, BSD, some Unix systems
- Performance note: High values indicate the system is writing to swap frequently
PgIn
- Type:
uint64 - Description: Number of pages paged in from disk
- Platform availability: Linux, BSD
- Note: Includes all page-ins, not just swap (e.g., file-backed pages)
PgOut
- Type:
uint64 - Description: Number of pages paged out to disk
- Platform availability: Linux, BSD
- Note: Includes all page-outs, not just swap
PgFault
- Type:
uint64 - Description: Total number of page faults (both major and minor) since boot
- Platform availability: Linux, BSD, some Unix systems
- Note: This is a cumulative counter
- Minor fault: Page was in memory but not in the process’s page table
- Major fault: Page had to be loaded from disk (see
PgMajFault)
Linux Specific Fields
PgMajFault
- Type:
uint64 - Description: Number of major page faults since boot (page had to be loaded from disk)
- Platform: Linux only
- Source:
/proc/vmstator cgroup v2 memory.stat - Performance indicator: High values indicate heavy disk I/O for paging
- Reference: Linux cgroup v2 documentation
Methods
String
Usage Example
Monitoring Swap Performance
To monitor swap activity over time, you can pollSwapMemory() periodically and calculate deltas:
SwapDevice Struct
For Linux systems, you can get per-device swap information:Fields
- Name (
string): Device name or file path (e.g.,/dev/sda2,/swapfile) - UsedBytes (
uint64): Bytes in use on this swap device - FreeBytes (
uint64): Bytes available on this swap device
Usage
Platform Behavior
Linux
- All fields populated from
/proc/meminfo,/proc/vmstat, and/proc/swaps PgMajFaultavailable from/proc/vmstatSwapDevices()function available
Windows
- Basic swap fields available (Total, Used, Free, UsedPercent)
- Paging fields may be 0 or limited
- Uses Windows API
GlobalMemoryStatusExandGetPerformanceInfo
macOS / BSD
- Basic swap fields available
- Some paging fields available depending on the specific BSD variant
- Values obtained from
sysctlcalls
Other Unix
- Platform-dependent field availability
- Basic fields typically available, paging activity may be limited
Performance Considerations
High swap usage or frequent paging activity can severely impact system performance. Monitor these metrics:
- UsedPercent > 50%: System may be under memory pressure
- UsedPercent > 80%: Consider adding more RAM
- High Sin/Sout delta: Active swapping indicates insufficient RAM
- High PgMajFault rate: Disk I/O bottleneck due to paging
See Also
- Virtual Memory - RAM statistics
- Memory Package Overview - Package introduction
- Platform-Specific Extensions - Additional memory metrics