Get size of an object (bytes)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By DrewS

Is there a sizeof function or some way we can see how much memory an object is taking up in memory, say an array of integers or a dict?

:bust_in_silhouette: Reply From: Zylann

There isn’t a sizeof for specific objects in GDScript, to my knowledge. However, you can infer it by looking at the memory usage in the Debugger Monitors tab while running the game.

If your object is simple, you can also approximate it. For example, an Array (or []) contains Variant items. Variant size is about 32 bytes, so you can do count * 32.
For PoolIntVector, items are type 32-bit int, so it would be count * 4.

Another way might be to do the following:

var memory_before = OS.get_static_memory()
var obj =
var memory_used = OS.get_static_memory() - memory_before

Even this might be a little off because of GDScript’s execution itself in addition to your object, but that’s a way to get an idea.

Someone could be tempted to use var2bytes but this function actually encodes an object for later decoding rather than just dumping its exact memory.