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.
Asked
Active
Viewed 1.8k times
1 Answers
22
Yes there is, try:
object.size()

jhpratt
- 6,841
- 16
- 40
- 50

Matt Bannert
- 27,631
- 38
- 141
- 207
-
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. – David Oct 26 '13 at 19:26
-
8you 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')))` – flying sheep May 24 '16 at 18:21
-
9Great 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()` – Alan Nov 16 '16 at 20:51
-
ok, I agree. Could've, SHOULD'VE. Thanks for the great additions! – Matt Bannert Mar 06 '18 at 16:56