Prev: Tracing inside method
Next: Drawing Board (White Board)
From: Anders Eriksson on 26 May 2010 05:02 Hello, I wonder do I create a memory leak by using private void myFunc() { List<String> joblist = new List<string>(); // ... add a alot of string into the joblist } and then call myFunc "repeatedly" from the main program It feels like I should return the memory somehow, but how? // Anders -- English is not my first language so any error, insults or strangeness has happend during the translation. Please correct my English so that I may become better at it!
From: Jackie on 26 May 2010 06:01 On 5/26/2010 11:02, Anders Eriksson wrote: > Hello, > > I wonder do I create a memory leak by using > > private void myFunc() > { > List<String> joblist = new List<string>(); > // ... add a alot of string into the joblist > } > > and then call myFunc "repeatedly" from the main program > > It feels like I should return the memory somehow, but how? > > // Anders > If I'm not wrong, you don't need to worry about it as the garbage collector should take care of releasing that memory. But please note that on some things, you should call "Dispose" or "Close" (just check on MSDN). I thought I had an issue some time earlier when my application seemed to keep eating up memory, and at some point it suddenly released the memory.
From: Harlan Messinger on 26 May 2010 06:08 Anders Eriksson wrote: > Hello, > > I wonder do I create a memory leak by using > > private void myFunc() > { > List<String> joblist = new List<string>(); > // ... add a alot of string into the joblist > } > > and then call myFunc "repeatedly" from the main program > > It feels like I should return the memory somehow, but how? > > // Anders Managed .NET code uses garbage collection to return memory automatically at some point after it becomes unreachable by your code. In cases where that isn't happening soon enough, you can trigger collection explicitly. See http://www.developer.com/net/csharp/article.php/3343191/C-Tip-Forcing-Garbage-Collection-in-NET.htm
From: Göran Andersson on 26 May 2010 09:39 On 2010-05-26 11:02, Anders Eriksson wrote: > Hello, > > I wonder do I create a memory leak by using > > private void myFunc() > { > List<String> joblist = new List<string>(); > // ... add a alot of string into the joblist > } > > and then call myFunc "repeatedly" from the main program > > It feels like I should return the memory somehow, but how? > > // Anders > The memory is returned automatically. The garbage collector regains the used memory some time after you don't use the objects any more. Objects that needs to clean up unmanaged resources implements the IDisposable interface so that you can use the Dispose method to tell the object when to free the resources. Both the List<> and String classes are fully managed types, so you don't have to do anything when you stop using them. -- G�ran Andersson _____ http://www.guffa.com
From: Arne Vajhøj on 26 May 2010 20:36
On 26-05-2010 05:02, Anders Eriksson wrote: > I wonder do I create a memory leak by using > > private void myFunc() > { > List<String> joblist = new List<string>(); > // ... add a alot of string into the joblist > } > > and then call myFunc "repeatedly" from the main program > > It feels like I should return the memory somehow, but how? As soon as the data is not reachable then the data is eligible for garbage collection. When the CLR think it is a good time to do garbage collection (like if it needs the memory), then it will garbage collect the data. Arne |