% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/RcppExports.R
\name{can_allocate}
\alias{can_allocate}
\title{Check if memory allocation is safe}
\usage{
can_allocate(size_gb, safety_margin_pct = 20)
}
\arguments{
\item{size_gb}{Size in gigabytes (GB) to check}

\item{safety_margin_pct}{Percentage of available RAM to keep free
(default 20 percent)}
}
\value{
Logical. TRUE if allocation is likely safe, FALSE otherwise
}
\description{
Checks whether a given amount of memory can be safely allocated
while maintaining a safety margin.
}
\details{
This function checks if the requested memory can be allocated while
keeping a safety margin of free RAM. This helps prevent:
\itemize{
\item System instability from memory exhaustion
\item Swapping (which degrades performance)
\item Out-of-memory errors from other processes
}

\strong{Formula:}
\code{can_allocate = (size_gb < available_ram * (1 - safety_margin / 100))}

\strong{Safety margin guidelines:}
\itemize{
\item 20 percent (default): Conservative, recommended for most cases
\item 10 percent: Moderate, for controlled environments
\item 5 percent: Aggressive, only if you know what you're doing
\item 0 percent: Maximum risk, not recommended
}
}
\note{
This is a heuristic check, not a guarantee. Allocation can still fail
due to memory fragmentation or competing processes.
}
\examples{
\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)
}

}
\seealso{
\code{\link{get_available_ram}}
}
