using new List<String>();

  • Thread starter Thread starter Anders Eriksson
  • Start date Start date
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!
 
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.
 
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
 
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.
 
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
 
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
 
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
 
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
 
Back
Top