High memory footprint of godot arrays, what is happening there?

Godot Version



Here is a very simple test code that creates an Array of int in godot with number of entries and measures the difference in static memory before and after arrray creation:

var mu_start = OS.get_static_memory_usage()
var number = 100000
var output = range(number)
var mu_end = OS.get_static_memory_usage()
print("average int memory usage: ", (mu_end - mu_start) / number)

This means that each of those integers approximately uses 41 bytes of memory, when the expected size is 8 bytes. Increasing/decreasing number does not change much about the result. I know that there is a little bit of array overhead involved, but using 5 times the expected amount seems way too extreme for me. Can somebody tell me what is going on there and if this is some kind of bug in get_static_memory_usage?

I wouldn’t underestimate the overhead. Arrays in godot aren’t the same as arrays in C. Godot has a packed arrays that implies notable overhead in regular arrays. Maybe you could try convert the range to a packed byte array and see how much that counts.

I am unfamiliar with how arrays are stored internally however…
The array is not storing ints it is storing Variants at 20 bytes. (iirc)
(Still doesn’t quite add up though.)
If you change it to PackedInt64Array you get 10 bytes per element:

var number = 100000
var mu_start = OS.get_static_memory_usage()
var a:PackedInt64Array
for n in number: 
var mu_end = OS.get_static_memory_usage()

Good point about the Variant data type. That is really one big downside of not having statically typed arrays. I could use PackedArrays and probably will do so, but this needs an additional encoding and decoding step for me to properly work with the data.