How does Generic<T>.Clear work
Hi, could you please explain how the Generic<T>.Clear method works? Does it instantly remove data from memory or not? When does the cleanup occur, and can this be controlled?
17 Replies
What is this referring to? Where are you seeing that method? Is this from a package or something?
that's not from the BCL, so it's from some library you're using that you should look up the documentation for
but in general you do not control when memory is freed in C#
Only thing that comes to mind that'd be even close, is
List<T>.Clear()
from System.Collections.Generic
namespace
That what you meant?It doesn't deallocate, it gets cleaned up by the GC
It just sets the count to 0
The programmer shouldn't control when the GC does its thing usually
You're right, and I apologize for the typo in the word "Generic." My question is about how the
Are the data cleared from memory immediately after the
I understand that memory deallocation is handled by the garbage collector (GC), but is it possible to influence its work in some way without disrupting its functioning?
Clear
method works in List<T>
, Queue<T>
, and so on.Are the data cleared from memory immediately after the
Clear
method is executed? Can this process be controlled?I understand that memory deallocation is handled by the garbage collector (GC), but is it possible to influence its work in some way without disrupting its functioning?
it just removes references to the objects in the underlying storage and the GC will collect them at some point in the future
you can force a collection but you generally shouldn't, why are you asking?
yes
I'm interested in this and I want to understand it
there's nothing special about the clear methods, it's the same behavior for any object that is no longer referenced
you can look up how the GC works here: https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/fundamentals
Fundamentals of garbage collection - .NET
Learn how the garbage collector works and how it can be configured for optimum performance.
the simple answer, as jimmacle said, is that it tracks when an object isn't being referenced any more and destroys it
more specifically it does generational sweeps to prioritise clearing up small frequent allocations
generally you don't want to try to game the GC. it's not worth it, it's smarter than you
I'm comparing C and C++, where memory allocation and deallocation are managed manually, with C#, where this task is handled by the Garbage Collector (GC). It seems to me that this approach is not very flexible. Or am I wrong?
gaming the GC includes things like setting fields to null in the hope that will make them get deallocated quicker
in a sense it's ultimately flexible, since the GC lets you pretend you have a machine with infinite memory
and you can always control the performance impact of the GC by just avoiding heap allocations
but yes, you don't get to say when an object is deleted from memory
for things like file handles, and other non-memory resources, you do get deterministic disposal. that's handled by the IDisposable interface
So I can't force GC to delete the object then how do I need to?
you force the GC to delete an object by removing all references to the object
you can call methods like GC.Collect to trigger a sweep, but it's generally not the right choice - for performance if nothing else
Thank you for providing the link, I'll do my best to understand the GC.
no prob, have fun!
You can allocate memory manually, it's just that you're going back to monkey land if you do that. All of the standard library relies on the GC