© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
5 replies
kettlemug

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
_entities
to
null
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
_entities
eligible to be GCed? (it's a private variable which is not ever passed to outside of the class)
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ OOM well below GCHeapLimit (Could not do a full GC) since .NET7
C#CC# / help
4y ago
GC Troubles
C#CC# / help
14mo ago
Making a map for restaurant
C#CC# / help
15mo ago