Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net Why can't I get more than 11GB of allocated memory in a x64 process?

I thought that the maximum user space for a 64bit process was 8TB, but I did a little test and the maximum I could get is 10-11GB.

Note: I don't need that much memory in a process, I just want to understand why out of curiosity.

Here is my test program:

static void Main(string[] args)
{
    List<byte[]> list = new List<byte[]>();

    while (true)
    {
        Console.WriteLine("Press any key to allocate 1 more GB");
        Console.ReadKey(true);
        list.Add(new byte[1024 * 1024 * 1024]);

        Console.WriteLine("Memory size:");
        double memoryUsage = Process.GetCurrentProcess().PeakVirtualMemorySize64 / (double)(1024 * 1024 * 1024);
        Console.WriteLine(memoryUsage.ToString("0.00") + " GB");
        Console.WriteLine();
    }
}

EDIT:

Updated the test program to be more deterministic.

To accept an answer I would like to know how the real maximum allocated memory is calculated if 8TB is only theoretical.

like image 287
Jeff Cyr Avatar asked Jan 12 '10 20:01

Jeff Cyr


1 Answers

It's your machine.

I have an x64 with 8GB RAM and 12GB pagefile, and I ran your program and it topped out at 16.23GB.

EPILOG: Then my Win7 install gradually slid into a coma as critical processes were apparently memory starved.

EDIT: If you want to understand how Windows allocates (i.e. reserves and commits) memory, read Pushing the Limits of Windows: Physical Memory and Pushing the Limits of Windows: Virtual Memory.

Since .Net relies on Windows to manage the memory it uses to build the GC heap, the mechanics of how Windows does this are reflected in how memory is allocated in .Net on a low level.

like image 119
codekaizen Avatar answered Dec 09 '22 00:12

codekaizen