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.
Objects
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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access