单个 .NET 进程是否有内存限制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/200348/
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-03 10:19:38  来源:igfitidea点击:

Is there a memory limit for a single .NET process

.netmemory.net-3.5processlimit

提问by Mats

We are currently thinking of building a cache-system to hold data pulled out of an SQL database and make it available to a couple of other applications (website, webservice, etc). We imagine the cache to be running as a windows service and basically consist of a smart dictionary which holds the cache entries. My question is, is there a limit to the working set of the application (it will be running under windows server 2003)? Or is the amount of physical memory the limit?

我们目前正在考虑构建一个缓存系统来保存从 SQL 数据库中提取的数据,并使其可用于其他几个应用程序(网站、网络服务等)。我们想象缓存作为 Windows 服务运行,并且基本上由保存缓存条目的智能字典组成。我的问题是,应用程序的工作集是否有限制(它将在 Windows Server 2003 下运行)?还是物理内存量是限制?

回答by Chris S

32bit or 64bit? 32bit is 2gb (for a process), 64 bit is 1TB (enterprise edition 2003 server).

32位还是64位?32bit是2gb(一个进程),64bit是1TB(企业版2003服务器)

However, the maximum size of a CLR Object is 2gbeven on 64bit.

但是,即使在 64 位上,CLR 对象的最大大小也是 2gb

Update:the information above was correct in 2008. See Ohad's answerfor more recent information. Windows 2016 server can have a maximum of 24TB.

更新:以上信息在 2008 年是正确的。有关更多最新信息,请参阅Ohad 的回答。Windows 2016 服务器的最大容量为24TB

回答by Luke Machowski

I have recently been doing extensive profiling around memory limits in .NET on a 32bit process. We all get bombarded by the idea that we can allocate up to 2.4GB (2^31) in a .NET application but unfortunately this is not true :(. The application process has that much space to use and the operating system does a great job managing it for us, however, .NET itself seems to have its own overhead which accounts for approximately 600-800MB for typical real world applications that push the memory limit. This means that as soon as you allocate an array of integers that takes about 1.4GB, you should expect to see an OutOfMemoryException().

我最近在 32 位进程上围绕 .NET 中的内存限制进行了广泛的分析。我们都被 .NET 应用程序中最多可以分配 2.4GB (2^31) 的想法轰炸,但不幸的是这不是真的 :(。应用程序进程有那么多空间可以使用,操作系统做得很好为我们管理它的工作,然而,.NET 本身似乎有它自己的开销,对于推动内存限制的典型现实世界应用程序,它大约占 600-800MB。这意味着一旦你分配一个整数数组,大约需要1.4GB,您应该会看到 OutOfMemoryException()。

Obviously in 64bit, this limit occurs way later (let's chat in 5 years :)), but the general size of everything in memory also grows (I am finding it's ~1.7 to ~2 times) because of the increased word size.

显然,在 64 位中,此限制发生得更晚(让我们在 5 年后讨论 :)),但是由于字大小的增加,内存中所有内容的一般大小也会增加(我发现它是 ~1.7 到 ~2 倍)。

What I know for sure is that the Virtual Memory idea from the operating system definitely does NOT give you virtually endless allocation space within one process. It is only there so that the full 2.4GB is addressable to all the (many) applications running at one time.

我可以肯定的是,操作系统的虚拟内存理念绝对不会在一个进程中为您提供几乎无限的分配空间。只有在那里,整个 2.4GB 才能被一次运行的所有(许多)应用程序寻址。

回答by Ohad Schneider

The following table from MSDNis the most precise answer to your query. Note that the IMAGE_FILE_LARGE_ADDRESS_AWARE flag cannot be set directly from the managed compiler, though fortunately it can be set post build via the editbin utility. 4GT refers to the /3gb flag.

来自MSDN的下表是您查询的最准确答案。请注意,无法直接从托管编译器设置 IMAGE_FILE_LARGE_ADDRESS_AWARE 标志,但幸运的是,它可以通过 editbin 实用程序在构建后设置。4GT 指的是 /3gb 标志。

alt text

替代文字

回答by Sean

On 32bit Windows you can get a bit more memory by booting Windows with the /3gb flag and flagging your app as "large address aware"

在 32 位 Windows 上,您可以通过使用 /3gb 标志启动 Windows 并将您的应用程序标记为“大地址感知”来获得更多内存

回答by Sam Meldrum

Matthias,

马蒂亚斯,

Not actually an answer to the direct question, but another way of approaching this problem which will get around some of the big pitfalls, which can be a major headache with caching solutions. (Sorry I don't have any recommended reading on the matter.)

实际上不是直接问题的答案,而是解决这个问题的另一种方法,它将绕过一些大陷阱,这可能是缓存解决方案的主要问题。(对不起,我没有关于此事的任何推荐阅读。)

We implemented this in a previous project, and it did create other problems.

我们在之前的项目中实现了这一点,但它确实产生了其他问题。

For offline access, can you use sql express on the desktops to create a mirror of your database (or just the bit you need to cache)? Then all you need to do is switch which database your application is pointing to. You can even use it store diffs and replay these to the server - although this has other problems. You can alter the permissions on the local copy to make this one read-only if that's how it should be.

对于离线访问,您是否可以在桌面上使用 sql express 来创建数据库的镜像(或仅需要缓存的部分)?然后您需要做的就是切换您的应用程序指向哪个数据库。您甚至可以使用它存储差异并将它们重播到服务器 - 尽管这还有其他问题。如果应该是这样,您可以更改本地副本的权限以将其设为只读。

The dictionaries you are thinking of creating sound remarkably like Sql indexes. I would rely on sql to do the job for you if you can architect it that way. Why reinvent that wheel? If you do, you will have to think carefully about cache expiration and memory management - particularly if this is a windows service.

您正在考虑创建的字典听起来非常像 Sql 索引。如果您能以这种方式构建它,我将依靠 sql 为您完成这项工作。为什么要重新发明那个轮子?如果这样做,您将不得不仔细考虑缓存过期和内存管理 - 特别是如果这是 Windows 服务。

Good luck,

祝你好运,

Sam

山姆

回答by Roger Lipscombe

As with any other Windows program, you're limited by address space. That is: on 32-bit, you can have 2GB of address space. On x64, you can have 8TB.

与任何其他 Windows 程序一样,您受到地址空间的限制。也就是说:在 32 位上,您可以拥有 2GB 的地址空间。在 x64 上,您可以拥有 8TB。

If you don't have 8TB of physical memory, it will start to page.

如果您没有 8TB 的物理内存,它将开始分页。