Пример #1
0
    public static void Main()
    {
        AllocateMemory();

        // Get addresses of static Age fields.
        IntPtr freePtr1 = FreeClass.AddressOfFreeAge();

        AllocateMemory();

        IntPtr fixedPtr1 = FixedClass.AddressOfFixedAge();

        AllocateMemory();

        // Garbage collection.
        GC.Collect();
        GC.WaitForPendingFinalizers();

        // Get addresses of static Age fields after garbage collection.
        IntPtr freePtr2  = FreeClass.AddressOfFreeAge();
        IntPtr fixedPtr2 = FixedClass.AddressOfFixedAge();

        // Display addresses before and after garbage collection
        Console.WriteLine("Normal static: {0} -> {1}", freePtr1, freePtr2);
        Console.WriteLine("Pinned static:  {0} -> {1}", fixedPtr1, fixedPtr2);
    }
    public static int Main()
    {
        // Get addresses of static Age fields.
        IntPtr freePtr1 = FreeClass.AddressOfFreeAge();

        IntPtr fixedPtr1 = FixedClass.AddressOfFixedAge();

        // Garbage collection.
        GC.Collect(3, GCCollectionMode.Forced, true, true);
        GC.WaitForPendingFinalizers();

        // Get addresses of static Age fields after garbage collection.
        IntPtr freePtr2  = FreeClass.AddressOfFreeAge();
        IntPtr fixedPtr2 = FixedClass.AddressOfFixedAge();

        if (freePtr1 != freePtr2 && fixedPtr1 == fixedPtr2)
        {
            return(100);
        }

        return(-1);
    }