您的位置:首页 > 其它

使用WorkingSet减少.net程序内存占用 转

2008-08-29 09:08 686 查看
正如大家所知.Net对内存资源的占用是非常恐怖的,一个空的Form程序就得占用10M的内存。 再加上一个Windows Service那就轻松越过20M了。这不,最近在工作中就遇到了这个问题。需求方要求内存在10M以内。。。郁闷。。。查阅了相关资料后,终于找到解决方法。
我们知道,Windows是一个多任务的操作系统,而物理内存却是一个相对贫乏的资源,为避免某个进程(或是系统)耗尽这一资源,引入了工作集(WorkingSet)的概念。进程的工作集是物理 RAM 内存中当前对该进程可见的内存页的集合。这些内存页是常驻内存,可供应用程序使用,而不会触发页面错误。工作集包括共享数据和私有数据。共享数据包括那些包含应用程序执行的所有指令的页(包括 .dll 文件和 system.dll 文件中的页)。随着工作集大小的增加,内存需求也增加。进程具有最小和最大的工作集大小。每次创建进程资源时,系统都会保留等于该进程最小工作集大小的内存量。虚拟内存管理器会尝试在进程处于活动状态时至少保留最小的常驻内存量,但决不会保留超过最大大小的内存量。

在Framework中为我们提供了以下设置进程最大工作集合的属性:
Process.MaxWorkingSet 属性
获取或设置关联进程的允许的最大工作集大小。
命名空间: System.Diagnostics

程序集: System(在 System.dll 中)

看到这里,笔者迫不及待在代码去测试这个属性:

public partial class TestPerformance : Form

2 {

3{

7{

12{

17{

22{

28{

34 ReduceMemoryOnce();

35 }

36 }
这次声明了一个Thread.Timer 间隔5秒去设置MaxWorkingSet的值。内存一直维持在2M以内,如图:



至此问题得到解决。。。。

在一个国外网站上找到一篇文章,对该问题有类似的描述,如下

Windows Forms apps are pretty bloated in terms of memory usage. The main reason .Net apps have such a huge footprint is that the JIT compiler loads when the app starts and all that bootstrap code and a ton of the WinForms engine is loaded and compiled at startup and gets loaded into your process. Beside the fact that this takes up processor cycles it also consumes a lot of memory. The JIT is pretty good about which code gets compiled – for the most part it compiles only code that actually gets run, but the framework itself pulls in a lot of code as well and that gets compiled as well. Third party controls - ditto. The result of this is that on startup your app consumes a lot of resources that you will not need any longer once the app is up and running. Actually if you access another new part of the application that part will compile later, but for many apps the startup and main application form make up a huge chunk of what gets loaded and compiled as the data engine and business objects etc. usually are among the things that get called at startup. A large percentage generally gets compiled right at startup.

If you’ve ever run your app and looked at Task Manager you might have noticed that the app starts out with a significant amount of memory. A basic WinForm app with a couple of textboxes and a button typically will run around 8 megs. If you move the form around a bit closer to 10. You then can minimize the app and it generally reduces to some really low memory usage number which slowly creeps back up as you open the form back up. What’s happening is that the app internally is adjusting the Working Set for the application which aggressively reduces the memory in use by the app. The app will reclaim what it needs, but for that moment the memory usage goes down drastically and it will stay much lower than the original startup usage.

详细文章:

http://www.west-wind.com/Weblog/posts/240.aspx

笔者对这篇文章的翻译,给需要的朋友。翻的不好好请多多见谅

/article/5689535.html

疑问:

1. 大家都发现这个语句非常奇怪:P.MaxWorkingSet = P.MaxWorkingSet。那是因为在程序运行中,无法给MaxiWorkingSet设置一个确定的值,所以就有了这个写法。为什么这样就可以了呢?目前笔者正在分析P.MaxWorkingSet 的源代码。相信不久就有结果。也请知道原因的牛人能道破其中的玄机。

2. 这样做对性能有影响吗?

笔者水平有限,难免存在错误,希望大家指出和讨论。谢谢。

测试程序的源代码:http://files.cnblogs.com/netlife/PerformanceTest.rar
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: