From: Anders Eriksson on
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
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
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
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
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