w0051977
w0051977

Reputation: 15787

Releasing memory in .NET - lists

I read a post online earlier (not on StackOverflow), which stated that you should set lists (http://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.110%29.aspx) to nothing once you have finished with them. I cannot see any benefit whatsoever of doing this.

If list implemented IDisposable then I would wrap them in Using statements but they do not so I do not.

This has confused me a little bit. I use to set variables to nothing once I had finished with them as I was under the false impression that this would encourage the garbage collector to free up the resource more quickly. Is there ever a scenario where you should set a variable to nothing? and more specifically does this apply to lists?

Upvotes: 1

Views: 959

Answers (1)

Victor Zakharov
Victor Zakharov

Reputation: 26414

As far as I know setting a variable to Nothing never helps, even with COM objects, where you need to do Marshal.ReleaseComObject. Lists are .NET objects, so they don't need any memory management. Unless you are experiencing memory issues. Then you may need to force garbage collection sooner / more often etc. Never had this opportunity in practice though.

Good thing you can do is always maintain a scope of your variables. If you don't need them to exist throughout the lifecycle of the application, declare them inside subs and functions where are used.

Upvotes: 1

Related Questions