14

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.

0

1 Answer 1

24

Yes there is, try:

object.size()
Sign up to request clarification or add additional context in comments.

4 Comments

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.
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')))
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()
ok, I agree. Could've, SHOULD'VE. Thanks for the great additions!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.