Skip to content

Instantly share code, notes, and snippets.

@danlewer
Created December 10, 2020 18:15
Show Gist options
  • Save danlewer/517442d9cd5e10741561542ee87f617e to your computer and use it in GitHub Desktop.
Save danlewer/517442d9cd5e10741561542ee87f617e to your computer and use it in GitHub Desktop.
objectSizeTable <- function(unit = 1e6, env = .GlobalEnv, top = 10, totals = T) { # 1e6 approximates Mb (1e9 for Gb)
o <- ls(envir = env)
y <- data.frame(object = o, size = sapply(o, function(x) object.size(get(x, envir = env))))
y$size <- y$size / unit
y <- y[order(y$size, decreasing = T),]
if (totals) {y <- rbind(data.frame(object = c('total', 'other_environments'), size = c(sum(y$size), memory.size() - sum(y$size))), y)}
rownames(y) <- NULL
if (is.na(top)) return(y) else return(y[seq_len(top + (2 * totals)),])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment