2

I wanted to find out the memory consumed (in bytes) by data types. I called size method on an integer. Since I am running a 64 bit machine, it returned 8.

1.size # => 8

Similarly, for strings and arrays, it returned 1 byte per character/integer.

'a'.size # => 1
['a'].size # => 1
['a', 1].size # => 2
  1. Why is there no size method for float?
  2. Shouldn't heterogeneous arrays like ['a', 1] return 1 + 8 = 9 bytes (1 for char, 8 for integer)?
  3. Is it correct to call size to check memory allocated to ruby data types?
3
  • in the case of an array, the size method is just an alias for the length method of the array (which is just the number of elements in the array). similarly, in the case of strings, the size method is just the character length of the string. Commented Dec 31, 2014 at 6:25
  • 2
    In the title, you indicate that array does not have size. In the text, you indicate that float does not have it. It is not clear what you mean. Commented Dec 31, 2014 at 6:43
  • '€π'.size returns 2; so .size counts the number of characters in strings, not bytes. Commented Jan 2, 2015 at 18:46

3 Answers 3

9

I think you are looking for MRI memory usage. Ruby has ObjectSpace : The objspace library extends the ObjectSpace module and adds several methods to get internal statistic information about object/memory management.

You need to require 'objspace' to use this extension module.

Here is what you will get:

 > require 'objspace'
 => true 
 > ObjectSpace.memsize_of(Array)
 => 5096 
 > ObjectSpace.memsize_of(Hash)
 => 3304 
 > ObjectSpace.memsize_of(String)
 => 6344 
 > ObjectSpace.memsize_of(Integer)
 => 1768 

Note: Generally, you SHOULD NOT use this library if you do not know about the MRI implementation. Mainly, this library is for (memory) profiler developers and MRI developers who need to know about MRI memory usage.

Sign up to request clarification or add additional context in comments.

3 Comments

From the documentation of this method: Note that the return size is incomplete. You need to deal with this information as only a HINT. Especially, the size of T_DATA may not be correct.
@spickermann: Yes you are right. Actually I never need to use of this in my development. As OP has asked so I was also eager to know and got this solution.
Also note that the example here actually get the size the of classes themselves, not of an instance. ObjectSpace.memsize_of([]) usually returns 40 on ruby 2.2+ for example, which is the size of the empty array in memory (also the minimum size of most ruby objects)
4

These are two different methods that serves different purpose for two different data types.

In eg 1, you are applying size to fixnum. This method:

Returns the number of bytes in the machine representation of fix.

source: http://www.ruby-doc.org/core-2.2.0/Fixnum.html#method-i-size

However when used with array, size is alias for length. Here: http://www.ruby-doc.org/core-2.1.2/Array.html#method-i-size. Which:

Returns the number of elements in self. May be zero.

Comments

2

Array#size returns the count of elements of the Array rather than memory allocated.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.