Using R now, and my memory is almost full(gc() applied already). Is there a way to know each variables' memory consumption so that I know which one takes the most memory and remove that one.
1 Answer
Yes there is, try:
object.size()
4 Comments
David
If you notice a large difference between the space required by your objects and their object size you can also try saving your working space via
save.image() restarting R and loading your workspace.flying sheep
you could e.g. have worked out the perfect method to get a sorted human-readable list, as the asker wanted “each variables’ memory consumption”:
sizes <- sapply(ls(), function(n) object.size(get(n)), simplify = FALSE); print(sapply(sizes[order(as.integer(sizes))], function(s) format(s, unit = 'auto')))Alan
Great comment. I've put these commands in a function and inverse the ordering:
list_obj_sizes <- function(list_obj=ls(envir=.GlobalEnv)){ sizes <- sapply(list_obj, function(n) object.size(get(n)), simplify = FALSE) print(sapply(sizes[order(-as.integer(sizes))], function(s) format(s, unit = 'auto'))) } . It's easier to use: list_obj_sizes()Matt Bannert
ok, I agree. Could've, SHOULD'VE. Thanks for the great additions!