Forum Xamarin.Android

Garbage collection is not collecting anything

borrelborrel USMember ✭✭

Hi

I have problems with memory not being deallocated by the GC for my Android project, similar to a previous discussion.

When I run the following code using

MONO_GC_PARAMS=soft-heap-limit=128m

(and combinations using nursery-size and max-heap-size) the GC is still not collecting anything

private void CreateArray()
{
    int arraysize = 80000000;

    System.Diagnostics.Debug.WriteLine("Total memory (before init): " + System.GC.GetTotalMemory(false) / 1024);

    int[] bigarray = new int[arraysize];

    System.Diagnostics.Debug.WriteLine("Total memory (after init): " + System.GC.GetTotalMemory(false) / 1024);

    for (int i = 0; i < arraysize; i++)
    {
        bigarray[i] = 50;
    }

    bigarray = null;

    System.GC.Collect(System.GC.MaxGeneration, System.GCCollectionMode.Forced);
    System.GC.WaitForPendingFinalizers();
    System.Diagnostics.Debug.WriteLine("Total memory (after GC): " + System.GC.GetTotalMemory(false) / 1024);
}

I have attached the example project.

Any suggestions?

Sign In or Register to comment.