Tricks to manage the available memory in an R session

前端 未结 27 1547
情深已故
情深已故 2020-11-22 01:23

What tricks do people use to manage the available memory of an interactive R session? I use the functions below [based on postings by Petr Pikal and David Hinds to the r-he

27条回答
  •  花落未央
    2020-11-22 02:01

    This adds nothing to the above, but is written in the simple and heavily commented style that I like. It yields a table with the objects ordered in size , but without some of the detail given in the examples above:

    #Find the objects       
    MemoryObjects = ls()    
    #Create an array
    MemoryAssessmentTable=array(NA,dim=c(length(MemoryObjects),2))
    #Name the columns
    colnames(MemoryAssessmentTable)=c("object","bytes")
    #Define the first column as the objects
    MemoryAssessmentTable[,1]=MemoryObjects
    #Define a function to determine size        
    MemoryAssessmentFunction=function(x){object.size(get(x))}
    #Apply the function to the objects
    MemoryAssessmentTable[,2]=t(t(sapply(MemoryAssessmentTable[,1],MemoryAssessmentFunction)))
    #Produce a table with the largest objects first
    noquote(MemoryAssessmentTable[rev(order(as.numeric(MemoryAssessmentTable[,2]))),])
    

提交回复
热议问题