can_allocate: Check if memory allocation is safe

View source: R/RcppExports.R

can_allocateR Documentation

Check if memory allocation is safe

Description

Checks whether a given amount of memory can be safely allocated while maintaining a safety margin.

Usage

can_allocate(size_gb, safety_margin_pct = 20)

Arguments

size_gb

Size in gigabytes (GB) to check

safety_margin_pct

Percentage of available RAM to keep free (default 20 percent)

Details

This function checks if the requested memory can be allocated while keeping a safety margin of free RAM. This helps prevent:

  • System instability from memory exhaustion

  • Swapping (which degrades performance)

  • Out-of-memory errors from other processes

Formula: can_allocate = (size_gb < available_ram * (1 - safety_margin / 100))

Safety margin guidelines:

  • 20 percent (default): Conservative, recommended for most cases

  • 10 percent: Moderate, for controlled environments

  • 5 percent: Aggressive, only if you know what you're doing

  • 0 percent: Maximum risk, not recommended

Value

Logical. TRUE if allocation is likely safe, FALSE otherwise

Note

This is a heuristic check, not a guarantee. Allocation can still fail due to memory fragmentation or competing processes.

See Also

get_available_ram

Examples


# Check if 1 GB can be safely allocated
if (can_allocate(1)) {
  message("1 GB allocation is safe")
} else {
  message("Not enough RAM for 1 GB allocation")
}

# 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   # estimate in GB
if (can_allocate(size_gb)) {
  mat <- as.matrix(X)
} else {
  mat <- X[1:50, ]   # load subset
}

hdf5_close_all()
unlink(fn)



BigDataStatMeth documentation built on May 15, 2026, 1:07 a.m.