Reputation: 1
I have a quick question here. Can some of one advise me…
I found some memory leak with some controls when I use it in the following use case. To replicate this I just prepared a simple console application to check it out. When I define a different classes as “MyClass” and “Class1” alternatively to the same object “a”. The number of instance keep on increases for every time and the previous instance does not get disposed. This can be notified using Some Profiler like “Ants Profiler” with the following replication procedure.
Hint: Press c to continue. It will assign next object.
[Code snippet]
namespace ConsoleApplication3
{
class Program : IDisposable
{
static void Main(string[] args)
{
object a;
string s;
s = Console.ReadLine();
if (s == "c")
a = new Class1();
Console.WriteLine("a === Class1 ");
s = Console.ReadLine();
if (s == "c")
a = new MyClass();
Console.WriteLine("a ==== MyClass");
// Take a memory snap shot here. We can see only one instance created for both class.
s = Console.ReadLine();
if (s == "c")
a = new Class1();
Console.WriteLine("a === Class1 ");
s = Console.ReadLine();
if (s == "c")
a = new MyClass();
Console.WriteLine("a ==== MyClass");
s = Console.ReadLine();
if (s == "c")
a = new Class1();
Console.WriteLine("a === Class1 ");
s = Console.ReadLine();
if (s == "c")
a = new MyClass();
Console.WriteLine("a ==== MyClass");
s = Console.ReadLine();
if (s == "c")
a = new Class1();
Console.WriteLine("a === Class1 ");
s = Console.ReadLine();
if (s == "c")
a = new MyClass();
Console.WriteLine("a ==== MyClass");
s = Console.ReadLine();
if (s == "c")
a = new Class1();
Console.WriteLine("a === Class1 ");
s = Console.ReadLine();
if (s == "c")
a = new MyClass();
Console.WriteLine("a ==== MyClass");
s = Console.ReadLine();
if (s == "c")
a = new Class1();
Console.WriteLine("a === Class1 ");
s = Console.ReadLine();
if (s == "c")
a = new MyClass();
Console.WriteLine("a ==== MyClass");
s = Console.ReadLine();
if (s == "c")
a = new Class1();
Console.WriteLine("a === Class1 ");
s = Console.ReadLine();
if (s == "c")
a = new MyClass();
Console.WriteLine("a ==== MyClass");
s = Console.ReadLine();
if (s == "c")
a = new Class1();
Console.WriteLine("a === Class1 ");
s = Console.ReadLine();
if (s == "c")
a = new MyClass();
Console.WriteLine("a ==== MyClass");
s = Console.ReadLine();
if (s == "c")
a = new Class1();
Console.WriteLine("a === Class1 ");
s = Console.ReadLine();
if (s == "c")
a = new MyClass();
Console.WriteLine("a ==== MyClass");
s = Console.ReadLine();
if (s == "c")
a = new Class1();
Console.WriteLine("a === Class1 ");
s = Console.ReadLine();
if (s == "c")
a = new MyClass();
Console.WriteLine("a ==== MyClass");
// Take a snap shot here multiple instance are created for both the class.
Console.WriteLine("End of the prog");
Console.ReadLine();
}
public void Dispose()
{
GC.SuppressFinalize(this);
}
}
}
Please let me know this is the property behavior to define a class.
Thanks Kamal.
Upvotes: 0
Views: 586
Reputation: 3848
To be sure that all answers are correct, force garbage collection before getting the second snapshot
GC.Collect();
GC.WaitForPendingFinalizers();
Upvotes: 0
Reputation: 6955
The previous instances will not be removed from memory immediately when you assign the variable to a new class instance - the instances will get removed when garbage collection kicks in. So I don't think you have a memory leak in this code.
But - why are you calling SuppressFinalize in the programs Dispose method?
Upvotes: 4
Reputation: 9814
I think that this is not memory leak - GC will collect memory after some time, not immedietly. So for short time it is still in memory and you have few instances.
Upvotes: 2