.net内存限制事件 [英] .net memory limit event

查看:59
本文介绍了.net内存限制事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何方法可以在运行时检测.net应用程序的内存使用情况并实现内部内存刷新/释放(即,通过配置将我的应用程序限制为使用500mb内存),显然释放将由您自己完成.

Is there any way to detect memory usage of a .net application while running and implement internal memory flushing/freeing (i.e. limit my application to use 500mb ram through configuration) obviously the freeing would be done by yourself.

推荐答案

您可以检查任何进程的内存使用情况:

如果达到一定大小,则可以处理不需要的对象并调用垃圾回收.

You can check the memory usage of any process:

If it reaches a certain size you can dispose of unwanted objects and call garbage collection.

System.Diagnostics.Process p = System.Diagnostics.Process.GetCurrentProcess();
long memory = p.PrivateMemorySize64;
if(memory > 500 * 1024 * 1024)
{
  //dispose of unwanted object
  GC.Collect();
}



尽管根据我的经验,GC.Collect()有时也不总是有效.进程内存可能会一直增长,直到框架决定需要释放一些空间为止.



Although from my experience GC.Collect() doesn''t always work when you ask it too. The process memory may keep growing until the framework decides it need to free up some space.


这篇关于.net内存限制事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆