ASP.NET 网站内存使用量相当高 [英] ASP.NET Website Memory Usage quite high

查看:23
本文介绍了ASP.NET 网站内存使用量相当高的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ASP.NET 网站,它将在大约 3-4 天内使用大约 2GB 的物理内存,这对我来说听起来很糟糕.目前,我已将 IIS 配置为在达到 500mb 时重新启动应用程序池进程.我想尝试找出问题所在.

I have an ASP.NET website that will hit about 2gb physical memory used within about 3-4 days, which to me sounds really bad. At the moment, I have configured IIS to restart the app pool process when it hits 500mb. I would like to try and track down the problem.

在 .NET 中创建对象的新实例时,我的印象是不需要释放它,因为 .NET 垃圾收集器会为我完成此操作.

When creating a new instance of an object in .NET, I was under the impression that it doesn't need to be freed as the .NET garbage collector will do this for me.

是这种情况还是这可能是我遇到问题的原因之一?

Is that the case or could this be one of the reasons I am having issues?

推荐答案

.NET 将非常有效地为您管理垃圾收集.虽然在实现 IDisposable 的类型上调用 Dispose 方法是明智的,但这可能不是你的问题..NET 中可能发生内存泄漏的原因有很多.以下是一些:

.NET will manage garbage collection for you very efficiently. While on types that implement IDisposable it is wise to call the Dispose method, this probably isn't your problem. Memory leaks can happen in .NET for a lot of reasons. Here are a few:

  • 您在会话中为每个用户缓存了过多数据.
  • 您在应用程序缓存或静态变量(如字典)中的应用程序级别缓存了过多数据.
  • 您在会话或应用程序级别存储网络控件(或用户控件).
  • 您将实例挂接到静态类型上的事件或一直被引用的类型(因为它们存储在缓存中).

我希望这能给你一些关于去哪里寻找的想法.

I hope this gives you some ideas about where to look.

更新:您应该观看关于 ASP.NET 调试的此视频.

UPDATE: You should watch this video about ASP.NET debugging.

更新2:关于您对我的回答的评论如下.CLR 将收集所有托管内存,因此您使用 new 创建的所有对象都将被收集.从这个意义上说,对象是否实现 IDisposable 并不重要.然而,很多时候您需要直接或间接地使用本机资源(例如文件句柄、图形句柄、数据库连接、使用本机 - 因此是非托管 - 内存).CLR 不知道如何释放这些资源.为此,.NET 具有终结器的概念.终结器是类的开发人员可以实现的虚拟方法.执行此操作时,CLR 将在该类型的实例未引用之后和收集之前调用此方法.终结器通常包含释放这些资源的逻辑.换句话说,当一个类型需要原生资源时,它通常会有一个终结器方法,允许该类型释放这些资源.

UPDATE 2: About your comment on my answer the following. The CLR will collect all managed memory, therefore all object you create using new will get collected. In this sense, it doesn't matter whether an object implements IDisposable or not. There are however many times that you need to use native resources (such as file handles, graphic handles, database connections, use of native -thus unmanaged- memory) directly or indirectly. The CLR does not know how to release these resources. For this .NET has the notion of finalizers. A finalizer is a virtual method that a the developer of a class can implement. When you do this, the CLR will call this method after an instance of that type gets unreferenced and before it gets collected. Finalizers typically contain logic that release these resources. In other words, when a type needs native resources, it will usually have a finalizer method that allow the type to release those resources.

关于CLR呢,故事到此结束.CLR 没有具体处理实现IDisposable 接口的对象.然而,.NET 垃圾收集器本质上是不确定的.这意味着您不知道它何时运行以及是否运行.这意味着可能需要很长时间才能清理本机资源(因为终结器只会在收集后被调用).但是,对于许多资源,有必要在使用完后立即释放它们.例如,当您不关闭数据库连接或通过 System.Drawing 命名空间在 .NET 中使用 GDI+ 时,您往往会很快耗尽数据库连接.

What about the CLR is concerned, the story ends here. The CLR has no specific handling of objects that implement the IDisposable interface. The .NET garbage collector however, is undeterministic in nature. This means that you don't know when it runs and if it runs. This means that it can take a very long time before your native resources get cleaned up (because a finalizer will only get called after a collect). For many resources however, it is necessary to release them as soon as your done with them. For instance, you tend to run out of database connections quickly when you don't close them or when you’re working with GDI+ in .NET through the System.Drawing namespace).

为此引入了IDisposable 接口.同样,CLR 和垃圾收集器不会查看此接口.它是类型与其用户之间的契约,允许其用户直接释放对象的底层资源.在正常设计中,对象的终结器和对象的 Dispose 方法将调用相同的私有或受保护方法来释放这些资源.当类型实现 IDisposable 时,明智的做法是在完成后调用它的 Dispose 方法或将对象包装在 using 语句中以允许确定性地释放原生资源.

For this reason the IDisposable interface was introduced. Again, the CLR and the garbage collector don't look at this interface. It is a contract between the type and its users, allowing its users to directly release the underlying resources of an object. In a normal design both the object's finalizer and the object's Dispose method will call the same private or protected method that will release those resources. When a type implements IDisposable it is wise to call it's Dispose method when you’re done with it or wrap the object in a using statement to allow the release of native resources to be deterministic.

所以回到你的问题.所有托管对象都将由 GC 收集,但本机资源不会.因此,类型可能会实现终结器方法,而这些对象通常也会实现 IDisposable 接口.对它们调用 Dispose 将显式直接释放这些原生资源.

So to come back to your question. All managed objects will be collected by the GC, but native resources won't. Therefore types might implement a finalizer method and those objects will also typically implement the IDisposable interface. Calling Dispose on them will explicitly and directly release those native resources.

我希望这是有道理的.

这篇关于ASP.NET 网站内存使用量相当高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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