Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How free memory used by a large list in C#?

I have a list called Population, is a great list of very many positions and at some point I stop using it. How I can free the resources? Then this is part of the code:

 private List <BasePopulation> Population=new List <BasePopulation>();  Population.SomeMethod();  Population.Clear(); 

I used the Clear method, but not works. Any idea?

like image 414
Esneyder Avatar asked Jul 30 '13 19:07

Esneyder


People also ask

How do you clear a list of objects?

The remove(Object obj) method of List interface in Java is used to remove the first occurrence of the specified element obj from this List if it is present in the List. Parameters: It accepts a single parameter obj of List type which represents the element to be removed from the given List.

Which list is faster in C#?

BinarySearch will be faster than List<T>.

How objects are stored in memory in C#?

In C# there are two places where an object can be stored -- the heap and the stack. Objects allocated on the stack are available only inside of a stack frame (execution of a method), while objects allocated on the heap can be accessed from anywhere.

Are C# arrays contiguous?

It is used to store a data collection, but the array can be considered a collection of variables of the same type stored at contiguous memory locations. All arrays consist of contiguous memory locations, with the lowest address corresponds to the first element and the highest address to the last element.


2 Answers

The problem may be that Clear is not doing what you think it is. Clear simply marks the List as being empty without resizing the internal array that it uses behind the scenes. It will, however, remove all references to the individual BasePopulation instances. So if no other data structure has a reference to them they will be eligible for garbage collection. But, it will not reduce the size of the List directly. I just verified this using ILSpy.

You have two options.

  1. Set Population = null. This will unroot the entire object instance making it eligible for garbage collection.

  2. Call TrimExcess on this List. This will resize the internal array.

like image 197
Brian Gideon Avatar answered Sep 16 '22 14:09

Brian Gideon


Well, since the garbage collector (GC) is taking care of the memory management for you, the first thing you can do is getting rid of all references to the list (and the contained elements) so that the GC is able to remove it on the next occasion. You can do this, for example, by explicitly setting

Population = null; 

If this isn't enough for you, for example because you're really eager to get rid of the objects now and you can accept non-optimal run-time behaviour, you can tell the GC to start collecting objects now via

GC.Collect(); 

More information about this method can be found here.

As indicated above, this practice can induce a performance penalty since it forces the GC to clean up resources at a point in the program where it usually wouldn't. Directly calling the method is thus often discouraged, but it might serve your needs if this is really a special point in your application. As a practical example, I've successfully improved peak memory usage in a program that requires a lot of objects during an initialization that can be discarded once the actual program execution has started. Here, the small performance penalty for calling GC.Collect() after the initialization was justifiable.

like image 42
bigge Avatar answered Sep 17 '22 14:09

bigge