kettlette
kettlette
CC#
Created by kettlette on 3/14/2024 in #help
Making a field eligible for GC
recently i've been working on a small game engine and i'm trying to call the GC to free my entities after i unload a world (i'm trying to trigger finalizers which will then make opengl resources eligible for disposal and then reclaimed through a seperate method) i was expecting for when i set my field _entities to null that it would become eligible for GC but the following code always shows the list is alive
// _entities is a List<T>
WeakReference listRef = new(_entities);
_entities = null!;

GC.Collect();
GC.WaitForPendingFinalizers();

Console.WriteLine($"list is alive? {listRef.IsAlive}");
// _entities is a List<T>
WeakReference listRef = new(_entities);
_entities = null!;

GC.Collect();
GC.WaitForPendingFinalizers();

Console.WriteLine($"list is alive? {listRef.IsAlive}");
how can i make _entities eligible to be GCed? (it's a private variable which is not ever passed to outside of the class)
6 replies