sizeof

The sizeof expression returns an Int32 with the size in bytes of a given type. For example:

  1. sizeof(Int32) #=> 4
  2. sizeof(Int64) #=> 8

For Reference types, the size is the same as the size of a pointer:

  1. # On a 64 bits machine
  2. sizeof(Pointer(Int32)) #=> 8
  3. sizeof(String) #=> 8

This is because a Reference’s memory is allocated on the heap and a pointer to it is passed around. To get the effective size of a class, use instance_sizeof.

The argument to sizeof is a type and is often combined with typeof:

  1. a = 1
  2. sizeof(typeof(a)) #=> 4