Open In App

Determine Memory Usage of Data Objects in R

Last Updated : 16 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to discuss how to find the memory used by the Data Objects in R Language. In R, after the creation of the object, It will allocate some space to the particular object. The object in memory is stored in bytes. Numeric type can allocate 56 bytes and character type can allocate 112 bytes.

Method 1: Using memory.profile()

memory.profile(): It will show the Lists the usage of the cons cells by SEXPREC type. If you want to know all the memory profiles in R, You can get it using memory.profile() function.

Syntax: memory.profile()

Code:

R




print(memory.profile())


Output:

Method 2: Using object.size()

If you want to get individual object size then we can get it by using object.size() function

Syntax: object.size(data_object)

Where, data_object is the R object.

Example 1: R program to get the bytes sizes of numeric and character objects.

R




# numeric value
a = 11
print(object.size(a))
  
# character value
b ='a'
print(object.size(b))


Output:

56 bytes
112 bytes

Example 2: Get the list of datatype sizes using sapply().

If we want to get the all datatype sizes at a time, we can apply sapply() function along with object.size() function with ls() function. R program to create 5 variable sin workspace and determine the memory in bytes

R




# numeric value
a = 11
  
# character value
b ='a'
  
# numeric value
c = 120.90
  
# character value
d ='sravan'
  
# numeric value
e =23
  
# sapply function
print(sapply(ls(), function(x) {    
  object.size(get(x)) }))


Output:

a    average.z            b            c     climbing   climbing.z 

          56           96          112           56           96           96 

           d        drink      drink.z            e error_values            x 

         112           96           96           56          112          112 

           y 

         112 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads