我怎么可以限制对.NET /单进程的内存大小内存大小、进程、我怎么、NET

2023-09-03 17:41:45 作者:生肉

假设你有C#编写的应用程序(单个进程)

imagine you have an application (single process) written in c#

默认情况下,应用程序正在分配大量的虚拟内存,远远超过它需要(例如常驻内存约为10MB,而虚拟内存是约几个GB)。

By default the application is allocating huge virtual memory, far more than it needs (for example the resident memory is about 10mb, while virtual memory is about several GB).

在Java中可能使用的选项与的Java -mx128m限制本

In Java it's possible to use an option to limit this with java -mx128m

我如何可以做同样的.NET /单声道应用和奖金的问题:是否有可能执行/更改GC选项来始终保持为低内存占用为可能的(收集尽可能尽快,而不是当系统运行时内存不足)

How can I do the same for .net / mono application? And the bonus question: is it possible to enforce / change the GC options to always keep as low memory usage as possible (collect as soon as possible, not when system is running out of memory)

基本上都是我的目标是创建一个最小的内存占用.NET应用程序

basically my goal is to create a .net application with minimal memory footprint

编辑:为了更清楚我插入这种情况下,因为几乎所有的.NET应用程序似乎有类似的问题:

in order to make it more clear I insert this scenario, given that nearly every .net application seems to have similar problem:

我运行一个MonoDevelop的,这是下单C#应用程序 - 它吃的 1800 MB的虚拟内存,而它仅使用 291 MB 的驻留内存。这意味着应用程序仅使用内存大于300MB较少,但它要求1800MB由于未知原因。这是什么原因,我想探索,以及怎样的方式prevent它这样做。

I run a monodevelop, which is c# application under mono - it eats 1800 MB of virtual memory, while it only uses 291 MB of resident memory. That means the application is using only less than 300mb of memory but it asked for 1800MB for unknown reason. This reason is what I would like to discover as well as the way how to prevent it doing so.

这可能不是问题,对于大多数用户,但它可能是在集群像上海黄金交易所运行的应用程序的问题,当任务由虚拟内存的限制。

That probably isn't problem for most of users, but it may be problem for applications running on clusters like SGE when task is limited by virtual memory.

推荐答案

单有几个标志记忆像Java设定上限。例如,当我运行群集作业我设置以下的环境变量。

Mono has several flags for setting an upper limit on memory like java. For example, when I run my cluster jobs I set the following environmental variable.

SETENV MONO_GC_PARAMS最大堆大小= 8G

setenv MONO_GC_PARAMS max-heap-size=8G

您可以看到在这个问题在这里提供更多的选择:

you can see more options available in this question here:

Mono GC最大堆大小没有记载。是安全的在生产中使用?