Determining memory usage of objects? [duplicate]

2019-01-04 05:03发布

This question already has an answer here:

I'd like to work out how much RAM is being used by each of my objects inside my current workspace. Is there an easy way to do this?

标签: memory r
4条回答
唯我独甜
2楼-- · 2019-01-04 05:33

1. by object size

to get memory allocation on an object-by-object basis, call object.size() and pass in the object of interest:

object.size(My_Data_Frame)

(unless the argument passed in is a variable, it must be quoted, or else wrapped in a get call.)variable name, then omit the quotes,

you can loop through your namespace and get the size of all of the objects in it, like so:

for (itm in ls()) { 
    print(formatC(c(itm, object.size(get(itm))), 
        format="d", 
        big.mark=",", 
        width=30), 
        quote=F)
}

2. by object type

to get memory usage for your namespace, by object type, use memory.profile()

memory.profile()

   NULL      symbol    pairlist     closure environment     promise    language 
      1        9434      183964        4125        1359        6963       49425 
special     builtin        char     logical     integer      double     complex 
    173        1562       20652        7383       13212        4137           1 

(There's another function, memory.size() but i have heard and read that it only seems to work on Windows. It just returns a value in MB; so to get max memory used at any time in the session, use memory.size(max=T)).

查看更多
疯言疯语
3楼-- · 2019-01-04 05:39

some time ago I stole this little nugget from here:

sort( sapply(ls(),function(x){object.size(get(x))})) 

it has served me well

查看更多
聊天终结者
4楼-- · 2019-01-04 05:42

This question was posted and got legitimate answers so much ago, but I want to let you know another useful tips to get the size of an object using a library called gdata and its ll() function.

library(gdata)
ll() # return a dataframe that consists of a variable name as rownames, and class and size (in KB) as columns
subset(ll(), KB > 1000) # list of object that have over 1000 KB
ll()[order(ll()$KB),] # sort by the size (ascending)
查看更多
乱世女痞
5楼-- · 2019-01-04 05:57

You could try the lsos() function from this question:

R> a <- rnorm(100)
R> b <- LETTERS
R> lsos()
       Type Size Rows Columns
b character 1496   26      NA
a   numeric  840  100      NA
R> 
查看更多
登录 后发表回答