Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining memory usage of objects?

Tags:

memory

r

People also ask

How do you determine memory usage?

Press Ctrl + Shift + Esc to launch Task Manager. Or, right-click the Taskbar and select Task Manager. Select the Performance tab and click Memory in the left panel. The Memory window lets you see your current RAM usage, check RAM speed, and view other memory hardware specifications.

How much memory does an object use?

In a modern 64-bit JDK, an object has a 12-byte header, padded to a multiple of 8 bytes, so the minimum object size is 16 bytes. For 32-bit JVMs, the overhead is 8 bytes, padded to a multiple of 4 bytes. (From Dmitry Spikhalskiy's answer, Jayen's answer, and JavaWorld.)

Which memory is used by the objects?

Stack memory stores primitive types and the addresses of objects. The object values are stored in heap memory. An object reference on the stack is only an address that refers to the place in heap memory where that object is kept.

How do you check the memory consumption of an object in Python?

The deep\_getsizeof() Function getsizeof() can only tell you how much memory a primitive object takes, let's take a look at a more adequate solution. The deep\_getsizeof() function drills down recursively and calculates the actual memory usage of a Python object graph.


some time ago I stole this little nugget from here:

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

it has served me well


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)).


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> 

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)

another (slightly prettier) option using dplyr

    data.frame('object' = ls()) %>% 
      dplyr::mutate(size_unit = object %>%sapply(. %>% get() %>% object.size %>% format(., unit = 'auto')),
                    size = as.numeric(sapply(strsplit(size_unit, split = ' '), FUN = function(x) x[1])),
                    unit = factor(sapply(strsplit(size_unit, split = ' '), FUN = function(x) x[2]), levels = c('Gb', 'Mb', 'Kb', 'bytes'))) %>% 
      dplyr::arrange(unit, dplyr::desc(size)) %>% 
      dplyr::select(-size_unit)

A data.table function that separates memory and unit for easier sorting:

ls.obj <- {as.data.table(sapply(ls(), function(x){format(object.size(get(x)), nsmall=3,digits=3,unit="Mb")}),keep.rownames=TRUE)[, c("mem","unit") := tstrsplit(V2, " ", fixed=TRUE)][, setnames(.SD,"V1","obj")][,.(obj,mem=as.numeric(mem),unit)][order(-mem)]}

ls.obj

                    obj     mem unit
 1:                obj1 848.283   Mb
 2:                obj2  37.705   Mb

...