freachable.net
Next generation's garbage
Monday, October 19, 2009
GC.Heresy
I'm actually going to encourage you to consider using GC methods. Well, now hang on cowboys... this is for pretty limited circumstances.
You see, the GC class can do more than compact memory. It can also run finalizers on unreferenced objects. If you are writing a class that included a finalizer (which you would only need if your class is the lowest-level class to deal with an unmanaged resource or are managing a pool of scarce resources) then you might consider using GC. Even though you might (read should) implement IDisposable, if a user of your library fails to Dispose your object, that unmanaged resource will leak until, at some non-deterministic (read random) point, the garbage collector runs and that object's finalizer might (read random) be queued and run. If this happens often enough, your attempt to create additional objects with the unmanaged resource could fail because you have exhausted the available supply.
Rather than fail, you could attempt:
GC.Collect(0);
GC.WaitForPendingFinalizers();
and try again. If it works on the retry, you might want to log that for the developer's information. One might argue that this is just encouraging and forgiving sloppiness, but I look at it as a step towards robustness and can actually help the developer diagnose a real problem whereas not doing this would just lead to more random run-time only-on-heavy-load failures.
If you wanted to be really awesome, once you detect that you could recover, flip a bit on to start recording a stacktrace on each create, then in your finalizer, put that stacktrace in a application-wide queue that then gets logged by the next create. Now you've told the dev exactly where to go find the spot where they got your object but didn't dispose it.
Another place is when you are calling something that has an unmanaged resource behind it along with other code that isn't behaving. For example, you are writing a web part in SharePoint that is calling new SPSite and OpenWeb... perhaps you are being a good SPRequest citizen, but another web part on the site that you didn't write isn't being so nice.
You could make auto-recovery wrappers for the stuff you use. Your newSPSite method would attempt to return new SPSite but if that should fail, Collect + WFPF and try again. Ugh, what a pain. But sometimes it beats trying to explain to someone why it is another webpart's fault that your webpart explodes.
Monday, October 19, 2009 9:40:14 PM (US Mountain Standard Time, UTC-07:00)
Comments [0]
-
.NET Internals
Comments are closed.
Twitter
Navigation
Home
dasBlog
Scott Hanselman
Categories
.NET Internals
Bing Maps
Certification
dasBlog
From the labs
Hardware
HTML
iphone
LINQ
merb
Office 12
ruby
Security
SharePoint
Virtualization
Visual Studio
Windows 7
Windows Live
Archive
<
March 2010
>
Sun
Mon
Tue
Wed
Thu
Fri
Sat
28
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
Blogroll
Andrew Connell
BCL Team
Beth Massi - VB
Brad Abrams
IE blog
Mike Stall's .NET Debugging
Panopticon Central
Scott Hanselman
SharePoint Designer Team
SharePoint Team
The Old New Thing
VB Team
Windows PowerShell
All Content © 2010, Hafthor Stefansson -
Disclaimer:
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way. -
Sign In