Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

.net - ASP.NET Website Memory Usage quite high

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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

.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:

  • You cache too much data per user in the Session.
  • You cache too much data on an application level in the application cache or in a static variable such as a dictionary.
  • You store web controls (or user controls) in the session or application level.
  • You hook instances to events on static types or to types that keep being referenced (because they are stored in a cache).

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

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

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.

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).

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.

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.

I hope this makes sense.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...