13

I've noticed in VS 2010 the default platform target for a C# project is x86 (it used to be any CPU), and was wondering why the change.

Does the compiler perform any optimizations based on fixing the platform to x86 vs x64 vs Any CPU?

Should I be forcing my apps to one platform or the other for performance reasons?

Superman
  • 3,686
  • 6
  • 34
  • 46
  • I don't know if it's optimizations, but if you reference a library that was compiled for x86 you'll need to also build a project for x86 or it'll fail to load the assembly at runtime. – Roman May 11 '11 at 04:14
  • 1
    The change was made because it's rarely beneficial to run as a 64-bit process, and often suboptimal (more memory used, can't load necessary libraries, etc.). – Gabe May 11 '11 at 04:19
  • 1
    Posted this in @Kragen's answer, but probably should have put it here - this [question](http://stackoverflow.com/questions/516730/visual-studio-any-cpu-target) is quite helpful in understanding the 'Any CPU' option. – Gian Paolo May 11 '11 at 04:56

2 Answers2

15

The previous version of Visual Studio used to default this to "Any CPU", which means that on a x86 machine you would always end up using x86, wheras on a x64 machine you would end up either running x64 or x86 depending on whether or not the process that the assembly is being loaded into was 32bit or 64bit.

The trouble is that when starting a new process a .Net exe built with the "Any CPU" option will end up as a 64 bit process rather than as a 32 bit process which can cause problems for 2 reasons:

  • Any native modules compiled for x86 (i.e. most of them) will not load into your process any more (also the errors you got as a result of this were sometimes cryptic if you didn't know to look out for this problem).
  • Also unless your application actually uses more than 4GB of address space (i.e. memory - note that the operating system no longer reserves the top 1-2GB of address space for 32 bit processes in a 64 bit OS), 64 bit code often performs worse than 32 bit code due to the increased pointer size.

As so few applications actually use enough address space (i.e. memory) to make the hassle worthwhile, the default was changed to x86 in order to avoid these problems.

Justin
  • 84,773
  • 49
  • 224
  • 367
  • 1
    I also found this [answer](http://stackoverflow.com/questions/516730/visual-studio-any-cpu-target) to be helpful in understand the 'Any CPU' option. – Gian Paolo May 11 '11 at 04:54
3

You'll notice that libraries still default to Any CPU and libaries should always be Any CPU so if there were optimizations, they would only apply to EXEs and that doesn't make sense. No, the issue is that Any CPU executables are usually more hassle than they are worth except in the hands of those that know what they are doing. And for those that know what they want, the defaults aren't a serious problem.

I'll add that I didn't agree with this policy initially but since the x86 debugging experience is superior, I've come to accept it as what it is: the default.

Rick Sladkey
  • 33,988
  • 6
  • 71
  • 95