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

90,187

Solution 1

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.

Solution 2

Here's a simple answer:

Application arch.

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

Share:
90,187

Related videos on Youtube

Aaron
Author by

Aaron

Passionate software engineer with strong knowledge and experience in Linux, security, and databases.

Updated on December 26, 2020

Comments

  • Aaron
    Aaron over 3 years

    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?

    • Marc Gravell
      Marc Gravell over 11 years
    • Aaron
      Aaron over 11 years
      Ah, thanks. Didn't see that before. I am still curious as to how the compiler automatically knows which architecture to target. Any ideas?
  • JP Richardson
    JP Richardson over 11 years
    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.
  • Aaron
    Aaron over 11 years
    Any idea on how the compiler knows which architecture to target?
  • Peru
    Peru over 11 years
    Edited my answer. But not Sure if this is what you are looking for :)
  • Alexei Levenkov
    Alexei Levenkov over 11 years
    @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.
  • Peru
    Peru over 11 years
    @JPRichardson Yes you are right. But in .net 4.5 you have the option to increase the size. refer MSDN
  • Alexei Levenkov
    Alexei Levenkov over 11 years
    @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).
  • Lee Grissom
    Lee Grissom almost 11 years
    +1. Also, the "Prefer 32-bit" checkbox is only enabled for .NET 4.5+ executable projects.
  • Bruno Martinez
    Bruno Martinez almost 11 years
    Another advantage of anycpu32bitspreferred is that another .exe running in 64 bits can load that assembly.
  • Dave
    Dave over 10 years
    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.
  • Brian David Berman
    Brian David Berman over 7 years
    Wait, so there is no configuration that will lead to a 64-bit process?
  • Lex Li
    Lex Li over 7 years
    @BrianDavidBerman there is, if you set false to 32-but preferred and set x64 or Any CPU on a 64-bit machine.
  • jjxtra
    jjxtra over 6 years
    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.
  • jjxtra
    jjxtra over 6 years
    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.
  • Nic
    Nic over 6 years
    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.
  • jrh
    jrh almost 6 years
    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?"
  • Shiv
    Shiv over 4 years
    Changed the default to Prefer 32-bit is a HUGE regression.
  • Peter Cordes
    Peter Cordes over 4 years
    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.
  • Ran Sagy
    Ran Sagy over 4 years
    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).
  • Yousha Aleayoub
    Yousha Aleayoub over 4 years
    @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.
  • Ran Sagy
    Ran Sagy over 4 years
    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)