Do constants use up memory in object instances?

uaknightuaknight Posts: 35Member

I have noticed that if you define enums and constants in a class, and then create several instances of that class, the constants are showed in the inspector at runtime, in every instance. See screenshot.

I hope that this is really for convenience purposes, and that no additional memory is allocated for those constants in each and every instance?

Answers

  • MegalomaniakMegalomaniak Posts: 2,654Admin

    If it's not a reference from elsewhere but local to the instance then I would presume so, yes.

  • uaknightuaknight Posts: 35Member

    Hm, that's bad news, and really not what I had expected.

    If I create a constant like this:

    const TEST_INT = 9

    my intention is that the value should be shared among all instances, and that the value only should be allocated once, on the class level.

    If you put a lot of constants in a class (in the belief that they are only allocated once, for the class), and create a lot of instances, you will end up with much unnecessary allocated data in memory, which are all copies. For example a million allocations of the value 9.

    There also wouldn't be any practical difference between using const declarations and var declarations. You could as well use varall the time.

    If this really is true, you would want to put all your enum and const declarations in common singleton classes.

    I think I'll need to create a simple test program to find out because this is important.

  • uaknightuaknight Posts: 35Member

    I have now tested and it seems that you are correct, Megalomaniac. Constants are really allocated in each and every instance .

    I made a program which creates 10 000 copies of a class. In the first test the class contains a const declaration, in the second a var declaration.

    It turns out that the program's memory allocation is almost the same.

    Here's the test program:

    extends Node2D
    
    export (PackedScene) var test_class_template
    
    func _ready():
        var test_class_template = load("res://TestClass.tscn")
        for i in 10000:
            var test_class = test_class_template.instance()
            add_child(test_class)       
        pass # Replace with function body.
    

    And here 's the two screenshots:

    So it's back to making a massive re-engineering of my project. Duh! :s

  • uaknightuaknight Posts: 35Member

    No, wait, I forgot the overhead of the other data of the class node. And there's actually a difference, 40.30 vs 38.77. I need to put more data in the const value.

  • uaknightuaknight Posts: 35Member

    So, I tested again using an array containing about 1000's ints. And now the difference is showing clearly.

    Using const: 38,8 Mb
    Using var: 353,0 Mb

    extends Node2D
    class_name TestClass
    
    const test_int = [\
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,\
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,\
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,\
     ...
    

    So , that was a relief... =)

Sign In or Register to comment.