ASP.NET:大量内存泄漏..它在哪里?数据库?更新面板?没有一次性用品吗?
我一直在开发一个相当大的应用程序,几天前我将其上传到我的服务器。现在我发现它有几个内存泄漏 - 呃哦。 我的服务器在 1GB RAM 上运行 Windows Server 2008。当我有0人在线时,只使用了550-600mb。当一个人上线时,内存开始飙升,如果有 3-4 个人在线,则全部使用 1GB 内存。
该应用程序是在 ASP.NET 中使用 AJAX 编写的。它有许多每秒运行的更新面板和大量的 JavaScript。它始终使用 5-7 个会话。我使用 LINQ to SQL 作为数据库通信。
我在我的服务器上尝试了 perfmon.exe,发现:
- Gen 0 集合从 0% 变为 几分钟内 100%
- 第 1 代系列 5 分钟内从 0% 升至 50%
- 第 2 代非常接近 0% 次 堆
- 总字节数达到 100% 非常快
我还使用 Visual Studio 对我的程序进行了分析。我的总运行时间的 8% 是在 .ToList() 方法中完成的,这实际上是由 LINQ to SQL 引起的。
我的理论....
(1) Linq to SQL dataContext
这可能是一件疯狂的事情,但是:在我的数据访问层中我有很多方法:
- AddSomethingToDatabase();
- AddSomethingElseToDatabase();
- 从数据库中删除一些东西();
其中每一个都有以下初始化:
GameDataContext db = new GameDataContext();
这意味着上述语句几乎每秒或更长时间运行一次。
(2) 没有对象实现 IDisposable
我必须说实话:我从未使用过 IDisposable。据我所读,这可能是一个问题。
另外,如果这是泄漏,哪些类应该实现它?我没有任何 I/O 工作或其他工作,只有 DataContext。
(3) UpdatePanels 和 jQuery 的负载
我担心 updatepanels 的负载会带来性能问题,但我不知道如何检查它。
所以我的问题是:关于内存泄漏可能是什么的任何想法?关于如何查找内存泄漏有什么想法吗?关于如何解决它有什么想法吗?
我很想听听有上述情况经验的人的意见!
谢谢, 拉尔斯
I have been developing a quite large application, and I uploaded it to my server some days ago. Now I have found out it has several memory leaks - Uh oh.
My server is running Windows Server 2008 on 1GB ram. When I have 0 people online, only 550-600mb is used. When one people comes online the memory starts skyrocketing, and if 3-4 people are online all 1GB ram is used.
The application is made in ASP.NET with AJAX. It has many updatepanels which runs every second and quite a lot of javascript. It uses 5-7 sessions at all times. I use LINQ to SQL as database communication.
I tried perfmon.exe on my server, and I found:
- Gen 0 collections goes from 0% to
100% within minutes - Gen 1 collections
goes from 0% to 50% within 5 minutes - Gen 2 is very close to 0% at all
times - Total heap bytes goes up to
100% very fast
I also ran an analysis of my program with Visual Studio. 8% Of my total runtime is done in .ToList() methods, which properly is caused by LINQ to SQL.
My theories....
(1) Linq to SQL dataContext
This might be a crazy thing to do, but: In my data access layer I have a load of methods:
- AddSomethingToDatabase();
- AddSomethingElseToDatabase();
- DeleteSomethingFromDatabase();
Each of these has the following initialization:
GameDataContext db = new GameDataContext();
Which means the above statement runes nearly every second or more.
(2) No objects implement IDisposable
I have to be honest: I have never worked with IDisposable. As far as I have read, this might be a problem.
Also, if this is the leak, which classes should implement it? I do not have any I/O work or others, only the DataContext.
(3) Loads of UpdatePanels and jQuery
I have some fear loads of updatepanels can give problems with performance, but I do not know how to check it.
So my question is: Any ideas on what the memory leak could be? Any ideas on how to find the memory leak? And any ideas on how to solve it?
I would love to hear from someone who has experience with the situation above!
Thanks,
Lars
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我根本不确定这里是否存在问题。当您尚未确定自内存开启以来存在内存泄漏时,所有内存泄漏故障排除的建议似乎都只是非常糟糕的建议服务器太低,无法建立。
所以这是我的 2 美分 - 有些人可能不喜欢它,但只要它能给你指明正确的方向,我不介意反对票。
看来你有一个非常严格的内存要求。 Windows 2008 Server 的 1GB RAM 仅足以完成其操作系统相关的工作。 这远远低于推荐的 RAM 要求,如果我没记错的话,至少需要 2 GB RAM。仅运行 w3wp.exe 和 IIS 的开销约为 200-300 MB。第 2 代始终在 0% 左右这一事实是一切看起来都很好并且您的服务器可能内存不足的最佳证据。
我的建议是给你的服务器至少 2GB RAM(4GB 应该更好),然后监视内存使用情况,看看它是否在上升。如果是这样,请用您的发现提出另一个问题,我们应该能够提供帮助。
I am not sure at all that there is a problem here. All the suggestions for memory leak troubleshooting seem to be just really bad advice when you have not yet established that you have a memory leak since your memory on the server is so low that this cannot be established.
So here is my 2 cents - some might not like it but as long as it could point you at the right direction, I do not mind the downvotes.
It seems that you have a very stringent memory requirement. 1GB of RAM for a Windows 2008 Server just gives about enough RAM to do its OS related job. This is way way below recommended RAM requirements for it which if I am not wrong is minimum 2 GB RAM. Overhead of just running a w3wp.exe and IIS would be around 200-300 MB. The fact that generation 2 is always is around 0% is the best evidence that all looks good and your server is probably being starved of the memory.
My suggestion is to give your server at least 2GB of RAM (4GB should be better) and then monitor the memory usage and see if it is going up. If so, post another question with your findings and we should be able to help.
您绝对必须确保在使用完
IDisposable
对象后能够调用Dispose
。最简单的方法是使用 使用< /a>:如果在完成此操作后仍然存在问题,则需要进行分析,但首先要解决此问题,因为这是理所当然的。
如果您自己的对象封装了您希望保证确定性释放回操作系统的非托管资源,那么您自己的对象通常只需要实现 IDisposable,这样这些资源(文件句柄、套接字等)就不会被释放。等待 GC 的时间间隔是不可信赖的。
抱歉,我无法回答您的问题 3)。
You absolutely must ensure that
IDisposable
objects getDispose
called when you are done with them. The simplest way to do this is to use using:If you still have problems after doing this throughout, then profiling is needed, but fix this first since it's a no-brainer.
Your own objects usually only need to implement
IDisposable
if they encapsulate unmanaged resources for which you wish to guarantee deterministic release back to the OS, so that those resources - file handles, sockets, and so on - are not sitting around waiting for GC for an interval of time you cannot rely on.I don't have an answer for your question 3), sorry.
我建议您使用内存分析器。 Redgate的ANTS相当优秀;它可以为您提供给定时间内存中哪些对象的详细信息。
I'd recommend you use a memory profiler. Redgate's ANTS is pretty superior; it can give you a breakdown of which objects are in memory at a given time.
我不是这方面的专家。但如果您尝试 ANTS Memory Profiler 可能会帮助您找出问题所在。
I am not expert at this. But If you try ANTS Memory Profiler might help you figure out where the problem is.
Scitech 内存分析器 发现了我们的漏洞并提供了很好的建议。
Scitech memory profiler found our leaks and gives good advice.