假设在 x64 Windows 机器上使用最新版本的 Visual Studio 和 C# 并分配大量数据。
果然,当使用默认构建设置进行编译时(如下图所示为 VS 2019 Preview 2.1),当进程达到 4 GB 时,您将耗尽用户虚拟地址空间。这是可以预料的,并讨论原因here https://stackoverflow.com/questions/54543938/memory-allocation-pattern-on-anycpu-platform-target.
分配本身可以通过创建数百个简单数组来完成,每个数组包含数百万个int
元素。
我想了解的是为什么Any CPU/Prefer 32-bit
被选为默认构建选项。我还注意到 VS 2015 也具有相同的默认设置,并且很可能自 VS 11 以来发布的每个版本都有相同的默认设置,如上所述here http://blogs.microsoft.co.il/sasha/2012/04/04/what-anycpu-really-means-as-of-net-45-and-visual-studio-11/.
通常问的问题是“什么是 AnyCPU...?”并且已经被多次回答(1 https://stackoverflow.com/questions/516730/what-does-the-visual-studio-any-cpu-target-mean 2 https://stackoverflow.com/questions/12066638/what-is-the-purpose-of-the-prefer-32-bit-setting-in-visual-studio-and-how-does 3 https://stackoverflow.com/questions/7508965/what-does-the-prefer-32-bit-compiler-flag-mean-for-visual-studio-c-vb 4 https://stackoverflow.com/questions/13149905/any-cpu-prefer-32-bit/13150845 5 https://stackoverflow.com/questions/5229768/c-sharp-compiling-for-32-64-bit-or-for-any-cpu),简单介绍一下定位的优点x86
/ x64
/ Any CPU + Prefer 32-bit
。但我还没有找到明确的答案Any CPU + Prefer 32-bit
选择为 VS 中的默认设置。
详细了解默认情况下反对构建 x64 的原因:
- The x64 process will use more memory: for the simple example described above (arrays of arrays of
int
) this shouldn't really be the case. Sure, the reference to the array itself is going to be double (8 bytes instead of 4), but that's about it. As per the "Windows Internals" book https://blogs.msdn.microsoft.com/microsoft_press/2017/05/09/new-book-windows-internals-seventh-edition-part-1/ (Memory Management chapter), the PFN entries in the page table structures themselves are 64 bits wide on both x86 and x64 architectures, it's only that there are 3 (for x86) vs 4 (for x64) of levels of tables for resolving the virtual addresses to physical ones. As for the data referenced, it's the same size (4 bytes per int
value). So allocating 20 arrays of 10 million int
will roughly translate to 800 MB used for both architectures on the managed heap. Actually, the overall committed size on the x64 version of the simple example just described was about the same as the x86 one when tested (comparison follows, x64 on top, x86 below; and ignoring the 4 GB chunk that's simply in a reserved state for the x64 version). Interestingly enough, while running the 32 bit process on x64, each thread within the process will end up with 2 stacks, a 32-bit wow64 one and a 64-bit one, thus resulting in a higher memory consumption from the perspective of the stack.
- 跨平台可移植性:答案here https://stackoverflow.com/questions/516730/what-does-the-visual-studio-any-cpu-target-mean/2124096#2124096(小心,这是之前
Prefer 32-bit
选项被发明)提供了一个链接女士推荐 https://learn.microsoft.com/en-us/windows/uwp/packaging/device-architecture#x86(文章截至 2017 年撰写本文时更新)。引用的文章还显示了兼容性矩阵,但这仅适用于 UWP。特别是关于 ARM 架构作为使用构建的输出的开箱即用目标AnyCPU/Prefer 32-bit
,这是由一个漂亮的表格支持的这个答案 https://stackoverflow.com/questions/516730/what-does-the-visual-studio-any-cpu-target-mean/41766077#41766077. 本文 http://blogs.microsoft.co.il/sasha/2012/04/04/what-anycpu-really-means-as-of-net-45-and-visual-studio-11/引用也颇多,展示了.NET 4.5带来的变化以及对ARM的影响。
- 与现有 32 位应用程序不兼容:有人评论here https://stackoverflow.com/questions/12066638/what-is-the-purpose-of-the-prefer-32-bit-setting-in-visual-studio-and-how-does/12066861#12066861Office 是一个问题,因为它通常安装为 32 位。不过没有太多进一步的信息。
- Loaded assemblies: A BadImageFormatException is thrown when trying to load an x86 assembly inside a x64 process or the other way around. However, within the comments to this comment https://stackoverflow.com/questions/12066638/what-is-the-purpose-of-the-prefer-32-bit-setting-in-visual-studio-and-how-does/12066861#12066861 it's stated that an assembly compiled with
Any CPU/Prefer 32-bit
can be loaded in a 64-bit process. I haven't been able to find an official article supporting this though later edit: It can be loaded just fine; I've detailed this and all possible tests for assembly loading
here https://mihai-albert.com/2019/03/10/net-assembly-cross-bitness-loading/
最后还是默认的Any CPU/Prefer 32-bit
设置很可能是一种权衡,并且以某种方式牺牲了大(> 4 GB)内存访问以换取其他被认为更重要的东西。
不过,Win10 上的 x64 进程用户模式虚拟地址空间的限制是 128 TB,并且 4 GB 物理 RAM 是当今入门级笔记本电脑的标配,因此可能会失去所有额外 RAM 的优势(对于Windows 版本有here https://learn.microsoft.com/en-us/windows/desktop/Memory/memory-limits-for-windows-releases).
None
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)