\donttest{
# 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)
}can_allocate
can_allocate
SYSTEM_UTILS
1 Description
Checks whether a given amount of memory can be safely allocated while maintaining a safety margin.
2 Usage
can_allocate(size_gb, safety_margin_pct = 20.0)3 Arguments
| Parameter | Description |
|---|---|
size_gb |
Size in gigabytes (GB) to check |
safety_margin_pct |
Percentage of available RAM to keep free (default 20 percent) |
4 Value
Logical. TRUE if allocation is likely safe, FALSE otherwise
5 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: