How to debug a potential memory leak?

I programmed a Windows service to do the usual work.

I InstallUtil it to the windows service, and it wakes up and does something, and then thread.sleep(5min)

The code is simple, but I noticed a potential memory leak. I traced it using the DOS tasklist and drew a diagram: enter image description here

May I say that it is quite clear that there was a memory leak, although so few.

My code is similar to below, please help me find a 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. What should I do with 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[] 
+4
source share
4 answers

Here's how I can find a memory leak:

1) Download WinDbg if you do not already have it. It is a really powerful (albeit difficult to use because it is complex) debugger.

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

3) When it is attached, enter the following commands: (then enter)

// this will load managed extensions

.loadby sos clr

// this is a dump of 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 return to the service. Run the command !dumpheap -stat again to find out what is currently on the heap. If you have a memory leak (in managed code), you will see that one or more of your classes continue to be added to the heap over time. Now you know what is stored in memory, so you know where to look for the problem in your code. You may decide that it keeps links to objects leaked from WinDbg if you want, but this is a complicated process. If you decide to use WinDbg, then you will probably want to start reading the Tess blog and make labs .

+5
source

you will need to use distribution profiling to detect memory leaks, there is a good profiler for this, I can recommend AQTime ( see this video )

And read: How to find memory leaks using a distribution profiler

Maby this article may also be useful.

+1
source

To find a memory leak, you should look at performance counters for a long period of time. If you see the number of pens or total bytes in the entire heap growing without decreasing, you have a real memory leak. Then you can use, for example, profiling tools in visual studio to track leaks. There is also a tool from redgate that works quite well.

0
source

It's hard to say, but I suspect this line in your while loop inside DoWork:

 JsonIn jsonIn = new JsonIn { KeyString = "secretekeystring", }; 

Although jsonin only has an area inside the while block, I would jeopardize that the garbage collector takes time to remove unwanted instances.

0
source

Source: https://habr.com/ru/post/1386470/


All Articles