From: Arne Vajhøj on
On 26-05-2010 20:36, Arne Vajh�j wrote:
> 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.

To illustrate the asynchness of GC try run the following:

using System;
using System.Collections.Generic;

namespace E
{
public class Big
{
private byte[] b;
public Big()
{
Console.WriteLine("Allocating 10 MB");
b = new byte[10000000];
}
~Big()
{
Console.WriteLine("Soon to free 10 MB");
}
}
public class Program
{
public static void F()
{
List<Big> lst = new List<Big>();
lst.Add(new Big());
lst.Add(new Big());
lst.Add(new Big());
lst.Add(new Big());
lst.Add(new Big());
}
public static void Main(string[] args)
{
for(int i = 0; i < 50; i++)
{
F();
}
Console.ReadKey();
}
}
}

Arne
From: Arne Vajhøj on
On 26-05-2010 21:14, Arne Vajh�j wrote:
> On 26-05-2010 20:36, Arne Vajh�j wrote:
>> 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.
>
> To illustrate the asynchness of GC try run the following:
>
> using System;
> using System.Collections.Generic;
>
> namespace E
> {
> public class Big
> {
> private byte[] b;
> public Big()
> {
> Console.WriteLine("Allocating 10 MB");
> b = new byte[10000000];
> }
> ~Big()
> {
> Console.WriteLine("Soon to free 10 MB");
> }
> }
> public class Program
> {
> public static void F()
> {
> List<Big> lst = new List<Big>();
> lst.Add(new Big());
> lst.Add(new Big());
> lst.Add(new Big());
> lst.Add(new Big());
> lst.Add(new Big());
> }
> public static void Main(string[] args)
> {
> for(int i = 0; i < 50; i++)
> {
> F();
> }
> Console.ReadKey();
> }
> }
> }

Well - strictly speaking it just show the asynchness
of finalization.

But GC will be just as asynch.

Arne
From: Anders Eriksson on
"Anders Eriksson" <andis.eriksson(a)gmail.com> skrev i meddelandet
news:eJt$6IL$KHA.980(a)TK2MSFTNGP04.phx.gbl...
> Hello,
>
> I wonder do I create a memory leak by using
>
Now that is an improvement over C++!

I like C# more and more...

Thank you everyone for answering!

// Anders