Skip to content

Instantly share code, notes, and snippets.

@smalik
Created October 27, 2015 14:14
Show Gist options
  • Save smalik/48b528e9445d7363bfd2 to your computer and use it in GitHub Desktop.
Save smalik/48b528e9445d7363bfd2 to your computer and use it in GitHub Desktop.
# Get memory allocation on an object by object basis. Loop over your namespace and print out memory used by all objects.
for (itm in ls()) {
print(formatC(c(itm, object.size(get(itm))), format="d", big.mark=",", width=30), quote=F)
}
# Print memory used collectively in namespace
print(memory.profile())
# To estimate if vector (of numbers) will fit into memory so we can predict the memory usage based on vector size.
# To find the size of a matrix or a dataframe just multiply out the number of vectors used or just loop over the vectors in the data frame or matrix.
estimate_dataSize = function(nsize, ntype = "numeric") {
if(ntype == "integer") {
byte_per_number = 4
} else if(ntype == "numeric") {
byte_per_number = 8
} else {
stop(sprintf("Unknown number_type: %s", number_type))
}
byte_size = (nsize * byte_per_number)
class(byte_size) = "object_size"
print(byte_size, units = "auto")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment