Help with the Garbage Collector

  1. What do you want to achieve? Keep it simple and clear!
    I want to find out if the garbage collector can delete values from ObjectValues if memory is too low. Currently my system is using a bit of pagefile when running a Play Test. The ObjectValue is created locally, and is never used in a server context.
  2. What is the issue? Include screenshots / videos if possible!
    The GC (I think it is the GC) keeps deleting values from an ObjectValue.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have had to create a dedicated if statement just to check whether the value is not equal to nil.
1 Like

The ObjectValue is part of the data model so it shouldn’t be getting cleared

2 Likes

If it’s being referenced anywhere then it shouldn’t be GC’ed. I don’t know much about garbage collection in Roblox but my intuition tells me instances wouldn’t get cleared unless they are both destroyed and de-referenced.

Roblox Instances are not Lua objects. When you index an Instance in Lua, you are actually obtaining a userdata that provides interfacing to the actual instance on the C side.

This means if Lua decides to GC the instance userdata proxy, it wont remove it from the ObjectValue, though you’ll get a new copy of the userdata when you next retrieve it.

Also Roblox does not delete any instances if memory is limited. It just crashes.

I wonder if this has to do with maybe Animations being GC’d. I am using object values to store loaded animations (this is due to the fact that I am using a single module script to play almost 20 animations). Can an animation that gets unloaded also set an ObjectValue’s Value to nil?

local ObjectValue = Instance.new("ObjectValue")
local Part = Instance.new("Part")
ObjectValue.Value = Part
Part:Destroy()
print(ObjectValue.Value.Name) --Part

You need to handle 'nil’ing ‘ObjectValue’ objects manually.