+2 votes

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?

in Engine by (95 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

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.

by (27,897 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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.