如何知道它是否是内存泄漏与否,如果在任务管理器内存使用不断增加 [英] How to know if it is memory leak or not if Mem Usage in Task Manager keep increasing

查看:643
本文介绍了如何知道它是否是内存泄漏与否,如果在任务管理器内存使用不断增加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个小服务器类,基本上是一个包装的TcpListener线程池和线程派生。

I wrote a small Server class which basically is a TcpListener wrapper and ThreadPool thread spawner.

该线程运行服务器:: ProcessMessage的(),它做了一些工作,发送消息来来回回,然后退出在它的结束。

The threads run Server::ProcessMessage() which does some work sending messages to and fro and then quits at the end of it.

但就退出函数之前,我还呼吁TcpClient.GetStream()。关闭(),然后TcpClient的。关()。我不使用任何互斥或ManualResetEvent的WaitHandles

But just before exiting the function, I also call TcpClient.GetStream().Close() and then TcpClient.Close(). I don't use any Mutex or ManualResetEvent WaitHandles.

测试的客户端和服务器,一切正常,除了在任务管理器中它显示了内存使用保持在每个服务器上增加了对: :ProcessMessage的()。即使在所有客户端的应用程序已经被断开和闭合,内存使用仍然存在,而不是下降。

Tested the Client and Server, everything works except in task manager it shows the Mem Usage keep on increasing on every Server::ProcessMessage(). Even after all Client apps have been disconnected and closed, the Mem Usage is still there, not decreased.

Server正在运行作为Windows服务。

Server is running as a windows service.

我如何知道它是否是内存泄漏的标志或只是垃圾收集没有做的工作(没有)?

How do I know if it is a sign of memory leak or just garbage collector not doing its job (yet)?

感谢。

编辑:
我想我找到我的内存泄漏的原因。我在服务器有Console.WriteLine():: ProcessMessage的()。它仍然将是有益的,如果有检查内存问题更简单的方法。

I think I found the cause of my "memory leak". I had Console.WriteLine() in my Server::ProcessMessage(). Still it would be helpful if there was an easier way to check for memory problems.

推荐答案

尝试使用WinDbg的使用的 SoS的扩展程序来检查的不需要的对象是在堆仍引用。检查的此链接也。

Try using WinDbg with SoS extension to check if unneeded objects are still referenced in the heap. Check this link also.

您通常使用<$ C $启动C>!DumpHeap型(部分型或-刚刚命名空间)来获得某种类型的对象列表(我通常只写相关层的命名空间来获取列表或物体哪些我相信可能是仍在内存)。

You usually start by using !DumpHeap -type (some-type-or-just-namespace) to get a list of objects of a certain type (I usually just write the relevant layer's namespace to get a list or objects which I believe might be still in memory).

如果你有一定对象的多个实例,你需要选择对应的MT为您的地址对象,然后调用!gcroot(您的地址)来看看有什么让你的对象活着。

If you have more than one instance of a certain object, you will need to pick one address which corresponds to the MT for your object, and then call !gcroot (your-address) to see what's keeping your object alive.

在最的情况下,由于它们的方法中的一种被注册为某些其他对象的事件的事件处理程序的对象保持活力。一个好的做法是始终拆离父类的事件处理程序,一旦你的对象是不再需要。

In most cases, objects are kept alive because one of their methods is registered as an event handler for some other object's event. A good practice is to always detach your event handlers from parent classes once your object is no longer needed.

这篇关于如何知道它是否是内存泄漏与否,如果在任务管理器内存使用不断增加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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