Csharp/C Sharp by API/System/GC
Содержание
GC.AddMemoryPressure
<source lang="csharp"> using System; using System.IO; using System.Reflection; using System.Runtime.ConstrainedExecution; using System.Runtime.InteropServices; using Microsoft.Win32.SafeHandles; public class MainClass {
public static void Main() { IntPtr ptr = Marshal.AllocHGlobal(1024); GC.AddMemoryPressure(1024); if (ptr != IntPtr.Zero) { Marshal.FreeHGlobal(ptr); ptr = IntPtr.Zero; GC.RemoveMemoryPressure(1024); } }
}
</source>
GC.GetGeneration
<source lang="csharp"> using System; using System.Collections.Generic; using System.Text; public class Car {
private int currSp; private string petName; public Car() { } public Car(string name, int speed) { petName = name; currSp = speed; } public override string ToString() { return string.Format("{0} is going {1} MPH", petName, currSp); }
} class Program {
static void Main(string[] args) { Console.WriteLine("Estimated bytes on heap: {0}", GC.GetTotalMemory(false)); Console.WriteLine("This OS has {0} object generations.\n", (GC.MaxGeneration + 1)); Car refToMyCar = new Car("Zippy", 100); Console.WriteLine(refToMyCar.ToString()); Console.WriteLine("\nGeneration of refToMyCar is: {0}", GC.GetGeneration(refToMyCar)); } public static void MakeACar() { Car myCar = new Car(); }
}
</source>
GC.GetTotalMemory
<source lang="csharp"> using System; using System.Collections.Generic; using System.Text; class Program {
static void Main(string[] args) { Console.WriteLine("Estimated bytes on heap: {0}", GC.GetTotalMemory(false)); Console.WriteLine("This OS has {0} object generations.\n", (GC.MaxGeneration + 1)); }
}
</source>
GC.MaxGeneration
<source lang="csharp"> using System; using System.Collections.Generic; using System.Text; class Program {
static void Main(string[] args) { Console.WriteLine("Estimated bytes on heap: {0}", GC.GetTotalMemory(false)); Console.WriteLine("This OS has {0} object generations.\n", (GC.MaxGeneration + 1)); }
}
</source>
GC.RemoveMemoryPressure
<source lang="csharp"> using System; using System.IO; using System.Reflection; using System.Runtime.ConstrainedExecution; using System.Runtime.InteropServices; using Microsoft.Win32.SafeHandles; public class MainClass {
public static void Main() { IntPtr ptr = Marshal.AllocHGlobal(1024); GC.AddMemoryPressure(1024); if (ptr != IntPtr.Zero) { Marshal.FreeHGlobal(ptr); ptr = IntPtr.Zero; GC.RemoveMemoryPressure(1024); } }
}
</source>