[Mono-list] bigger (vs .net) memory usage (GC bug or design?)

Paweł Sikora pawel.sikora at agmk.net
Sun Apr 8 09:33:01 UTC 2012


Hi,

i have a small code example which allocates 4 big objects (4*1GB),
releases 2 objects and allocates 2 objects again. in such case i'd like
to stay around ~4GB memory usage. in fact mono touches the 5GB limit
while .net (win7/x64) works more realistic. moreover, adding --stats
option changes mono memory usage from 4G->3G->5G to 4G->2G->4G.

i've tested this on mono-2.10.9 with kernel 3.3.1 on machine with 12GB ram.

BR,
Paweł.

$ mono-sgen --optimize=all ./Program.exe

mem after step  |  mono-sgen (virt/rss)   |   win7/x64 (working set)
----------------+-------------------------+-------------------------
allocate 4*1GB  |       4317M/4109M       |            4131M
free 2*1GB      |       <no change>       |      <no change>
gc #1           |       3293M/3085M       |            2096M
allocate 2*1GB  |       5341M/5133M       |            4148M
array=null      |       <no change>       |      <no change>
gc #2           |       <no change>       |              44M
----------------+-------------------------+-------------------------

$ cat Program.cs

using System;
using System.Collections.Generic;

namespace test
{
    class Program
    {
        static byte[] alloc1G()
        {
            byte[] array = new byte[1024 * 1024 * 1024];
            for (int i = 0; i < array.Length; i++)
                array[i]=0xff;
            return array;
        }

        static void Main(string[] args)
        {
            Console.WriteLine("allocate 4*1GB...");
            Console.ReadKey();
            byte[][] arr = new byte[ 4 ][];
            for (int i = 0; i < arr.Length; i++)
                arr[i] = alloc1G();

            Console.WriteLine("free 2*1GB...");
            Console.ReadKey();
            for (int i = 0; i < arr.Length; i++)
                if (i % 2 == 0)
                    arr[i] = null;

            Console.WriteLine("gc #1");
            Console.ReadKey();
            GC.Collect();

            Console.WriteLine("allocate 2*1GB...");
            Console.ReadKey();
            for (int i = 0; i < arr.Length; i++)
                if (i % 2 == 0)
                    arr[i] = alloc1G();

            Console.WriteLine("array=null");
            Console.ReadKey();
            arr = null;

            Console.WriteLine("gc #2");
            Console.ReadKey();
            GC.Collect();

            Console.WriteLine("exit");
            Console.ReadKey();
        }
    }
}



More information about the Mono-list mailing list