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

查看:215
本文介绍了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中的一个对象的新实例,我下的IM pression,它并不需要被释放的.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 明智的做法是调用处置的方法,这可能不是你的问题。内存泄漏可能在.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:

  • 您在Session缓存每个用户的数据太多。
  • 您缓存太多的数据在应用程序缓存,或在一个静态变量如字典应用程序级别。
  • 您在会话或应用程序级存储Web控件(或用户控件)。
  • 您钩实例在静态类型或该不断被引用(因为它们被存储在一个高速缓存中)类型的事件。

我希望这给了你去哪里找一些想法。

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

更新:你应该看有关ASP.NET调试视频

更新2: 关于我的回答您的评论如下。 CLR将收集所有托管内存,因此你创建使用新对象将得到收集。在这个意义上说,这不要紧,无论一个对象实现的IDisposable 与否。有直接或间接的,您需要使用本地资源(如文件句柄,图形把手,数据库连接,使用原生-thus unmanaged-内存),但是很多次。在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垃圾收集然而,undeterministic性的。这意味着,你不知道什么时候运行,它是否运行。这意味着它可能需要很长的时间你的本机资源得到清理(因为终结会后,才收集被调用)之前。然而,对于许多资源,有必要尽可能快释放它们作为与他们完成的。举例来说,你会很快用完数据库连接,当你不关闭它们,或当你通过System.Drawing命名空间正与GDI + .NET中)。

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和垃圾收集不看这个界面。它是类型和它的用户之间的合同,允许其用户直接释放一个对象的基本资源。在正常的设计,既对象的终结和对象的处置方法调用会释放这些资源相同的私有或受保护的方法。当一个类型实现的IDisposable 明智的做法是调用它的处置方法,当你用它做或包裹的对象在使用语句,使原生资源的释放是确定的。

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 接口。呼叫处置他们会明确和直接释放这些本机资源。

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天全站免登陆