Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve Private bytes (Native memory) leak?

I'm developing a C# application which seems to have a leak. I've used memory profiler and found that my

private bytes keep increasing but Bytes in all Heaps do not, which means that probably it's a native memory leak

Now I'm stuck, how do I find memory leaks in native code ?

like image 365
Erez Avatar asked Oct 26 '11 08:10

Erez


2 Answers

First, if you have a dump of the leaking process, you can open it in WinDbg and issue the command : !address -summary

  • if RegionUsageHeap is large, then it should be a native memory leak
  • if RegionUsageIsVAD, then it should be a .NET memory leak.

If this is a native leak, then you have 2 options :

  • Use DebugDiag : when prompt, choose 'Native Memory leak and Handle leak', choose the process you want to diagnose, and start working with the application until you experiment the memory leak. When complete, generate a full dump of the application (right click on the leak rule and select Full user dump). You can then analyze the generated dump (you'll need to have the symbols properly configured for this to work efficiently) : on 'advanced analysis' tab, select 'Memory pressure analyzers', open the dump file and press 'Start analysis'. This produces and html report you can analyze. You can refer to this page for a detailed walkthrough.

  • Use Application Verifier / WinDbg. In application verifier, select your application (.exe). In tests page, be sure Basics/Heaps is selected. In the lower pane, be sure 'Traces' is set to true. Once the configuration saved, re-run the application and generate a full dump when the leak occurs. Don't forget to clean application flags after the dump is generated. Then you can open the dump from within WinDbg, and investigate the leak with the help of '!heap' command. In particular, '!heap -l' will give you a list of leaked blocks, '!heap -p -a ' will show the details of a block, including the call stack of allocation.

If this is a .NET leak, there are third party tools to troubleshoot it. Starting from version 1.2, DebugDiag is also enable to perform .NET memory leak analysis (never tried this however).

like image 76
Thierry Franzetti Avatar answered Oct 23 '22 02:10

Thierry Franzetti


Diagnosing native memory leaks in a managed application is (at least initially) very similar to diagnosing memory leaks in any other native application.

The way I normally approach these problems is to get the process to leak a large amount of memory, take a full process dump and then examine the dump to see what is using the most memory. For example if your process has a normal / initial private bytes of ~20MB but you can get your process to leak memory until it has ~200MB of private bytes, then there is a good chance that ~180MB of that memory is leaked - generally speaking whatever has the most memory allocated is where you should start looking.

Microsoft have a very useful tool called DebugDiag - initially developed for use in diagnosing memory leaks in IIS it is a very vesatile tool and very handy when dealing with memory issues. If you give it a crash dump it will perform some analysis and should (at the very least) tell you what module has allocated all of that memory, you can then start looking more specifically at how that module is used.

like image 40
Justin Avatar answered Oct 23 '22 04:10

Justin