Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Ruby

Idiom #109 Number of bytes of a type

Set n to the number of bytes of a variable t (of type T).

require 'objspace'
n = ObjectSpace.memsize_of(t)

From the docs: " 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."
N : Integer := (T'Size + 7) / 8;

T'Size returns size of T in bits. It is the minimum number of bits required to store an object of that type.
Divide by 8 to convert to bytes

New implementation...
< >
programming-idioms.org