Session and cookie management in.Net Justin Brunelle CS795 6/18/2009.
11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE...
-
Upload
damon-lewis -
Category
Documents
-
view
216 -
download
0
Transcript of 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE...
![Page 1: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/1.jpg)
11
How to detect and avoid memory and resources leaks in .NET applications
Fabrice MARGUERIEIndependent .NET expertMicrosoft MVPmetaSapiens / Tuneo
![Page 2: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/2.jpg)
22
Who am I?
Independent .NET expert
Microsoft MVP since 2004
Book author: LINQ in Action
Entrepreneur metaSapiens Tuneo Proagora.com SharpToolbox.com JavaToolbox.com
metaSapiens
![Page 3: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/3.jpg)
33
Agenda
Basics
Detecting and fixing
Prevention is better than cure
![Page 4: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/4.jpg)
44
Agenda
Basics
Detecting and fixing
Prevention is better than cure
![Page 5: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/5.jpg)
55
Memory in .NET
Memory of a processstack, heap
The great GC aka the Garbage Collector
What keeps things aliveStatic referencesGCHandlesStack references (one stack per thread)The finalization queue
![Page 6: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/6.jpg)
66
Memory leaks
DefinitionIn computer science, a memory leak is a particular type of unintentional memory consumption by a computer program where the program fails to release memory that it no longer needs.
The most common causes of this condition are:carelessnessbugslack of knowledge
![Page 7: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/7.jpg)
77
Memory leaks
Languages that provide automatic memory management, like Java, C#, VB.NET or LISP, are not immune to memory leaks .
The garbage collector recovers only memory that has become unreachable. It does not free memory that is still reachable.In .NET, this means that objects reachable by at least one reference won't be released by the garbage collector.
![Page 8: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/8.jpg)
88
Memory leaks
A drop of water is not a big issue. But drop by drop, a leak can become a major problem.
In computing, even a little leak can bring down the system if it occurs many times.
![Page 9: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/9.jpg)
99
Agenda
Basics
Detecting and fixing
Prevention is better than cure
![Page 10: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/10.jpg)
1010
Steps in leak eradication
1. Detect a leak
2. Find the leaking resource
3. Decide where and when the resource should be released in the source code
![Page 11: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/11.jpg)
1111
Demothe wonderful PhotoLight application
![Page 12: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/12.jpg)
1212
Photo…Light ?
![Page 13: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/13.jpg)
1313
Tools for action
Three complementary approaches
High level view, general audit : dotTrace
More in depth: .NET Memory Profiler
For explorers and ninjas: WinDbg
![Page 14: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/14.jpg)
1414
dotTrace
![Page 15: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/15.jpg)
1515
dotTrace
![Page 16: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/16.jpg)
1616
dotTrace
![Page 17: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/17.jpg)
1717
Identified issue
SystemEvents.InstalledFontsChanged
MainForm
DetailsFormOptionsForm
PhotoLight.exe
Static event
![Page 18: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/18.jpg)
1818
.NET Memory Profiler
![Page 19: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/19.jpg)
1919
.NET Memory Profiler
![Page 20: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/20.jpg)
2020
Identified issue
PhotoLight.exe
MainForm
DetailsFormOptionsForm
OpacityChanged
Lapsed listener (missing unsubscription)
![Page 21: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/21.jpg)
2121
WinDbg
Native/managed and « graphical » debugger
Free
Lightweight installation
Can attach to a process or explore a memory dump
Ideal to dive into the internals of a process
![Page 22: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/22.jpg)
2222
WinDbg extensions
Sos.dll for inspecting managed memory
!DumpHeap for listing objects from the heap
!DumpObj (do)
!GCRoot finds the root object for a given object
![Page 23: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/23.jpg)
2323
WinDbg
![Page 24: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/24.jpg)
2424
Process Explorer
![Page 25: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/25.jpg)
2525
Identified issue
Dynamically generated code
MainForm
DetailsFormOptionsForm
PhotoLight.exe
SettingsManager
XmlSerializerassemblies
XmlSerializerassemblies
![Page 26: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/26.jpg)
2626
Agenda
Basics
Detecting and fixing
Prevention is better than cure
![Page 27: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/27.jpg)
2727
Common leak causes
Static references
![Page 28: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/28.jpg)
2828
Common leak causes
Static events
Lapsed event listeners
![Page 29: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/29.jpg)
2929
Common leak causes
Dispose method not invoked
Incomplete Dispose method
![Page 30: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/30.jpg)
3030
You’ll never be bored…
On-the-fly code generation
Technology-specific issues(Silverlight, WPF, composite applications, etc.)
![Page 31: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/31.jpg)
3131
Prevention is better than cure
Control objects ownershipStatic analysisAbove all a good knowledge of your code
Each += (or AddHandler) is a potential enemy!
use -= !using and DisposeWeakEventsEventBroker
Or simply restart the app…
![Page 32: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/32.jpg)
3232
Prevention is better than cure
We are not immune to oversightCustom tooling – Here a built-in monitoring screen
![Page 33: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/33.jpg)
3333
Tools
Tools dedicated to profiling in .NETdotTrace, .NET Memory Profiler, ANTS Profiler, YourKit Profiler, PurifyPlus, Aqtime, CLR Profiler…http://sharptoolbox.com/categories/profilers-debuggers
WinDbg, SOS.dll, linqdbghttp://www.microsoft.com/whdc/devtools/debugging/http://code.google.com/p/linqdbg/
VMMaphttp://sysinternals.com
Bear, GDI Usagehttp://thesz.diecru.eu/content/bear.phphttp://msdn.microsoft.com/en-us/magazine/cc301756.aspx
![Page 34: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/34.jpg)
3535
Bear and GDIUsage
http://marketingscience.co/2010/12/facebook-and-adobe-sitecatalyst-google-analytics-implementation/ http://marketingscience.co/2010/12/facebook-and-adobe-sitecatalyst-google-analytics-implementation/
![Page 35: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/35.jpg)
3636
Resources
Article How to detect and avoid memory and resources leaks in .NET applications
http://msdn.microsoft.com/en-us/library/ee658248.aspx
Book Advanced .NET DebuggingMario Hewardt, also author of Advanced Windows Debugging
Book Debugging Microsoft .NET 2.0 ApplicationsJohn Robbins
![Page 36: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/36.jpg)
3737
Resources
Tess Ferrandez’ blog, ASP.NET Escalation Engineer at Microsofthttp://blogs.msdn.com/tess/archive/tags/Memory+issues/default.aspx
Detecting event handlers leaks with WinDbghttp://is.gd/79Qlk (David Anson’s blog, MS)
More links at the end of my article
![Page 37: 11 How to detect and avoid memory and resources leaks in.NET applications Fabrice MARGUERIE Independent.NET expert Microsoft MVP metaSapiens / Tuneo.](https://reader031.fdocuments.us/reader031/viewer/2022032307/56649cc55503460f9498e3b4/html5/thumbnails/37.jpg)
3838
Contacts
Architecture and .NET Expertise
http://weblogs.asp.net/fmargueriehttp://metaSapiens.com