Memory leak regarding table and destroyed instance as index/key

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    This is more of a wondering is this a bug or intentional. I thought that there would be a system in-place for destroyed instance to set table key to nil.

  2. What is the issue?
    Unintended
    image
    Intended
    image

  3. What solutions have you tried so far?
    I already found the solution by removing the key manually,

I had never thought that instance would cause memory leak as a key index until I check why my script activity suddenly increasing overtime.

This is intentional, using anything as a key is considered a ā€œstrongā€ reference, so it will never be garbage collected

You might be able to get around this behaviour by setting the tableā€™s metatableā€™s __mode to ā€˜kā€™, but itā€™s probably best to just t[attachment] = nil

1 Like

I appreciate the answer, though I wish luau improve on the garbage collecting around tables that kept over a long period of time. (Considering it part of a game* engine)

This is just the nature of the garbage collector unfortunately, canā€™t really be addressed. The garbage collector can only garbage collect what it can safely deem garbage (which happens when there are no lingering references to that object).

Luau canā€™t really ever safely assume that an object is garbage if there is still a reference to it (if itā€™s used as a key, value, or in a variable that is still being accessed). However as I mentioned above, setting the __mode to ā€˜kā€™, then using the object as a key will treat it as a weak reference, and if no other strong references exist, will allow it to be gcā€™d. This is also how you can detect whether or not an object is being garbage collected

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.