人生是一场不能存盘的RPG,我只能尽量多搞几个Screenshot

September 8, 2006

防止IIS out of memory

Filed under: ASP.NET

有关System.OutOfMemoryException,google一下,会找到很多究其本质,System.OutOfMemoryException出现有两种可能:
1.在我们试图新建一个对象时,而CLR又找不到任何可用内存,GC也找不到可释放的对象时被抛出,应用程序此时可捕获该异常.
2.CLR需要内存时,而却系统却不能提供,也会抛出该异常,此时,应用程序不能捕获该异常.

思路:给CLR更多的内存.减少应用程序所占的内存,及时释放无用的对象.对于ASP.NET程序,
CLR host在 asp.net的工作进程中,问题就转化为如何让asp.net的工作进程有更多的用户
地址空间.同时要设法利用IIS的一些功能来回收工作进程占用的内存.

————————————————————————————–
1. Enabling 4GT RAM Tuning
————————————————————————————–
此配置只在Windows 2000 Advanced Server,Data Center及Windows Server 2003以上才支持.
该模型可提供 3GB 的用户地址空间,另 1GB 保留给内核,通过在 boot.ini 中添加 /3GB 选项来启用该模型。

注意:Windows Server 2003 上支持的最大内存为 4 GB。但是,Windows Server 2003 Enterprise Edition
支持 32 GB 的物理 RAM。使用物理地址扩展 (PAE) 功能,Windows Server 2003 Datacenter Edition 可支持
64 GB 的物理内存。
对于下列系统,可以在 Boot.ini 文件中使用 3 GB 开关:
Microsoft Windows Server 2003 Standard Edition, but only in non-production environments.
Microsoft Windows Server 2003 Enterprise Edition 或
Microsoft Windows Server 2003 Datacenter Edition。

在WindowsXP和Windows Server 2003中提供一个新的启动参数/USERVA,必须和/3GB参数联合使用,
比如: 3GB /USERVA=2500 的意思就是配置2.5G的内存地址空间预留给用户内存空间,1.5G的留给核心
内存空间.


What Is 4GT?
http://technet2.microsoft.com/WindowsServer/en/library/cab49770-0239-4a8b-90c1-612e70b729c81033.mspx

How 4GT Works
http://technet2.microsoft.com/WindowsServer/en/library/edc9f27d-76fb-4139-9555-20acc684c3af1033.mspx

You may receive the “System.OutOfMemoryException” error message when you view ASP.NET pages on a server that has 3 gigabytes of RAM
http://support.microsoft.com/default.aspx?scid=kb;en-us;820108

如何配置 SQL Server 以便使用 2 GB 以上的物理内存
http://support.microsoft.com/default.aspx?scid=kb;zh-cn;274750

结论: 此选项可以使CLR有更多的可分配内存,我们使用的是windows 2003标准版,不推荐用这个参数.

————————————————————————————–
2. 设置.NET machine.config file的<processModel>或配置IIS,及时释放工作进程.
————————————————————————————–
<processModel>中有的一些配置:

memoryLimit(default=60),指定了ASP.NET进程(IIS5中为aspnet_wp,IIS6中为w3wp)
能够使用所有物理内存的百分比。当超过这个限额时,IIS回收工作进程,并创建一个新
的进程去负责应付Http请求。
*注意,如果将session存放在进程中,session会丢失.

当我们有很大内存时,memoryLimit这个值是需要进行适当的调整的。比如一台4G内存的服务器,
那么4G*60%=2.4G。但是,对于Win32操作系统,一个进程所能占用的所有内存空间只有2G。
当ASP.NET进程占用的内存开始达到2G时,由于它并没有达到2.4G的”回收阈值”,所以IIS不会
启动recycle进程操作,但是由于Win32的限制,实际上已经不能给这个进程分配更多的内存了,
于是,OutOfMemoryException就很可能会被抛出了。为了避免这样的情况,我们就必须将”memoryLimit”
适当调小,以让IIS更早的进行进程回收。

微软推荐的ASP.NET进程占用内存是不超过60%物理内存,当使用2GB地址空间时最好使计算出的实
际值不超过800M。

!注意缺省情况下IIS6并不设置内存的使用限制.


Running ASP.NET 1.1 with IIS 6.0
http://www.asp.net/faq/AspNetAndIIS6.aspx#2(非常重要!!)

ASP.NET Performance Monitoring, and When to Alert Administrators
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/monitor_perf.asp

Timeout(default=Infinite),持续运行了timeout指定的时间后,重启 ASP.NET服务

IdleTimeout(default=Infinite),在 idleTimeout 指定的时间内没人的访问,重启 ASP.NET服务

PingTimeout(default =5s),ISAPI extension 会每隔一段时间(PingFrequency, default=30)ping一次
工作进程,如果5秒内无相应,则重启工作进程.

PingFrequency(default =30s)

RequestLimit(default=Infinite),在处理了n个请求后重启工作进程.

这些配置在IIS6中不再由machine.config控制,而是由IIS控制.

如果使用iis5及以下版本需要安装MS提供的IIS5Recycle service
安装说明见http://support.microsoft.com/?id=322350

3. 编程
System.Drawing注意调用dispose
慎用new byte[],最好定义一个比较小的byte数组做为缓存,然后循环使用。

参照
OutOfMemoryException问题的处理
http://www.cnblogs.com/chainet/archive/2005/01/25/97000.html

ASP.NET中的OutOfMemoryException
http://blog.joycode.com/kaneboy/archive/2005/05/07/50409.aspx

Tuning .NET Application Performance
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag/html/scalenetchapt17.asp

IIS 6.0 Tuning for Performance
http://www.eggheadcafe.com/articles/20050613.asp

FIX: SetMinThreads and GetMinThreads API Added to Common Language Runtime ThreadPool Class
http://support.microsoft.com/default.aspx?scid=kb;en-us;810259

Comments »

The URI to TrackBack this entry is: http://recordsome.blogsome.com/2006/09/08/p167/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.






















Get free blog up and running in minutes with Blogsome
Theme designed by Hadley Wickham