Description
Godot version
4.1.1.stable
System information
Godot v4.1.1.stable - Windows 10.0.22621 - Vulkan (Compatibility) - NVIDIA GeForce RTX 3070 Laptop GPU (NVIDIA; 31.0.15.3141) - Intel(R) Core(TM) i7-10870H CPU @ 2.20GHz (16 Threads)
Issue description
I was perplexed with my game's memory usage. Basically, I have a dictionary of tile objects, the keys are all Vector2i.
Even fairly small maps (180x180 / ~32,400 tiles) were taking over 2GB of memory. I was considering switching over to dictionaries instead of using objects...
But, I decided to try using RefCounted as Tile's base type instead of Object, and the game's memory usage dropped from over 2GB to just around 80MB. Even if I made the tiles Nodes and added them as children of the test scene, the game's memory usage was sitting at 120MB.
I used Object thinking it would be the most efficient solution, only to be faced with this bizarre behavior. I'd think this must be a bug, there's no other explanation for this behavior. I thought it might be something to do with how they're allocated in the context of a dictionary or something like that, but nope, pushing them all into an array presents the same issue. Just changing the base type of the Tile
class (which at this point is empty, by the way) to RefCounted or Node totally fixes the issue.
Steps to reproduce
Make a scene and two scripts.
The first script should just look like this:
class_name MyObject
extends Node
Then, in the scene's script, paste this in:
extends Node
var objects = []
func _ready():
for i in 32400:
var object = MyObject.new()
objects.append(object)
print(objects.size())
The memory usage should be fine. Even better if RefCounted is used.
But try switching the base type of MyObject to Object, and if there's nothing wrong with my machine, your memory usage should soar to over 2GB and the scene take significantly longer to load.
Minimal reproduction project
N/A