I've always wondered why rugby players seem to get injured less than Football players. It doesn't make sense to me. Football players have pads, Rugby players do not. One is protected, the other is not. One gets injured, the other does not. QED. 

So why in the name of Chuck Norris do Football players have more frequent and more extreme injuries than Rugby players?

The answer is in the pads:

[UMKC physics professor Robbert] Riggs believes there would be fewer serious injuries if players didn’t wear pads or helmets.

"As a retired Marine officer," Riggs said, "I know that when you’re wearing body armor, sometimes you feel invincible and I think there’s some of that. They put on all those pads and they feel invincible." (emphasis mine)

Sound familiar? (warning, PDF)

The developer does not need to explicitly free memory assigned to an object, which goes a long way toward reducing the amount of time spent debugging memory leaks. (10) There can be no memory leaks in 100% managed code.

Not only is this statement downright false, it's also dangerous.  

Google currently has 103,000 hits for "Memory Leak C#", and they all contain a similar tale of woe, programmers telling other programmers how not to make the same mistake they did. Each example boils down to one of three things:

1. Not de-referencing an event handler:

MyType object2 = new MyType();

// ...
object1.SomeEvent += object2.myEventHandler;
// ...

// Should call this
// object1.SomeEvent -= object2.myEventHandler;

object2.Dispose();

My most recent encounter with a memory leak was much like this: There was a class that dealt with dynamically wiring up events for other objects (as if they couldn't do it themselves?) and although these references went out of scope, the event handlers were still hanging around, subscribing to references that were now gone. Anything that was being used as a reference couldn't be garbage-collected, resulting in hundreds of megabytes of memory being retained over the course of an application's life.

2. Creating global references to objects:

I don't care what Greenpeace tells you: Think locally, not globally. Objects that don't have external references are much easier to keep track of than those that do. 

3. Disposing of Forms

If you're a Winforms or Webforms developer, this one is for you. ASP.NET MVC guys get a pass because they wised up. If you're using something that isn't yours, Dispose of it. Period. Database Handles, Filehandles, Controls, Forms, Whatever. Dispose of it. 

Bad decisions have a half-life that rivals uranium. Don't make bad decisions: Know your code.


View Comments: Comments (3) |

Comments


March 28. 2010 10:34
Phileosophos
If you haven't learned the proper care, feeding, and deployment of the 'using' statement, G, then go look it up right now. I can honestly say I've had a mere  one or perhaps two resource leaks in the last 5+ years with the .NET platform because of my paranoia in using 'using'.

http://phileosophos.com/http://phileosophos.com/


March 29. 2010 03:02
George
Phil, I should have written about the using statement in my post. I use it just about everywhere I can. I'm surprised I don't see it in more code, however.

http://blog.yapb.net/http://blog.yapb.net/


April 5. 2010 16:32
Phileosophos
You don't see it in more code because somehow, and don't ask me how, it remains an "obscure" feature of the language. I totally get how certain things buried in C# minutia (e.g. the ?? operator) can get overlooked, but THE USING STATEMENT?!?! In a garbage-collected "paradise", that ought to be taught immediately.

http://phileosophos.com/http://phileosophos.com/

Add comment


(Will show your Gravatar icon)

biuquote
  • Comment
  • Preview
Loading



Recent Comments

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.