How to debug the potential memory leak?

后端 未结 4 1074
醉话见心
醉话见心 2021-01-06 03:02

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.sl

相关标签:
4条回答
  • 2021-01-06 03:46

    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.

    0 讨论(0)
  • 2021-01-06 03:56

    you will need to use an Allocation Profiler to Detect Memory Leaks, there are some good profiler for that, i can recommend the AQTime (see this video)

    And read: How to Locate Memory Leaks Using the Allocation Profiler

    Maby this article can be helpfull too

    0 讨论(0)
  • 2021-01-06 04:01

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

    0 讨论(0)
  • 2021-01-06 04:01

    Difficult to say, but I suspect this line in your while loop within DoWork:

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

    Although jsonin only has scope within the while block, I would hazard that the Garbage Collector is taking its time to remove unwanted instances.

    0 讨论(0)
提交回复
热议问题