+1 vote

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?

asked Aug 9, 2019 in Engine by DrewS (89 points)

1 Answer

+1 vote

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 = YourObject.new()
var memory_used = OS.get_static_memory() - memory_before
print(memory_used)

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.

answered Aug 9, 2019 by Zylann (26,784 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.