Understand How Ruby Uses Memory

Ruby stores objects in its own heap, and uses operating system heap for data that doesn’t fit into objects. Let’s see how that works.


In Ruby everything is an object that’s internally represented as the RVALUE struct. sizeof(RVALUE) is machine dependent:

  • 20 bytes in 32-bit architecture when double is 4-byte aligned

  • 24 bytes in 32-bit architecture when double is 8-byte aligned

  • 40 bytes in 64-bit architecture

Most modern computers are 64-bit, so we’ll assume that one object costs us 40 bytes of memory to create.

You can see the object size on your computer in the debugger. I’m running my examples on 64-bit Linux, so I’ll use the gdb debugger. If you are on Mac OS X, try lldb that comes with the Xcode ...

Get Ruby Performance Optimization now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.