// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

// Tests GC.TotalMemory

using System;

public class Test {

    public static int Main() {

        GC.Collect();
        GC.Collect();

        int[] array1 = new int[20000];
        int memold = (int) GC.GetTotalMemory(false);
        Console.WriteLine("Total Memory: " + memold);
        
        array1=null;
        GC.Collect();
        
        int[] array2 = new int[40000];
        int memnew = (int) GC.GetTotalMemory(false);
        Console.WriteLine("Total Memory: " + memnew);

        if(memnew >= memold) {
            Console.WriteLine("Test for GC.TotalMemory passed!");
            return 100;
        }
        else {
            Console.WriteLine("Test for GC.TotalMemory failed!");
            return 1;
        }
    }
}
 

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

// Tests GC.TotalMemory

using System;

public class Test {

    public static int Main() {

        GC.Collect();
        GC.Collect();

        int[] array1 = new int[20000];
        int memold = (int) GC.GetTotalMemory(false);
        Console.WriteLine("Total Memory: " + memold);
        
        array1=null;
        GC.Collect();
        
        int[] array2 = new int[40000];
        int memnew = (int) GC.GetTotalMemory(false);
        Console.WriteLine("Total Memory: " + memnew);
        GC.KeepAlive(array2);

        if(memnew >= memold) {
            Console.WriteLine("Test for GC.TotalMemory passed!");
            return 100;
        }
        else {
            Console.WriteLine("Test for GC.TotalMemory failed!");
            return 1;
        }
    }
}
 

Fix the GC total memory test.
Fix the GC total memory test.

This test was relying upon the result of GC.GetTotalMemory() returning a
greater number after allocating a large array. However, the array was
not kept live past the call to GC.GetTotalMemory, which resulted in this
test failing under GCStress=0xC (in which a GC is performed after each
instruction). This change adds the requisite call to GC.KeepAlive to
keep the allocated array live enough to be observed by
GC.GetTotalMemory.
