Csharp/C Sharp by API/System/GC
Содержание
GC.AddMemoryPressure
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);
}
}
}
GC.GetGeneration
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();
}
}
GC.GetTotalMemory
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));
}
}
GC.MaxGeneration
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));
}
}
GC.RemoveMemoryPressure
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);
}
}
}