如何调试潜在的内存泄漏? [英] How to debug the potential memory leak?

查看:147
本文介绍了如何调试潜在的内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我程序性的窗口服务做日常工作。



InstallUtil 它的窗口服务,它会醒来,做的东西,然后 Thread.sleep代码(5分钟)



中的代码很简单,但我'已经注意到了潜在的内存泄漏。我跟踪它使用 DOS任务列表并提请图表:



我可以说,这是很清楚的有内存泄漏,但这么少的。



我的代码是像下面,请大家帮我找到了潜在的泄漏。谢谢



 公共部分类自动创建报表:ServiceBase 
{
INT时间= Convert.ToInt32(的AppSettings [ 时代]的ToString());
私人Utilities.RequestHelper requestHelper =新RequestHelper();

公共自动报表()
{
的InitializeComponent();
}

保护覆盖无效的OnStart(字串[] args)
{
线程线程=新主题(新ParameterizedThreadStart(DoWork的));
thread.Start();
}

保护覆盖无效调用OnStop()
{
}

公共无效的DoWork(对象数据)
{
在(真)
{
串jsonOutStr = requestHelper.PostDataToUrl({\KeyString\:\somestring\},HTTP:// myurl。 ASHX);
Thread.sleep代码(时间);
}
}
}



编辑:使用后的WinDbg @罗素建议。我应该怎么做这些类?



  MT数总计TOTALSIZE类名
79330b24 1529 123096 System.String
793042f4 471 41952 System.Object的[]
79332b54 337 8088 System.Collections.ArrayList
79333594 211 70600 System.Byte []
79331ca4 199 3980 System.RuntimeType
7a5e9ea4 159 2544系统.Collections.Specialized.NameObjectCollectionBase + NameObjectEntry
79333274 143 30888 System.Collections.Hashtable +斗[]
79333178 142 7952 System.Collections.Hashtable
79331754 121 57208 System.Char []
7a5d8120 100 4000 System.Net.LazyAsyncResult
00d522e4 95 5320 System.Configuration.FactoryRecord
00d54d60 76 3952 System.Configuration.ConfigurationProperty
7a5df92c 74 2664 System.Net.CoreResponseData
7a5d8060 74 5032 System.Net.WebHeaderCollection
79332d70 73 876 System.Int32
79330c60 73 1460 System.Text.StringBuilder
79332e4c 72 2016年System.Collections.ArrayList + ArrayListEnumeratorSimple
7.93E +09 69 1380 Microsoft.Win32.SafeHandles.SafeTokenHandle
7a5e0d0c 53 1060 System.Net.HeaderInfo
7a5e4444 53 2120 + System.Net.TimerThread TimerNode
79330740 52 624 System.Object的
7a5df1d0 50 2000 System.Net.AuthenticationState
7a5e031c 50 5800 System.Net.ConnectStream
7aa46f78 49 588 System.Net.ConnectStreamContext
793180f4 48 960 System.IntPtr []


解决方案

这是我怎么会去寻找内存泄漏:



1)下载的WinDbg 如果你不 ŧ已经拥有了它。这是一个非常强大的(虽然很难用,因为它是复杂)调试器。



2)运行WinDbg中,按F6键并选择您的exe文件将其连接到你的进程。



3)当它连接键入以下命令:(依次输入)



//这将加载管理扩展



.loadby SOS CLR



//这将倾倒在堆



!dumpheap -stat

$ b所有的物体的细节
$ b

//这将再次启动该服务



先按g



现在等待几分钟,然后键入CTRL + BREAK突破回服务。运行!Dumpheap -stat 命令再次找出什么是在堆上了。如果你有内存泄漏(托管代码),然后你会看到一个或多个类别不断被添加到该堆随着时间的推移。你现在知道什么是被保存在内存中,所以你知道去哪里寻找在你的代码的问题。你能计算出,如果你喜欢的是什么阻碍从内部的WinDbg被泄露的对象的引用,但它是一个复杂的过程。如果您决定使用WinDbg,那么你可能希望通过阅读苔丝的博客,做的实验室


I programed the windows service to do a routine work.

I InstallUtil it to windows service and it'will wake up and do something and then thread.sleep(5min)

The code is simple, but I've noticed a potential memory leak. I traced it using DOS tasklist and drew a chart:

Can I say that it's pretty clear there was memory leak, although so little.

My code is like below, Please help me to find the potential leak. Thanks.

    public partial class AutoReport : ServiceBase
    {
        int Time = Convert.ToInt32(AppSettings["Time"].ToString());
        private Utilities.RequestHelper requestHelper = new RequestHelper();

        public AutoReport()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            Thread thread = new Thread(new ParameterizedThreadStart(DoWork));
            thread.Start();
        }

        protected override void OnStop()
        {
        }

        public void DoWork(object data)
        {
            while (true)
            {
                string jsonOutStr = requestHelper.PostDataToUrl("{\"KeyString\":\"somestring\"}", "http://myurl.ashx");
                Thread.Sleep(Time);
            }
        }
    }

Edit: After using WinDbg @Russell suggested. What should I do to these classes?

MT  Count   TotalSize   ClassName
79330b24    1529    123096  System.String
793042f4    471 41952   System.Object[]
79332b54    337 8088    System.Collections.ArrayList
79333594    211 70600   System.Byte[]
79331ca4    199 3980    System.RuntimeType
7a5e9ea4    159 2544    System.Collections.Specialized.NameObjectCollectionBase+NameObjectEntry
79333274    143 30888   System.Collections.Hashtable+bucket[]
79333178    142 7952    System.Collections.Hashtable
79331754    121 57208   System.Char[]
7a5d8120    100 4000    System.Net.LazyAsyncResult
00d522e4    95  5320    System.Configuration.FactoryRecord
00d54d60    76  3952    System.Configuration.ConfigurationProperty
7a5df92c    74  2664    System.Net.CoreResponseData
7a5d8060    74  5032    System.Net.WebHeaderCollection
79332d70    73  876 System.Int32
79330c60    73  1460    System.Text.StringBuilder
79332e4c    72  2016    System.Collections.ArrayList+ArrayListEnumeratorSimple
7.93E+09    69  1380    Microsoft.Win32.SafeHandles.SafeTokenHandle
7a5e0d0c    53  1060    System.Net.HeaderInfo
7a5e4444    53  2120    System.Net.TimerThread+TimerNode
79330740    52  624 System.Object
7a5df1d0    50  2000    System.Net.AuthenticationState
7a5e031c    50  5800    System.Net.ConnectStream
7aa46f78    49  588 System.Net.ConnectStreamContext
793180f4    48  960 System.IntPtr[]

解决方案

This is how I'd go about finding the memory leak:

1) Download WinDbg if you don't already have it. It's a really powerful (although difficult to use as it's complicated) debugger.

2) Run WinDbg and attach it to your process by pressing F6 and selecting your exe.

3) When it has attached type these commands: (followed by enter)

//this will load the managed extensions

.loadby sos clr

//this will dump the details of all your objects on the heap

!dumpheap -stat

//this will start the service again

g

Now wait a few minutes and type Ctrl+Break to break back into the service. Run the !Dumpheap -stat command again to find out what is on the heap now. If you have a memory leak (in managed code) then you will see one or more of your classes keep getting added to the heap over time. You now know what is being kept in memory so you know where to look for the problem in your code. You can work out what is holding references to the objects being leaked from within WinDbg if you like but it's a complicated process. If you decide to use WinDbg then you probably want to start by reading Tess's blog and doing the labs.

这篇关于如何调试潜在的内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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