Freeing Resources in C#

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


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).

Need Your Help

nginx with flask and memcached returns some garbled characters

python caching nginx memcached flask

I'm trying to cache Python/flask responses with memcached. I then want to serve the cache using nginx. I'm using flask code that looks something like this:

Sum() in python

python sum

I have been trying to be comfortable with the sum() in python, I did understand the basic function of sum but as a mathematica backgroud,I was just inquistive to know can we use sum in python in th...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.