windows 如何创建(32 位).NET 应用程序以使用 3 GB RAM?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/464458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:51:54  来源:igfitidea点击:

How do I create a (32-bit) .NET application to use 3 GB RAM?

.netwindowsmemory

提问by Hosam Aly

I am creating a .NET application (C#) that needs to use a lot of RAM. I recently knew that on 32-bit versions of Windows XP I can only use 2 GB, unless I use the /3Gbswitch, and set the IMAGE_FILE_LARGE_ADDRESS_AWAREflag in the executable header. But since I'm developing a .NET application, I guess I cannot modify the executable directly, can I? So, what should I do to allow my application to utilize the 3 GB?

我正在创建一个需要使用大量 RAM 的 .NET 应用程序 (C#)。我最近知道在 32 位版本的 Windows XP 上我只能使用 2 GB,除非我使用/3Gb开关,并IMAGE_FILE_LARGE_ADDRESS_AWARE在可执行文件头中设置标志。但是因为我正在开发一个 .NET 应用程序,我想我不能直接修改可执行文件,是吗?那么,我应该怎么做才能让我的应用程序使用 3 GB?

回答by Marc Gravell

An .NET exe is still a standard PE file; so you could try using editbin /LARGEADDRESSAWAREto set the flag, but note that this won't work if you are using something like ClickOnce (since that maintains a cryptographic hash of the files).

.NET exe 仍然是标准的 PE 文件;因此您可以尝试使用editbin /LARGEADDRESSAWARE来设置标志,但请注意,如果您使用的是 ClickOnce 之类的东西(因为它维护文件的加密哈希),这将不起作用。

However, note that you'll still have the same .NET limits in terms of the maximum size of a single object/array. For huge amounts of memory, x64 is a better idea.

但是,请注意,就单个对象/数组的最大大小而言,您仍然具有相同的 .NET 限制。对于大量内存,x64 是一个更好的主意。

回答by Spence

/3GB switch is on the OS bootloader, not on your applications. (EDIT: It is also present in native C/C++ compilers, but not C# compiler) As far as your app is concerned, it will request memory and the OS will give it to your process. However you have access to 1 more gig (potentially, you don't always get 3gig depending on your hardware peripherals) before your program uses virtual memory.

/3GB 开关在操作系统引导加载程序上,而不是在您的应用程序上。(编辑:它也存在于本机 C/C++ 编译器中,但不存在于 C# 编译器中)就您的应用程序而言,它将请求内存,而操作系统会将其提供给您的进程。但是,在您的程序使用虚拟内存之前,您可以再访问 1 个演出(根据您的硬件外围设备,您可能并不总是获得 3 个演出)。

As Marc Gavell has pointed out to me, you may need to run the command "editbin /LARGEADDRESSAWARE my.exe" as a post build option on your exe to enable this. Found a reference to an MS person speaking about it here: MS Forums

正如 Marc Gavell 向我指出的那样,您可能需要在 exe 上运行命令“editbin /LARGEADDRESSAWARE my.exe”作为后期构建选项以启用此功能。在这里找到了一个 MS 人的参考资料:MS 论坛

Might I suggest you look at your program and see whether you could rearchitect it to use less memory. Perhaps you could deal with a dataset in smaller chunks instead of trying to load the whole thing at once into memory?

我是否建议您查看您的程序,看看是否可以重新构建它以使用更少的内存。也许您可以以较小的块处理数据集,而不是尝试将整个内容一次加载到内存中?

回答by ChrisW

You should also increase your process' maximum working set size: see the SetProcessWorkingSetSizeAPI.

您还应该增加进程的最大工作集大小:请参阅SetProcessWorkingSetSizeAPI。

回答by Vilx-

Well, I'm not sure about this, but this is what I think:

好吧,我不确定这一点,但这是我的想法:

A .NET executable can be compiled in two ways - platform specific and platform independant. By default they are platform independant, and the code is (as mentioned in other answers) JIT'ed to platform specific code when running the program.

.NET 可执行文件可以通过两种方式编译 - 特定于平台和独立于平台。默认情况下,它们与平台无关,并且代码(如其他答案中所述)在运行程序时 JIT 到特定于平台的代码。

Now, for example, if your executable is one of these platform-independant ones, and you run it on a 64-bit OS, it will be JIT'ed to 64-bit code, right? Thus it will be able to address way more that 3GB of RAM.

现在,例如,如果您的可执行文件是这些独立于平台的可执行文件之一,并且您在 64 位操作系统上运行它,它将被 JIT 转换为 64 位代码,对吗?因此,它将能够处理超过 3GB 的 RAM。

What I'm trying to say is - I don't think it matters at all what is written in the PE header. The actual amount of available RAM is determined by the .NET runtime, which in turn looks at the current platform and produces the best JIT'ed code it can.

我想说的是 - 我认为 PE 标头中写入的内容根本不重要。可用 RAM 的实际数量由 .NET 运行时决定,它反过来查看当前平台并生成它所能生成的最佳 JIT 代码。

I think you shouldn't worry about the /3GB switch as .NET will take care of it for you. Trust in the .NET! :)

我认为您不应该担心 /3GB 开关,因为 .NET 会为您处理它。相信 .NET!:)

回答by Jonathan C Dickinson

You could try using Remoting via Named Pipes and get more memory by physically having more processes.

您可以尝试通过命名管道使用远程处理,并通过物理上拥有更多进程来获得更多内存。

If you are doing any form of interop (and normal .Net sockets count here) you should create an object cache (e.g. with sockets a byte[] buffer) that allocates a large amount of these objects at application startup.

如果您正在执行任何形式的互操作(这里是正常的 .Net 套接字计数),您应该创建一个对象缓存(例如,使用套接字一个 byte[] 缓冲区)在应用程序启动时分配大量这些对象。

You should read this article.

你应该阅读这篇文章

回答by hakan

As far as I remember, /3GB switch can only be used for Windows Server (2000 or 2003) but not for Windows XP. You need to write /3GB at the end of the boot.ini file. This way, for the applications, OS enables more memory which it normally allocates to use for kernel processes. However, /3GB does not mean you can use 3GB of memory for your application, it just can use more memory but it doesn't necessarily have to be 3GB. For .net applications, again as far as I remember, with /3GB switch you can use up to 1.8GB of memory. By the way, you might also want to check /PAE switch.

据我所知,/3GB 开关只能用于 Windows Server(2000 或 2003),而不能用于 Windows XP。您需要在 boot.ini 文件的末尾写入 /3GB。这样,对于应用程序,操作系统启用更多内存,它通常分配给内核进程使用。但是,/3GB 并不意味着您可以为您的应用程序使用 3GB 的内存,它只是可以使用更多的内存,但不一定必须是 3GB。对于 .net 应用程序,就我记忆而言,使用 /3GB 开关最多可以使用 1.8GB 的​​内存。顺便说一下,您可能还想检查 /PAE 开关。