Weird Garbage Collection behaviour with finalizers

View: New views
2 Messages — Rating Filter:   Alert me  

Weird Garbage Collection behaviour with finalizers

by Urs Hanselmann :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi!

I have downloaded the Mono 2.0 preview release today to make some performance comparisions with the MS Runtime. So I wrote a simple application to test massive object creation and the garbace collection auto-cleanup functionality:

using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;

namespace GarbageCollectionTest
{
class TestClass
{
private string mData;
public TestClass(int i)
{
mData = i.ToString();
}
/*~TestClass()
{
}*/
};

class Program
{
static void Main(string[] args)
{
DateTime start = DateTime.Now;
TestClass class1 = null;
for (int i = 0; i < 10000000; ++i)
{
class1 = new TestClass(i);
class1 = null;
}
Console.WriteLine("time elapsed: {0}", DateTime.Now.Subtract(start));
Console.ReadKey();
}
}
}

When I run this with the Microsoft Runtime it takes 2250ms. With the mono runtime it took 4000ms. The next thing I tried was to add an finalizer to my test class. I started the application with the MS Runtime and it took 5700 ms. I started the same application with mono and it took 2 minutes while the memory usage was raising continually.

I think that there is a memory leak somewhere in the Mono GC implementation or is there something about the Mono GC that is different to the MS one I missed?

Thanks & Regards,
Urs

Re: Weird Garbage Collection behaviour with finalizers

by Urs Hanselmann :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I solved the memory "leak" by adding
GC.WaitForPendingFinalizers();
inside the loop, but shouldn't this done by the GC internally?
The run time reduced to 20 seconds, but that is 5 times more than without finalizer! Without an EMPTY finalizer.