Beruflich Dokumente
Kultur Dokumente
http://www.codeproject.com/KB/dotnet/BestPractices5.aspx
Not quite what you are looking for? You may want to try: Build a Blog Site in 15 Minutes with WebMatrix WindowsDevNews.com: A Visual Studio LightSwitch Application
8,405,672 members and growing! Email Password Sign in Join Remember me? Lost password?
highlights off
Home
Articles Help!
Discussions
Learning Zones
Features
Blog application
See Also
More like this More by this author
,+
In this article we are going to detect the .NET application memory leaks.
65
Sponsored Links
ANTS Profiler Pro ANTS Profiler Pro is a code and memory profiler for .NET, used to... www.red-gate.com GdPicture Forms Processing Plugin GdPicture Forms Processing Plugin is
Improve garbage collector performance using finalize/dispose a royalty free & thread-safe... pattern www.gdpicture.com
1 of 13
1/13/2012 9:45 AM
http://www.codeproject.com/KB/dotnet/BestPractices5.aspx
Introduction
Memory leaks in .NET application have always being programmers nightmare. Memory leaks are biggest problems when it comes to production servers. Productions servers normally need to run with least down time. Memory leaks grow slowly and after sometime they bring down the server by consuming huge chunks of memory. Maximum time people reboot the system, make it work temporarily and send a sorry note to the customer for the downtime. Please feel free to download my free 500 question and answer eBook which covers .NET , ASP.NET , SQL Server , WCF , WPF , WWF@ http://www.questpond.com .
See Also...
Visual Leak Detector - Enhanced Memory Leak Detection for Visual C++ Silverlight application memory leaks detector The ASP.NET Worker Process Part 1 Memory(-Leak) and Exception Trace (CRT and COM Leaks) Memory Leak Detection in .NET Memory Leak Detection
.NET Best Practice No: 1:- Detecting High Memory consuming functions in .NET code Memory Leakage in Internet Explorer - revisited Detecting Memory Leaks using the CrtDbg Library Easy Detection of Memory Leaks Best Practice No 4:- Improve bandwidth performance of ASP.NET sites using IIS compression COM+ and .NET - A practical approach - Part 1 WPF GIF Animation Windows Mobile, iPhone, Android Marketplace Comparison GDI+ and MFC memory leak detection
2 of 13
1/13/2012 9:45 AM
http://www.codeproject.com/KB/dotnet/BestPractices5.aspx
First lets try to understand how the task manager memory information is misleading. Task manager shows working set memory and not the actual memory used, ok so what does that mean. This memory is the allocated memory and not the used memory. Adding further some memory from the working set can be shared by other processes / application.
So the working set memory can big in amount than actual memory used.
3 of 13
1/13/2012 9:45 AM
http://www.codeproject.com/KB/dotnet/BestPractices5.aspx
In order to get right amount of memory consumed by the application we need to track the private bytes consumed by the application. Private bytes are those memory areas which are not shared by other application. In order to detect private bytes consumed by an application we need to use performance counters. Below are the steps we need to follow to track private bytes in an application using performance counters:Start you application which has memory leak and keep it running. Click start Goto run and type perfmon. Delete all the current performance counters by selecting the counter and deleting the same by hitting the delete button. Right click , select Add counters , select process from performance object. From the counter list select Private bytes. From the instance list select the application which you want to test memory leak for. If you application shows a steady increase in private bytes value that means we have a memory leak issue here. You can see in the below figure how private bytes value is increasing steadily thus confirming that application has memory leak.
The above graph shows a linear increase but in live implementation it can take hours to show the uptrend sign. In order to check memory leak you need to run the performance counter for hours or probably days together on production server to check if really there is a memory leak.
4 of 13
1/13/2012 9:45 AM
http://www.codeproject.com/KB/dotnet/BestPractices5.aspx
What is the type of memory leak? Total Memory = Managed memory + unmanaged memory
Before we try to understand what the type of leak is, lets try to understand how memory is allocated in .Net applications. .NET application have two types of memory managed memory and unmanaged memory. Managed memory is controlled by garbage collection while unmanaged memory is outside of garbage collectors boundary.
So the first thing we need to ensure what is the type of memory leak is it managed leak or unmanaged leak. In order to detect if its a managed leak or unmanaged leak we need to measure two performance counters. The first one is the private bytes counter for the application which we have already seen in the previous session. The second counter which we need to add is Bytes in all heaps. So select .NET CLR memory in the performance object, from the counter list select Bytes in all heaps and the select the application which has the memory leak.
5 of 13
1/13/2012 9:45 AM
http://www.codeproject.com/KB/dotnet/BestPractices5.aspx
Private bytes are the total memory consumed by the application. Bytes in all heaps are the memory consumed by the managed code. So the equation becomes something as shown in the below figure.
Un Managed memory + Bytes in all helps = private bytes, so if we want to find out unmanaged memory we can always subtract the bytes in all heaps from the private bytes. Now we will make two statements:If the private bytes increase and bytes in all heaps remain constant that means its an unmanaged memory leak. If the bytes in all heaps increase linearly that means its a managed memory leak. Below is a typical screenshot of unmanaged leak. You can see private bytes are increasing while bytes in heaps remain constant
6 of 13
1/13/2012 9:45 AM
http://www.codeproject.com/KB/dotnet/BestPractices5.aspx
Below is a typical screen shot of a managed leak. Bytes in all heaps are increasing.
7 of 13
1/13/2012 9:45 AM
http://www.codeproject.com/KB/dotnet/BestPractices5.aspx
Its very difficult to inject a managed leak as GC ensures that the memory is reclaimed. In order to keep things simple we simulate a managed memory leak by creating lot of brush objects and adding them to a list which is a class level variable. Its a simulation and not a managed leak. Once the application is closed this memory will be reclaimed.
Collapse | Copy Code
private void timerManaged_Tick(object sender, EventArgs e) { for (int i = 0; i < 10000; i++) { Brush obj = new SolidBrush(Color.Blue); objBrushes.Add(obj); } }
In case you are interested to know how leaks can happen in managed memory you can refer to weak handler for more information http://msdn.microsoft.com/en-us/library/aa970850.aspx . The next step is to download debugdiag tool from http://www.microsoft.com/DOWNLOADS/details.aspx?FamilyID=28bd5941c458-46f1-b24d-f60151d875a3&displaylang=en Start the debug diagnostic tool and select Memory and handle leak and click next.
8 of 13
1/13/2012 9:45 AM
http://www.codeproject.com/KB/dotnet/BestPractices5.aspx
Now let the application run and Debugdiag tool will run at the backend monitoring memory issues.
9 of 13
1/13/2012 9:45 AM
http://www.codeproject.com/KB/dotnet/BestPractices5.aspx
Once done click on start analysis and let the tool the analysis.
You should get a detail HTML report which shows how unmanaged memory was allocated. In our code we had allocated huge unmanaged memory using AllochGlobal which is shown in the report below. Type Description mscorlib.ni.dll is responsible for 3.59 MBytes worth of outstanding allocations. The following are the top 2 memory consuming functions: Warning System.Runtime.InteropServices.Marshal.AllocHGlobal(IntPtr): 3.59 MBytes worth of outstanding allocations.
10 of 13
1/13/2012 9:45 AM
http://www.codeproject.com/KB/dotnet/BestPractices5.aspx
ntdll.dll is responsible for 270.95 KBytes worth of outstanding allocations. The following are the top 2 memory consuming functions: Warning ntdll!RtlpDphNormalHeapAllocate+1d: 263.78 KBytes worth of outstanding allocations. ntdll!RtlCreateHeap+5fc: 6.00 KBytes worth of outstanding allocations.
Managed memory leak of brushes are shown using GdiPlus.dll in the below HTML report. Type Description GdiPlus.dll is responsible for 399.54 KBytes worth of outstanding allocations. The following are the top 2 memory consuming functions: GdiPlus!GpMalloc+16: 399.54 KBytes worth of outstanding allocations.
Warning
Source code
You can download the source code from the top of this article which can help you inject memory leak.
11 of 13
1/13/2012 9:45 AM
http://www.codeproject.com/KB/dotnet/BestPractices5.aspx
/details.aspx?FamilyID=28bd5941-c458-46f1-b24d-f60151d875a3& displaylang=en Download for the debugdiag tool. http://blogs.msdn.com/davidklinems/archive/2005/11 /16/493580.aspx David Kline explains the 3 common causes of memory leak in managed application.
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
Shivprasad koirala I am a Microsoft MVP for ASP/ASP.NET and currently a CEO of a small E-learning company in India. We are very much active in making training videos , writing books and corporate trainings. Do visit my site for .NET, C# , design pattern , WCF , Silverlight , LINQ , ASP.NET , ADO.NET , Sharepoint , UML , SQL Server training and Interview questions and answers
Article Top
Sign Up to vote
Poor
Excellent
Vote
12 of 13
1/13/2012 9:45 AM
http://www.codeproject.com/KB/dotnet/BestPractices5.aspx
You must Sign In to use this message board. (secure sign-in) FAQ Profile popups Noise level
Medium
Search Layout
Normal
Per page
25
Update Refresh my mind My vote of 5 I don't find any process in the "Instances" list in perfmon My vote of 5 Empty Windows Service still shows memory leaks. My vote of 5 Excellent article My vote of 5 My vote of 5 Useful Article Message Automatically Removed Re: Useless Indian Jerk Re: Useless Indian Jerk Re: Useless Indian Jerk Re: Useless Indian Jerk Re: Useless Indian Jerk Re: Useless Indian Jerk Re: Useless Indian Jerk Re: Useless Indian Jerk Re: Useless Indian Jerk [modified] My Vote of 5 Where is article 1 Re: Where is article 1 Re: Where is article 1 Re: Where is article 1 Last Visit: 19:00 31 Dec '99 General Rant News Admin Mast Avalons Dom P Member 7928159 Knight1219 ludwigs3rd Eric Xue (brokensnow) Michael Bakker mburnie santosh poojari Faddel mypaljohn KunalChowdhury Tom Clement Kunal_Chowdhury Ashish Kaila Shivprasad koirala Lloyd Atkinson Johnny J. Lloyd Atkinson Johnny J. ScruffyDuck gaurav_verma_mca Shivprasad koirala gaurav_verma_mca Shivprasad koirala First Prev Next 1:57 10 Jan '12 9:07 5 Jan '12 2:41 1 Jul '11 15:37 27 Sep '10 8:36 21 Sep '10 22:17 6 Sep '10 5:23 30 Aug '10 23:28 26 Aug '10 3:30 23 Aug '10 5:57 21 Aug '10 0:54 21 Aug '10 3:12 21 Aug '10 10:15 29 Jul '11 17:25 29 Jul '11 5:05 21 Aug '10 10:01 21 Aug '10 3:10 22 Aug '10 4:58 22 Aug '10 6:26 22 Aug '10 7:54 22 Aug '10 0:17 21 Aug '10 21:35 27 Nov '09 22:03 27 Nov '09 18:35 28 Nov '09 18:36 28 Nov '09 1 2 3 Next Bug Answer Joke
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
Permalink | Advertise | Privacy | Mobile Article Copyright 2009 by Shivprasad koirala Web03 | 2.5.120109.1 | Last Updated 21 Aug 2010 Everything else Copyright CodeProject, 1999-2012 Layout: Terms of Use fixed | fluid
13 of 13
1/13/2012 9:45 AM