getAvailableMemoryMB

C++ Function Reference

1 Signature

size_t BigDataStatMeth::getAvailableMemoryMB()

2 Description

Detects available system memory using R-compatible methods.

3 Returns

size_t Available memory in megabytes (MB)

4 Details

Safely detects available memory using R’s internal functions without external dependencies. Designed for CRAN/Bioconductor compatibility across Windows, Linux, and macOS platforms.

5 Caller Graph

Function dependencies

6 Source Code

File: inst/include/Utilities/system-utils.hppLines 37-68

inline size_t getAvailableMemoryMB() {
    try {

                
        // Use R's internal memory functions (CRAN-safe)
        // // Use R's internal memory functions (CRAN-safe)
        // SEXP memCall = PROTECT(Rf_lang1(Rf_install("memory.size")));
        // SEXP memResult = PROTECT(Rf_eval(memCall, R_GlobalEnv));
        // 
        // if (Rf_isReal(memResult) && Rf_length(memResult) > 0) {
        //     double memMB = REAL(memResult)[0];
        //     UNPROTECT(2);
        //     return static_cast<size_t>(memMB * 0.6); // Use 60% of available
        // }
        // UNPROTECT(2);
        
        Rcpp::Function memSize("memory.size");
        Rcpp::NumericVector memResult = memSize();
        
        if (memResult.size() > 0) {
            double memMB = memResult[0];
            return static_cast<size_t>(memMB * 0.6); // Use 60% of available
        }
        

    } catch(...) {
        // Fallback silently - no error throwing for robustness
    }
    
    // Conservative fallback for any system (safe minimum)
    return 4000; // Assume 4GB available memory
}

7 Usage Example

#include "BigDataStatMeth.hpp"

// Example usage
auto result = getAvailableMemoryMB(...);