Click here to Skip to main content
15,915,611 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I have object 'class1object' that is a member of class2. I am initializing it as shown below.

C#
class2{

class1 [] class1object1;

public class2()
{
 class1object1=new class1[1000000];
}

public void Start()
{
}
}
I am using Class2object in another function Start in Class3 and calling it a 100 times.


C#
Class3
{
public static void Start()
{
      for(int i=0;i<100;i++)
    {  Class2 class2object=new Class2();
      class2object.Start();
     }
}
}

Finally the function Start in Class3 is called from a class4 as a static call.
C#
Class4
{
  void Start()
{
   Class3.Start();
}

}
Now the problem I am seeing is the array class1object1 i never cleared and keeps growing in memory. I used Redgate profiler to observer this. the size of class1object1 is 4MB and every run, the memory used by this object increases by 4MB. Why isnt garbage collection kicking in some point? I got it running till the it used about 500MB of memory. There are no unmanaged members in class object1 even thought its in an unsafe code object.


Any ideas or suggestions?

What I have tried:

I tried redgate profiler. I tried to use unmanaged memory in c# but couldnt do it with stackalloc because i need to store the variables in a function and pass it to another function.
Posted
Updated 18-Jun-16 12:13pm
v3

1 solution

Short question is: this is not how memory management works. I would guess, your misconception stems from experience in classical old C++, where going out of the function scope really cause destruction.

In .NET, you are dealing with memory management based on GC; and object destruction is also based on this architecture. The destruction and reclaiming of memory is based on the concept of reachable or unreachable references, which is a pretty complication criterion. When some reference becomes unreachable, object destruction follows, but not immediately. Rather, it happens according to the GC's internal behavior; so you don't have control over the moment of time when the destructor is called and memory is reclaimed. That's why destructors are pretty rarely written in .NET applications.

This is explained, for example, here: Garbage collection (computer science) — Wikipedia, the free encyclopedia.

Another, related problem is the problem of possible memory leaks. First of all, "detection" of memory leak is often a false-positive observation. Do you draw your conclusion from Windows Task Manager? If so, don't trust it.

Memory leaks are still possible, but the whole concept is not as trivial. The "accidental" leaks, very usual due to unmanaged code bugs, are very unlikely. But they can come from mistakes in general code design. I discussed this problem in my past answers:
Best way to get rid of a public static List Causing an Out of Memory,
deferring varirable inside the loop can cuase memory leak?,
Garbage collectotion takes care of all the memory management,
Memory management in MDI forms,
Memory leak in WPF DataBinding.

—SA
 
Share this answer
 
v2
Comments
BillWoodruff 18-Jun-16 23:42pm    
+5 excellent analysis
Sergey Alexandrovich Kryukov 19-Jun-16 1:39am    
Thank you, Bill.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900