Make sure "Prefer 32-bit" option is turned off for .NET 4.5 executables

comments

There is slippery place when you create new .NET 4.5 executable in Visual Studio 2012-2013 I learned today. .NET 4.5 executable project has new checkbox called "Prefer 32-bit", that .NET 4 and earlier doesn't have.

Meaning of "Prefer 32-bit" option

There is good explanation of meaning for this new flag. Excerpt:

  • If the process runs on a 32-bit Windows system, it runs as a 32-bit process. IL is compiled to x86 machine code.
  • If the process runs on a 64-bit Windows system, it runs as a 32-bit process. IL is compiled to x86 machine code.

So, by default, if you create .NET 4.5 project has one "AnyCPU" platform and "Prefer 32-bit" option is checked. I think that's was wrong decision by microsoft engineers to check this flag by default. That's reduces your application's memory usage limits to 2Gb of memory afterall.

Let's demonstrate how easy we can got System.OutOfMemoryException with this simple program:

using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace ConsoleApplication1
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var list = new List<byte[]>();

            while (true)
            {
                Console.Write("#{0} ProcessMemory: {1} bytes", list.Count, Process.GetCurrentProcess().PrivateMemorySize64);
                var bigObject = new byte[100000000];
                list.Add(bigObject);
                Console.ReadLine();
            }
        }
    }
}

If we run this program and press Enter multiple times, we'll got console output:

#0 ProcessMemory: 6873088 bytes
#1 ProcessMemory: 109809664 bytes
#2 ProcessMemory: 210927616 bytes
#3 ProcessMemory: 312930304 bytes
#4 ProcessMemory: 413818880 bytes
#5 ProcessMemory: 521457664 bytes
#6 ProcessMemory: 622018560 bytes
#7 ProcessMemory: 722583552 bytes
#8 ProcessMemory: 823144448 bytes
#9 ProcessMemory: 923705344 bytes
#10 ProcessMemory: 1024598016 bytes
#11 ProcessMemory: 1129689088 bytes
#12 ProcessMemory: 1230577664 bytes
#13 ProcessMemory: 1319194624 bytes
#14 ProcessMemory: 1419755520 bytes
#15 ProcessMemory: 1520316416 bytes
#16 ProcessMemory: 1620881408 bytes
#17 ProcessMemory: 1721442304 bytes
#18 ProcessMemory: 1822003200 bytes
Unhandled exception: OutOfMemoryException.

Sometimes 1.8Gb memory is not enough for long-running services.

To prepare your application to run as 64-bit process, in project settings select Build tab, select All Configurations and then uncheck "Prefer 32-bit" checkbox:

Right .NET 4.5 "Prefer 32-bit" option

Upgrading .NET 4.0 projects to .NET 4.5

I don't knew about this option before today because all my .NET 4.5 projects were upgraded from .NET 4.0. In this scenario "Prefer 32-bit" checkbox is unchecked. So, don't bother.

Conclusion

There is new "Prefer 32-bit" executable project option that mistakenly checked by default in new .NET 4.5 projects. It forces run your application as 32-bit process on 64-bit windows systems. Make sure that you have it turned off for your projects.

Comments