问题描述
假设你有一个用 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/mono 应用程序做同样的事情? 还有一个额外的问题:是否可以强制/更改 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,它是 mono 下的 c# 应用程序 - 它占用 1800 MB 的虚拟内存,而它只使用 291 MB 的常驻内存.这意味着该应用程序仅使用了不到 300MB 的内存,但它出于未知原因要求使用 1800MB.这个原因是我想发现的以及如何防止它这样做的方法.
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.
这对大多数用户来说可能不是问题,但当任务受虚拟内存限制时,对于在 SGE 等集群上运行的应用程序可能会出现问题.
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.
推荐答案
Mono 有几个标志用于设置内存上限,如 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 max-heap-size=8G
setenv MONO_GC_PARAMS max-heap-size=8G
您可以在此处查看此问题的更多可用选项:
you can see more options available in this question here:
单声道GC 最大堆大小没有记录.在生产中使用是否安全?
这篇关于如何限制 .net/mono 进程的内存大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!