get_available_ram

get_available_ram

SYSTEM_UTILS

1 Description

Returns the amount of RAM currently available for allocation.

2 Usage

get_available_ram()

3 Value

Numeric value with available RAM in gigabytes (GB)

4 Details

This function returns the RAM that can be allocated without swapping. The value changes dynamically as processes allocate and free memory.

Important notes: - Value can change rapidly; don’t cache it - On Linux, uses MemAvailable (more accurate than MemFree) - Includes memory that can be reclaimed from caches - Actual allocatable memory may be slightly less

Use case: Check available RAM before loading large datasets into memory.

5 Examples

\donttest{
available <- get_available_ram()
cat("Available RAM:", round(available, 2), "GB\n")

# Use it to decide how much data to load
fn <- tempfile(fileext = ".h5")
X  <- hdf5_create_matrix(fn, "data/M",
                          data = matrix(rnorm(1000), 100, 10))

size_gb <- prod(dim(X)) * 8 / 1e9
if (get_available_ram() > size_gb * 1.2) {
  mat <- as.matrix(X)
} else {
  mat <- X[1:50, ]
}

hdf5_close_all()
unlink(fn)
}

6 See Also