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

4

240

Enter image description here

It is unclear to me how the compiler will automatically know to compile for 64-bit when it needs to. How does it know when it can confidently target 32-bit?

I am mainly curious about how the compiler knows which architecture to target when compiling. Does it analyze the code and make a decision based on what it finds?

Severance answered 22/8, 2012 at 5:13 Comment(2)
#7509465Cyclamen
Ah, thanks. Didn't see that before. I am still curious as to how the compiler automatically knows which architecture to target. Any ideas?Severance
T
246

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.

Tahsildar answered 22/8, 2012 at 5:36 Comment(12)
+1. Also, the "Prefer 32-bit" checkbox is only enabled for .NET 4.5+ executable projects.Labuan
Another advantage of anycpu32bitspreferred is that another .exe running in 64 bits can load that assembly.Vaillancourt
Personally I think it is horrible they set this by default with no Tools setting to turn it off. Even worse, you can't search for it since not in the csproj files unless turned off! Probably added because of the Office Automation incompatibilities with CPUAny on a x64 machine with most folks installing 32 bit Office.Onto
Wait, so there is no configuration that will lead to a 64-bit process?Trapes
@BrianDavidBerman there is, if you set false to 32-but preferred and set x64 or Any CPU on a 64-bit machine.Tahsildar
There are performance implications to running as 32 bit on a 64 bit operating systems. I've noticed double and long operations taking a lot longer for example.Conceivable
The difference between x86 and Any CPU 32 bit preferred is that in the latter case the largeaddressaware flag is set on the executable. This means that the 32 bit process running on a 64 bit OS can use 2GB of memory in x86 mode and 4GB of memory in Any CPU 32 bit preferred mode.Lambeth
As a followup to @Nic's statement, Here's a post with more information about how "prefer 32 bit" sets IMAGE_FILE_LARGE_ADDRESS_AWARE : "Why does 'Any CPU (prefer 32-bit)' allow me to allocate more memory than x86 under .NET 4.5?"Fortress
Changed the default to Prefer 32-bit is a HUGE regression.Elba
But why was/is this the default? Why not use the native 64-bit architecture on a 64-bit OS?Kowatch
@Kowatch it was set for default, as Microsoft was promoting the ARM32 based Windows Phone and Windows RT at that time. We all know what happened next.Tahsildar
@LexLi What does that have to do with this? Those are not 64-bit platforms, so whether "Prefer 32-bit" is set or not would make no difference on them.Kowatch
W
29

Here's a simple answer:

Application arch.

Note: AnyCPU-32bitPreferred is only available in .Net version 4.5 and higher.

Woad answered 23/11, 2019 at 18:37 Comment(5)
What's the difference between "runs as 32-bit" vs. runs as "WoW64". I thought WoW64 = "Windows (32-bit) on Windows64" and was needed for any 32-bit application to run.Backer
Is there a source to this? Seemingly everywhere else still tells me the default is anycpu32bitpreferred, which is a vast difference to people running on 64bit windows machines (that's a lot).Cassis
@RanSagy you can simply test it by creating a new project and checking Project -> Properties -> Build tab -> Platform target... but note that AnyCPU-32bitPreferred is only available in .Net version 4.5 and higher. That's why the default is AnyCPU.Woad
In some cases mine was greyed out; I was just hoping there's some documentation on what happens in .net 4.5+ or .net standard/core (or really, MSBuild 16)Cassis
There is no difference between running as a 32-bit application on 64-bit Windows and running as a WoW64 application on 64-bit Windows. WoW64 or "Windows on Windows 64bit" is how 32bit apps run on 64-bit Windows.Apostles
M
0

When I had 32 bit preferred checked, when run on our server, it was trying to use the 32 bit db driver instead of 64 bit that we had installed, so it wasn't connecting to the db when we ran it, so queries were failing because of failure to connect.

Mattheus answered 12/10, 2022 at 14:32 Comment(1)
This does not really answer the question.Bikaner
C
-1

The reason is: in case you don't want to use more memory with 64 bit applicatios. Which means, if your application is AnyCPU, you want to run as 32 bit.

To add more, the setting in Visual Studio targets the particular CLR:

Visual Studio installs the 32-bit version of the CLR on an x86 computer, and both the 32-bit version and the appropriate 64-bit version of the CLR on a 64-bit Windows computer. (Because Visual Studio is a 32-bit application, when it is installed on a 64-bit system, it runs under WOW64.)

Please refer to the article 64-bit Applications (MSDN).

Cocainize answered 22/8, 2012 at 5:23 Comment(7)
I'm not sure that's accurate. As, it's my understanding that .NET executables regardless of 32 or 64 are still limited around 2 GB per process.Noun
Any idea on how the compiler knows which architecture to target?Severance
Edited my answer. But not Sure if this is what you are looking for :)Cocainize
@Aaron, compiler essentially sets flag for runtime to decide if it is ok to load assembly (i.e. block x86-only assembly to be loaded in x64 process) and how to start the process (for new EXE) based on flags. I believe IL is the same for both flavors.Gallagher
@JPRichardson Yes you are right. But in .net 4.5 you have the option to increase the size. refer MSDNCocainize
@JPRichardson, neither 32 nor 64 bit .Net executable limited to 2GB per process - first of all per-process address space is OS level restriction (2/3+GB for 32bit process and much more for 64bit), second even 32bit version can use more than 2GB if "LargeAddressAware" flag is set on the executable. The only 2GB restrictions I know are about array/allocation sizes that are limited by Int32 range (about 2GB).Gallagher
There are performance implications to running as 32 bit on a 64 bit operating systems. I've noticed double and long operations taking a lot longer for example.Conceivable

© 2022 - 2024 — McMap. All rights reserved.