ASP.NET Website Memory Usage quite high

2020-02-08 15:40发布

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?

7条回答
Juvenile、少年°
2楼-- · 2020-02-08 16:34

There could be lots of reasons for your high memory usage, but Garbage Collection in .NET is a very precise thing. That is, it does a lot for you, but sometimes not as much as you'd expect.

Specifically, it can only clean up objects to which there are no active references, so if you're done with a class, but something still has a reference to it, you'll want to remove that reference so the GC can recover that memory for you. Additionally, if you have any inactive open connections (say, to a DB or something) don't forget to close and dispose them. Typically, we wrap such objects in using blocks like this:

using(SqlConnection conn = new SqlConnection(myConnString))
{ ...rest of code here }

This will automatically close and dispose of the connection. This is implemented as a try...finally block, so the connection will be closed even if an exception is thrown in the using block.

Other than that, the answer is "profile, profile, profile" until you find your leak/bottleneck/whatever.

查看更多
登录 后发表回答