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
n = sizeof (t);
int n = sizeof(T);
n is the size in managed memory of built-in value type (numeric types, char, and bool) T. Permitted in safe contexts.
In a safe context, sizeof is only permitted for built-in value types. Because there is no corresponding type parameter constraint, the operand of_sizeof cannot be a type parameter in a safe context.
var t T
tType := reflect.TypeOf(t)
n := tType.Size()
This run-time reflection works on a value of the type T. Note that the size does not include the memory indirectly taken by the reference fields: Strings, slices, etc. Warning: for a given program, the size of a type is not the same on a 32-bit machine or a 64-bit machine.
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."
<T> int bytes(T t) {
String s = t.getClass().getSimpleName();
return switch (s) {
case "Boolean" -> 1;
case "Byte" -> Byte.BYTES;
case "Short" -> Short.BYTES;
case "Character" -> Character.BYTES;
case "Integer" -> Integer.BYTES;
case "Float" -> Float.BYTES;
case "Long" -> Long.BYTES;
case "Double" -> Double.BYTES;
default -> -1;
};
}