How to debug the potential memory leak?

后端 未结 4 1076
醉话见心
醉话见心 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.

提交回复
热议问题