简体   繁体   English

如何限制.net / mono进程的内存大小

[英]how can I limit the memory size for .net / mono process

imagine you have an application (single process) written in c# 想象你有一个用c#编写的应用程序(单个进程)

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). 默认情况下,应用程序正在分配巨大的虚拟内存,远远超过其需要(例如,驻留内存大约为10mb,而虚拟内存大约为几GB)。

In Java it's possible to use an option to limit this with java -mx128m 在Java中,可以使用选项来限制java -mx128m

How can I do the same for .net / mono application? 我怎么能为.net / mono应用程序做同样的事情? 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) 还有一个问题:是否可以强制/更改GC选项以始终保持尽可能低的内存使用率(尽快收集,而不是在系统内存不足时)

basically my goal is to create a .net application with minimal memory footprint 基本上我的目标是创建一个内存占用最小的.net应用程序

EDIT: in order to make it more clear I insert this scenario, given that nearly every .net application seems to have similar problem: 编辑:为了使它更清楚我插入这个场景,因为几乎每个.net应用程序似乎都有类似的问题:

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. 我运行monodevelop,它是单声道下的c#应用程序 - 它占用1800 MB的虚拟内存,而它只使用291 MB的驻留内存。 That means the application is using only less than 300mb of memory but it asked for 1800MB for unknown reason. 这意味着该应用程序仅使用少于300mb的内存,但由于未知原因它要求1800MB。 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. 对于大多数用户来说,这可能不是问题,但是当任务受到虚拟内存的限制时,在SGE等集群上运行的应用程序可能会出现问题。

Mono has several flags for setting an upper limit on memory like java. Mono有几个用于设置内存上限的标志,如java。 For example, when I run my cluster jobs I set the following environmental variable. 例如,当我运行我的集群作业时,我设置了以下环境变量。

setenv MONO_GC_PARAMS max-heap-size=8G setenv MONO_GC_PARAMS max-heap-size = 8G

you can see more options available in this question here: 您可以在此处查看此问题中的更多选项:

Mono GC max-heap-size isn't documented. 没有记录Mono GC max-heap-size。 Is it safe to use in production? 在生产中使用是否安全?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM