Csharp/CSharp Tutorial/Development/GC
Содержание
Clean up gen 0
<source lang="csharp">using System; public class MyClass : IDisposable {
~MyClass() { Console.WriteLine("In destructor!"); } public void Dispose() { Console.WriteLine("In Dispose() for {0}!"); GC.SuppressFinalize(this); }
} public class MainClass {
public static void Main(string[] args) { MyClass c1, c2, c3, c4; c1 = new MyClass(); c2 = new MyClass(); c3 = new MyClass(); c4 = new MyClass(); Console.WriteLine("\n***** Disposing c1 and c3 *****"); c1.Dispose(); c3.Dispose(); Console.WriteLine("***** Sweaping gen 0 *****"); GC.Collect(0); Console.WriteLine("\n***** New generations *****"); Console.WriteLine("C1 is gen {0}", GC.GetGeneration(c1)); Console.WriteLine("C2 is gen {0}", GC.GetGeneration(c2)); Console.WriteLine("C3 is gen {0}", GC.GetGeneration(c3)); Console.WriteLine("C4 is gen {0}", GC.GetGeneration(c4)); }
}</source>
***** Disposing c1 and c3 ***** In Dispose() for {0}! In Dispose() for {0}! ***** Sweaping gen 0 ***** ***** New generations ***** C1 is gen 1 C2 is gen 1 C3 is gen 1 C4 is gen 1 In destructor! In destructor!
Display current generations
<source lang="csharp">using System; public class MyClass : IDisposable {
~MyClass() { Console.WriteLine("In destructor!"); } public void Dispose() { Console.WriteLine("In Dispose() for {0}!"); GC.SuppressFinalize(this); }
} public class MainClass {
public static void Main(string[] args) { MyClass c1, c2, c3, c4; c1 = new MyClass(); c2 = new MyClass(); c3 = new MyClass(); c4 = new MyClass(); Console.WriteLine("\n***** Displaying generation of cars *****"); Console.WriteLine("C1 is gen {0}", GC.GetGeneration(c1)); Console.WriteLine("C2 is gen {0}", GC.GetGeneration(c2)); Console.WriteLine("C3 is gen {0}", GC.GetGeneration(c3)); Console.WriteLine("C4 is gen {0}", GC.GetGeneration(c4)); }
}</source>
***** Displaying generation of cars ***** C1 is gen 0 C2 is gen 0 C3 is gen 0 C4 is gen 0 In destructor! In destructor! In destructor! In destructor!
Forced garbage collection
<source lang="csharp">using System; class Junk {
public Junk() { Console.WriteLine("Created Junk"); } ~Junk() { Console.WriteLine("Destroyed Junk"); }
} class MainClass {
public static void Main() { Junk j = new Junk(); j = null; GC.Collect(); }
}</source>
Created Junk Destroyed Junk
Garbage collection type
<source lang="csharp">using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Text; using System.Security.Cryptography; public class MainClass {
public static void Main() { Console.WriteLine("TotalMemory: {0}", GC.GetTotalMemory(false)); Console.WriteLine("(forcing a GC...)"); GC.Collect(); Console.WriteLine("TotalMemory: {0}", GC.GetTotalMemory(false)); }
}</source>
TotalMemory: 141036 (forcing a GC...) TotalMemory: 153444
Get total memory
<source lang="csharp">using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Text; using System.Security.Cryptography; public class MainClass {
public static void Main() { Console.WriteLine("TotalMemory: {0}", GC.GetTotalMemory(false)); Console.WriteLine("(forcing a GC...)"); GC.Collect(); Console.WriteLine("TotalMemory: {0}", GC.GetTotalMemory(false)); }
}</source>
TotalMemory: 141036 (forcing a GC...) TotalMemory: 153444
weak references
<source lang="csharp">using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Text; using System.Security.Cryptography; public class MainClass {
public static void Main() { object target = new object(); WeakReference wr = new WeakReference(target); Console.WriteLine("IsAlive: {0}", wr.IsAlive); GC.Collect(); Console.WriteLine("IsAlive: {0}", wr.IsAlive); }
}</source>
IsAlive: True IsAlive: False