# How FastFetch Detects Memory and Swap Usage: A Deep Dive into Cross-Platform System Detection

> Discover how fastfetch detects memory and swap usage by reading kernel interfaces and platform specific APIs. Learn about cross platform system detection for your Linux, macOS, or Windows system.

- Repository: [fastfetch-cli/fastfetch](https://github.com/fastfetch-cli/fastfetch)
- Tags: deep-dive
- Published: 2026-03-30

---

**FastFetch detects memory and swap usage through platform-specific detector functions (`ffDetectMemory` and `ffDetectSwap`) that read kernel interfaces like `/proc/meminfo`, Windows APIs, or `sysctl`, then populate unified result structs for display.**

FastFetch, the popular system information tool from the `fastfetch-cli/fastfetch` repository, implements a modular detection layer that abstracts OS-specific memory statistics into consistent data structures. Understanding how fastfetch detects memory and swap usage reveals a sophisticated approach to cross-platform system programming, where each operating system requires distinct kernel interfaces to retrieve accurate RAM and swap metrics.

## The Detection Architecture

The generic workflow follows a four-step pipeline that separates data acquisition from presentation. First, the **module entry point** (`ffPrintMemory` or `ffPrintSwap`) allocates result storage. Next, it invokes the **platform detector**: `ffDetectMemory(&result)` or `ffDetectSwap(&list)`. The detector then queries the OS-specific source—whether parsing procfs, calling Win32 APIs, or executing sysctl commands—and fills the result fields (`bytesTotal`, `bytesUsed`, and device names for swap). Finally, the module formats these values for terminal display or JSON output.

The memory detector's signature is declared in [`src/detection/memory/memory.h`](https://github.com/fastfetch-cli/fastfetch/blob/main/src/detection/memory/memory.h):

```c
const char* ffDetectMemory(FFMemoryResult* ram);

```

Similarly, swap detection uses [`src/detection/swap/swap.h`](https://github.com/fastfetch-cli/fastfetch/blob/main/src/detection/swap/swap.h):

```c
const char* ffDetectSwap(FFlist* result);

```

## Memory Detection Implementation

FastFetch implements `ffDetectMemory` separately for each supported platform, ensuring accurate physical RAM calculations across diverse kernel architectures.

### Linux: Parsing /proc/meminfo

In [`src/detection/memory/memory_linux.c`](https://github.com/fastfetch-cli/fastfetch/blob/main/src/detection/memory/memory_linux.c), the detector reads `/proc/meminfo` using `ffReadFileData`. It extracts `MemTotal` and `MemAvailable` values. If `MemAvailable` is missing or unreasonable (common in older kernels), the code recomputes available memory using `MemFree`, `Buffers`, `Cached`, and `SReclaimable`, then subtracts `Shmem` to avoid double-counting. All values are multiplied by 1024 to convert kernel-reported KiB into bytes.

### Windows: GlobalMemoryStatusEx

The Windows implementation in [`src/detection/memory/memory_windows.c`](https://github.com/fastfetch-cli/fastfetch/blob/main/src/detection/memory/memory_windows.c) calls `GlobalMemoryStatusEx`, which populates a `MEMORYSTATUSEX` structure. The detector maps `ullTotalPhys` to `bytesTotal` and calculates `bytesUsed` as `ullTotalPhys - ullAvailPhys`, providing immediate physical memory consumption without pagefile inclusion.

### macOS and iOS: sysctl and host_statistics64

For Apple platforms, [`src/detection/memory/memory_apple.c`](https://github.com/fastfetch-cli/fastfetch/blob/main/src/detection/memory/memory_apple.c) uses `sysctl` to retrieve `hw.memsize` (or `hw.memsize_usable` when available) for total RAM. It then calls `host_statistics64` to obtain `vm_statistics64_data_t`. The calculation subtracts free pages (`free_count - speculative_count`) and file-backed pages (`external_page_count`) from the total page count to derive `bytesUsed`.

### BSD Systems: sysctlbyname

The BSD variant in [`src/detection/memory/memory_bsd.c`](https://github.com/fastfetch-cli/fastfetch/blob/main/src/detection/memory/memory_bsd.c) mirrors the Apple approach but uses `sysctlbyname("hw.physmem")` for total memory and `sysctlbyname("vm.stats.vm.v_page_size")` for page size. It reads `vm.stats.vm.v_free_count` and related counters to compute usage statistics.

### Haiku and SunOS

- **Haiku**: [`src/detection/memory/memory_haiku.c`](https://github.com/fastfetch-cli/fastfetch/blob/main/src/detection/memory/memory_haiku.c) calls `get_system_info(&info)` from `<OS.h>`, multiplying `info.max_pages` and `info.used_pages` by the platform page size.
- **SunOS**: [`src/detection/memory/memory_sunos.c`](https://github.com/fastfetch-cli/fastfetch/blob/main/src/detection/memory/memory_sunos.c) uses `sysconf(_SC_PHYS_PAGES)` and `sysconf(_SC_AVPHYS_PAGES)` combined with `instance.state.platform.sysinfo.pageSize` for conversion.

## Swap Detection Implementation

Swap detection follows a similar pattern but handles multiple devices per system, storing results in a dynamic list of `FFSwapResult` structures.

### Linux: /proc/swaps and /proc/meminfo

[`src/detection/swap/swap_linux.c`](https://github.com/fastfetch-cli/fastfetch/blob/main/src/detection/swap/swap_linux.c) first attempts to read `/proc/swaps` to enumerate individual swap devices with their names, total sizes, and usage. If this fails, it falls back to `/proc/meminfo`, extracting `SwapTotal` and `SwapFree` to create a single aggregated "Total" entry. Values are multiplied by 1024 to convert KiB to bytes.

### Windows: NtQuerySystemInformation

The Windows swap detector in [`src/detection/swap/swap_windows.c`](https://github.com/fastfetch-cli/fastfetch/blob/main/src/detection/swap/swap_windows.c) calls the undocumented `NtQuerySystemInformation` with `SystemPagefileInformation`. For each `SYSTEM_PAGEFILE_INFORMATION` entry returned, it extracts the file name, total pages, and used pages, multiplying by the system page size to populate `FFSwapResult` entries.

### BSD and Apple Variants

- **BSD**: [`src/detection/swap/swap_bsd.c`](https://github.com/fastfetch-cli/fastfetch/blob/main/src/detection/swap/swap_bsd.c) retrieves `vm.swapinfo` via `sysctl` and iterates over the `struct xswdev` array, converting page counts to bytes for each swap device.
- **macOS/iOS**: [`src/detection/swap/swap_apple.c`](https://github.com/fastfetch-cli/fastfetch/blob/main/src/detection/swap/swap_apple.c) calls `host_statistics64` with `HOST_VM_INFO64`, extracting `swapins` and `swapouts`. If swapping is disabled, it returns a single entry with `bytesTotal = 0`.

### Additional Platforms

- **Haiku**: [`src/detection/swap/swap_haiku.c`](https://github.com/fastfetch-cli/fastfetch/blob/main/src/detection/swap/swap_haiku.c) uses `get_system_info` to retrieve `info.swap_total` and `info.swap_used`.
- **SunOS**: [`src/detection/swap/swap_sunos.c`](https://github.com/fastfetch-cli/fastfetch/blob/main/src/detection/swap/swap_sunos.c) reads swap statistics via `kstat_lookup` and `kstat_data_lookup`.
- **Unsupported**: [`src/detection/swap/swap_nosupport.c`](https://github.com/fastfetch-cli/fastfetch/blob/main/src/detection/swap/swap_nosupport.c) returns an error string indicating the platform lacks detection support.

## Code Examples

### Direct Detector Invocation

You can invoke the detectors directly in C code as the modules do internally:

```c
/* Memory detection */
FFMemoryResult mem = {0};
const char *memErr = ffDetectMemory(&mem);
if (!memErr) {
    printf("Memory: %llu B total, %llu B used\n",
           (unsigned long long)mem.bytesTotal,
           (unsigned long long)mem.bytesUsed);
}

/* Swap detection (Linux example) */
FFlist swaps = ffListCreate(sizeof(FFSwapResult));
const char *swapErr = ffDetectSwap(&swaps);
if (!swapErr) {
    FF_LIST_FOR_EACH (FFSwapResult, s, swaps) {
        printf("Swap device %s: %llu B total, %llu B used\n",
               s->name.chars,
               (unsigned long long)s->bytesTotal,
               (unsigned long long)s->bytesUsed);
    }
}
ffListDestroy(&swaps);

```

### CLI Usage

When using the compiled binary, the detection logic triggers automatically:

```bash
$ fastfetch --module memory
 Memory: 7.8 GiB / 15.6 GiB (50%)

$ fastfetch --module swap
 Swap: 2.0 GiB / 2.0 GiB (100%)

```

The CLI commands invoke `ffPrintMemory` and `ffPrintSwap`, which internally call the same detection functions described above.

## Summary

- **FastFetch uses platform-specific detectors** (`ffDetectMemory` and `ffDetectSwap`) to abstract diverse kernel interfaces into unified result structs.
- **Linux implementations** parse `/proc/meminfo` and `/proc/swaps`, converting KiB values to bytes and handling edge cases like missing `MemAvailable`.
- **Windows detection** relies on `GlobalMemoryStatusEx` for memory and `NtQuerySystemInformation` for swap pagefile details.
- **Apple and BSD systems** use `sysctl` and `host_statistics64` to calculate physical memory usage by analyzing page counts and states.
- **Result structs** (`FFMemoryResult` and `FFSwapResult`) provide a consistent API for the display modules in [`src/modules/memory/memory.c`](https://github.com/fastfetch-cli/fastfetch/blob/main/src/modules/memory/memory.c) and [`src/modules/swap/swap.c`](https://github.com/fastfetch-cli/fastfetch/blob/main/src/modules/swap/swap.c).

## Frequently Asked Questions

### How does FastFetch handle older Linux kernels that lack MemAvailable?

According to the source code in [`src/detection/memory/memory_linux.c`](https://github.com/fastfetch-cli/fastfetch/blob/main/src/detection/memory/memory_linux.c), when `MemAvailable` is missing or reports unreasonable values, FastFetch recomputes available memory manually. It sums `MemFree`, `Buffers`, `Cached`, and `SReclaimable`, then subtracts `Shmem` to prevent double-counting cached shared memory pages.

### Why does the Windows swap detector use an undocumented API?

[`src/detection/swap/swap_windows.c`](https://github.com/fastfetch-cli/fastfetch/blob/main/src/detection/swap/swap_windows.c) implements `ffDetectSwap` using `NtQuerySystemInformation` with `SystemPagefileInformation` because the standard Windows APIs do not provide per-pagefile granularity for swap usage. This undocumented interface returns detailed `SYSTEM_PAGEFILE_INFORMATION` structures containing total and used page counts for each swap file.

### Does FastFetch support swap detection on macOS?

As implemented in [`src/detection/swap/swap_apple.c`](https://github.com/fastfetch-cli/fastfetch/blob/main/src/detection/swap/swap_apple.c), FastFetch detects swap status on macOS via `host_statistics64`. However, modern macOS versions use dynamic virtual memory compression rather than traditional swap partitions. The detector returns swap statistics if available, or a zeroed entry if swapping is disabled or compressed memory is used exclusively.

### What happens when FastFetch runs on an unsupported platform?

For platforms without specific implementations, [`src/detection/memory/memory_nosupport.c`](https://github.com/fastfetch-cli/fastfetch/blob/main/src/detection/memory/memory_nosupport.c) and [`src/detection/swap/swap_nosupport.c`](https://github.com/fastfetch-cli/fastfetch/blob/main/src/detection/swap/swap_nosupport.c) return error strings indicating lack of support. The calling modules in `src/modules/` handle these errors gracefully, typically omitting the memory or swap sections from output rather than crashing.