Csharp/C Sharp by API/System/GC — различия между версиями

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

Версия 15:31, 26 мая 2010

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