Question

I have a program running on [STAThread] in C#.

The program periodically creates instances of a class X and adds them to a list.
After a period of time, the tasks that these instances of X are required to perform are completed and the instance of the class is no longer needed.
Since I am running on a single thread, and many of these instances of X could be running at the same time and draining resources even though they are no longer needed, I would like to free resources when I know an instance of X is done with, so that my program doesn't slow down.

My code looks like:

 public partial class Form1 : Form
 {
    List<X> myList = new List<X>();

    ...
 }

I am wondering if:

  1. Performing the operation myList.RemoveAt(0) will remove all trace of the first instance of the class X from memory.
  2. If there is a better way to do this.

Thanks! Chris

Was it helpful?

Solution

Removing the class from the list will not remove all trace of it from memory no. C# is a managed language with a garbage collector. Whenever a particular object is no longer being referenced from any location that is considered "active" (meaning that the reference can still be accessed in code somehow) it is * eligible* for garbage collection. That simply means that at some indeterminate time in the future the GC is free to clear up that object's memory. It could be right away, or it could be a long time. Fortunately, you almost never need to worry about this; you just need to make sure that you're not holding onto references of objects that consume a large amount of memory once you no longer need them.

In your case, you haven't given very many details. Without knowing anything about class X, I'd say odds are it doesn't consume all that much memory and that your list probably isn't that large. Given that, as long as this isn't some long running task that will be going for days, it's probably not a big deal if it goes a little longer than it should without being freed.

If those objects are consuming a lot of memory (if they contain the results of large database queries, the contents of large files, etc.) then I'd ask why you have the list in the first place? You create a list of the type, and you state that it's being used in a background thread. Is that entire list passed to a background thread? Just certain items? Chances are if it's being passed to a background thread then you no longer need a reference to it in the main thread at all (although you'd need to show more code to say with certainty).

OTHER TIPS

No, myList.RemoveAt(0); only removes the reference to your object from the list. But the Garbage Collector (GC) will then remove your complete object, since there are no references to it anymore.

If you do not use unmanaged memory, just trust the GC, you don't know when he will remove the object exactly, but that's ok, just let it be.

If you use unmanaged memory in your class X (COM objects, file handles, etc. essentially anything that has an Dispose() method), it is not enough to remove the reference to the object. In this case, implement the IDisposable interface in your class X and write a Dispose method that will call the Dispose methode of the objects you use in X. Then change your code to

myList[0].Dispose();
myList.RemoveAt(0);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top