| can_allocate | R Documentation |
Checks whether a given amount of memory can be safely allocated while maintaining a safety margin.
can_allocate(size_gb, safety_margin_pct = 20)
size_gb |
Size in gigabytes (GB) to check |
safety_margin_pct |
Percentage of available RAM to keep free (default 20 percent) |
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
Logical. TRUE if allocation is likely safe, FALSE otherwise
This is a heuristic check, not a guarantee. Allocation can still fail due to memory fragmentation or competing processes.
get_available_ram
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.