What Is the Purpose of the "Prefer 32-Bit" Setting in Visual Studio and How Does It Actually Work

What is the purpose of the Prefer 32-bit setting in Visual Studio and how does it actually work?

Microsoft has a blog entry What AnyCPU Really Means As Of .NET 4.5 and Visual Studio 11:

In .NET 4.5 and Visual Studio 11 the cheese has been moved. The
default for most .NET projects is again AnyCPU, but there is more than
one meaning to AnyCPU now. There is an additional sub-type of AnyCPU,
“Any CPU 32-bit preferred”, which is the new default (overall, there
are now five options for the /platform C# compiler switch: x86,
Itanium, x64, anycpu, and anycpu32bitpreferred). When using the "Prefer 32-Bit"
flavor of AnyCPU, the semantics are as follows:

  • 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.
  • If the process runs on an ARM Windows system, it runs as a 32-bit process. IL is compiled to ARM machine code.

The difference, then, between “Any CPU 32-bit preferred” and “x86” is
only this: a .NET application compiled to x86 will fail to run on an
ARM Windows system, but an “Any CPU 32-bit preferred” application will
run successfully.


What does the Prefer 32-bit compiler flag mean for Visual Studio (C#, VB)?

It likely indicates the app is AnyCpu but when 32 bit is available it shouold run as such. This makes sense - 64 bit apps use more memory, and sometimes you just dont need the memory space ;)

How to disable 'prefer 32-bit' on VS2022?

If you want to run your .NET 6 app as a 64-bit processes on a 64-bit operating system and as a 32-bit process on a 32-bit machine, the only thing you need to do is to set the target platform to Any CPU.

When running the app you should then be able to confirm that the Environment.Is64BitProcess property returns true on a 64-bit system.

Why does 'Any CPU (prefer 32-bit)' allow me to allocate more memory than x86 under .NET 4.5?

It turns out that, in Visual Studio 2015, building as 'AnyCPU (prefer 32-bit)' sets the IMAGE_FILE_LARGE_ADDRESS_AWARE bit on the executable (equivalent to running editbin /LARGEADDRESSAWARE on it), whereas it does not for an x86 build. This can be confirmed with dumpbin /HEADERS and looking for the line "Application can handle large (>2GB) addresses".

This is not the case for Visual Studio 2013. The change is apparently undocumented.

In theory, this should give the CLR an additional 2GB to play with. I don't know why the allocatable memory only goes up by about 300MB.

Why is the checkbox 'Prefer 32-bit' disabled in Visual Studio 2012?

Assuming you've got an executable project, then if you change your target platform to .NET 4.5, it should become enabled.

It's a .NET 4.5-only thing, and it's only enabled for executables.



Related Topics



Leave a reply



Submit