C
C#5d ago
Podreju

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
ero
ero5d ago
What is this referring to? Where are you seeing that method? Is this from a package or something?
Jimmacle
Jimmacle5d ago
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#
Angius
Angius5d ago
Only thing that comes to mind that'd be even close, is List<T>.Clear() from System.Collections.Generic namespace That what you meant?
Anton
Anton5d ago
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
Podreju
PodrejuOP5d ago
You're right, and I apologize for the typo in the word "Generic." My question is about how the 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?
Jimmacle
Jimmacle5d ago
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?
Podreju
PodrejuOP5d ago
yes I'm interested in this and I want to understand it
Jimmacle
Jimmacle5d ago
there's nothing special about the clear methods, it's the same behavior for any object that is no longer referenced
becquerel
becquerel5d ago
Fundamentals of garbage collection - .NET
Learn how the garbage collector works and how it can be configured for optimum performance.
becquerel
becquerel5d ago
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
Podreju
PodrejuOP5d ago
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?
becquerel
becquerel5d ago
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
Podreju
PodrejuOP5d ago
So I can't force GC to delete the object then how do I need to?
becquerel
becquerel5d ago
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
Podreju
PodrejuOP5d ago
Thank you for providing the link, I'll do my best to understand the GC.
becquerel
becquerel5d ago
no prob, have fun!
Anton
Anton5d ago
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

Did you find this page helpful?