using new List<String>();

A

Anders Eriksson

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!
 
J

Jackie

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

Harlan Messinger

Anders said:
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
 
G

Göran Andersson

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

Arne Vajhøj

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
 
A

Arne Vajhøj

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
 
A

Arne Vajhøj

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
 
A

Anders Eriksson

Anders Eriksson said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top