How would one check the system memory available using R on a Windows machine?

Maybe one of the below will help ( I am also on Windows Server 2012 R2):

Maybe this would be the most useful:

> system('systeminfo')
#the output is too big to show but you can save into a list and choose the rows you want

Or just use one of the below which are specific about the memory

> system('wmic MemoryChip get BankLabel, Capacity, MemoryType, TypeDetail, Speed')
BankLabel    Capacity    MemoryType  Speed  TypeDetail  
RAM slot #0  8589934592  2                  512         
RAM slot #1  4294967296  2                  512   

Free available memory:

> system('wmic OS get FreePhysicalMemory /Value')
FreePhysicalMemory=8044340

Total available Memory

> system('wmic OS get TotalVisibleMemorySize /Value')
TotalVisibleMemorySize=12582456

Basically you can even run any other cmd command you want that you know it could help you through the system function. R will show the output on the screen and you can then save into a data.frame and use as you want.


Just for the sake of completion, I added support for Linux on Stefan's answer above- Tested on Ubuntu 16

getFreeMemoryKB <- function() {
  osName <- Sys.info()[["sysname"]]
  if (osName == "Windows") {
    x <- system2("wmic", args =  "OS get FreePhysicalMemory /Value", stdout = TRUE)
    x <- x[grepl("FreePhysicalMemory", x)]
    x <- gsub("FreePhysicalMemory=", "", x, fixed = TRUE)
    x <- gsub("\r", "", x, fixed = TRUE)
    return(as.integer(x))
  } else if (osName == 'Linux') {
    x <- system2('free', args='-k', stdout=TRUE)
    x <- strsplit(x[2], " +")[[1]][4]
    return(as.integer(x))
  } else {
    stop("Only supported on Windows and Linux")
  }
}

I wrapped LyzandeR's answer above up in a functions that returns the physical memory in kilobytes (1024 bytes). Tested on windows 7.

get_free_ram <- function(){
  if(Sys.info()[["sysname"]] == "Windows"){
    x <- system2("wmic", args =  "OS get FreePhysicalMemory /Value", stdout = TRUE)
    x <- x[grepl("FreePhysicalMemory", x)]
    x <- gsub("FreePhysicalMemory=", "", x, fixed = TRUE)
    x <- gsub("\r", "", x, fixed = TRUE)
    as.integer(x)
  } else {
    stop("Only supported on Windows OS")
  }
}

Tags:

Windows

Memory

R