Csharp/CSharp Tutorial/Development/GC

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Clean up gen 0

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));
  }
}
***** 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

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));
  }
}
***** 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

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();
  }
}
Created Junk
Destroyed Junk

Garbage collection type

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));
    }
}
TotalMemory: 141036
(forcing a GC...)
TotalMemory: 153444

Get total memory

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));
    }
}
TotalMemory: 141036
(forcing a GC...)
TotalMemory: 153444

weak references

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);
    }
}
IsAlive: True
IsAlive: False