Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does garbage collection happen at the process level or appdomain level?

FullGC normaly pauses all Threads while running. Having two AppDomains, each running several threads. When GC runs, will all threads be paused, or only those of either one AppDomain?

like image 694
sa.he Avatar asked Mar 06 '13 11:03

sa.he


People also ask

What are the phases of garbage collection?

Garbage collection then goes through the three phases: mark, sweep, and, if required, compaction.

How does .NET handle garbage collection internally?

. NET's garbage collector manages the allocation and release of memory for your application. Each time you create a new object, the common language runtime allocates memory for the object from the managed heap.

How do garbage collectors work?

In the common language runtime (CLR), the garbage collector (GC) serves as an automatic memory manager. The garbage collector manages the allocation and release of memory for an application. For developers working with managed code, this means that you don't have to write code to perform memory management tasks.

How does garbage collection take place in Java?

In Java, garbage collection happens automatically during the lifetime of a program. This eliminates the need to de-allocate memory and therefore avoids memory leaks. Java Garbage Collection is the process by which Java programs perform automatic memory management.


1 Answers

Hard to answer, best thing to do is just test it:

using System; using System.Reflection;  public class Program : MarshalByRefObject {     static void Main(string[] args) {         var dummy1 = new object();         var dom = AppDomain.CreateDomain("test");         var obj = (Program)dom.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, typeof(Program).FullName);         obj.Test();         Console.WriteLine("Primary appdomain, collection count = {0}, gen = {1}",             GC.CollectionCount(0), GC.GetGeneration(dummy1));         Console.ReadKey();      }     public void Test() {         var dummy2 = new object();         for (int test = 0; test < 3; ++test) {             GC.Collect();             GC.WaitForPendingFinalizers();         }          Console.WriteLine("In appdomain '{0}', collection count = {1}, gen = {2}",             AppDomain.CurrentDomain.FriendlyName, GC.CollectionCount(0),             GC.GetGeneration(dummy2));     } } 

Output:

In appdomain 'test', collection count = 3, gen = 2 Primary appdomain, collection count = 3, gen = 2 

Good evidence that a GC affects all AppDomains on the default CLR host. This surprised me.

like image 155
Hans Passant Avatar answered Oct 22 '22 03:10

Hans Passant