Die, You Gravy Sucking Pig Dog!

Wow, what a lame site.

When it comes to non-trivial applications, well written C/C++ is still an order of magnitude better (as in faster execution and easier to understand) than well written C#/Python/Java/Ruby/etc. Bad programmers (cough JWZ cough) who don’t understand how to use the tools they are provided will produce bad code no matter what the language. This post is an excellent example of why you should not take what you read on the internet by self-styled experts at face value. Hint to the clueless: even in plain 30 year old ANSI C, you can declare a double that will automatically clean itself up when you are finished with it. If you are as l33t as you think you are, you should be able to figure out how fairly quickly.

Personally, I rather like pure reference-counted garbage collection systems like Perl uses. Once the object’s reference count hits zero, it is immediately destroyed, no waiting around for the GC to do its thing while your program leaks handles or whatever. Granted, you have to be more careful when dealing with circular references, but in practice (at least for me) that doesn’t tend to come up as often.

Jeff,

Great post! I love the humor in this one, I have to say that I have once in a while taken it out back and shot it with SQL connections. Cause the mistrusting, insecure, inner dev-child in me cannot let GC have all the fun…I commonly refer to it as my Office Space Fax Machine Field Basher method, or in code overload terms myObject.OSFMFB();

Thanks again for the great post!

You’re not disposing your objects? That’s a really bad idea, Jeff, I’ve seen that approach in action on a high-volume ecommerce site. It’s not pretty.

But why take my word for it? If StackOverflow is taking this approach, I think you’ll see it soon enough (if you haven’t already).

Most of the Java people I’ve seen use calls to System.gc() usually didn’t have a faint clue what was the cause of their problems. Calling gc was, to them, a wave of the magical Java code-healing wand.

For more info on proper nulling out of objects, see: Effective Java 2nd Edition by Joshua Bloch, Item 6 : Eliminate obsolete object references

Nulling out object references should be the exception rather than the norm, but is sometimes necessary.

Cheers,
e

@Sad Coder:

void super_magic_self_cleaning_double()
{
double d = 0;
// do stuff.
}
:slight_smile:

Tongue-in-cheek, of course, but locally scoped variables created on the stack get cleaned up after you leave the scope they’re created in. :slight_smile:

The double closing of the connection is silly but setting objects to null after using them is generally good practice. I have seen quite a few memory leaks disappear after doing so.

All of which just goes to show that memory is only one kind of resource, and that accordingly GC is no magic fairie dust.
And topical, as I have just today been jumping through those stupid hoops that Java makes you go through to tidy up database connections, prepared statements and the like. Grrrr.

Pig dog? PIG DOG??

@Sad Coder

I’m hard pressed to find a reason to use a low level language to enhance the speed of my financial app to the point it would make an iota of difference to the end user. C easier to read and understand than C#. In many cases of code that I’ve reviewed, open source and in practice I find this hard to beleive.

At what point will it become acceptable to use the proper tool for the job you are doing without religious brow beating.

Jeff, I’m surprised to find out that you are confused about IDisposable.
I suggest you read the comment Aaron G on January 15, 2009 06:39 AM to learn the must-know basics.

@Aaron G: setting a reference to null makes the object eligible for garbage collection.

Oh bravo for citing Steve Martin. I loaded this page an hour and a half ago but didn’t read it. In the interim my subconscious churned up the Steve Martin bit and I wondered for a while if that was the source, and if I should comment on it. Then I wondered if Steve Martin was really the source (I was exercising, the mind wonders…)

Then I read the comments and you did the name-check for me! Well done!

I may have an unhealthy fascination w/ Martin’s old albums. I once sought out Darby Conley’s email (the guy who write the comic strip Get Fuzzy) to congratulate him on his beautiful inclusion of may I mambo dog-face through the banana patch in a strip. Never heard back oddly =)

sqlConnection.Close();
sqlConnection.Dispose();
sqlConnection = null;

All that code example shows is the dev didn’t RTFM. The MSDN is pretty clear on how to use the SqlConnection correctly.

If the SqlConnection goes out of scope, it won’t be closed. Therefore, you must explicitly close the connection by calling Close or Dispose. Close and Dispose are functionally equivalent. If the connection pooling value Pooling is set to true or yes, the underlying connection is returned back to the connection pool. On the other hand, if Pooling is set to false or no, the underlying connection to the server is actually closed.

And the code example is even better showing the class in a using statement. Really if a dev can’t be skillful enough to find out how to use a class properly they shouldn’t be writing code.

Aaron G is right, as is Ronald with his good advice on the using clause. If a class implements IDisposable, it is YOUR responsibility to dispose of it. Using() does this for you, as does a good IoC container (as Ryan Roberts points out).

The GC is all about MEMORY management. The IDisposable pattern is about RESOURCE management (in particular, unmanaged resources).

See
http://www.bluebytesoftware.com/blog/PermaLink.aspx?guid=88e62cdf-5919-4ac7-bc33-20c06ae539ae
and
http://msdn.microsoft.com/en-us/magazine/cc163392.aspx?ppud=4

And be sure to buy a copy of Framework Design Guidelines (Cwalina and Abrams). It discusses how to implement the Dispose Pattern correctly in detail.

I love your blog, Jeff, but you’re off on this one.

C:

b1 = (double )malloc(msizeof(double));

C:

double b1;

More developers need to take some time to read the following chapter: Improved Managed Code Performance

http://msdn.microsoft.com/en-us/library/ms998547.aspx

It would clear a lot of the misconceptions up – Dispose pattern, when to explicitly set something to null, etc.

Too much good and bad advice being passed around here. Problem is too many people can’t tell the difference.

+1 Ronald… beat me to it.

Pretty awesome, right? I’d wager the majority of programmers alive today have never once worried about malloc(). I call this progress…

I call it an excuse to write even-more-comupting-power-sucking-applications.
Lets make bigger and havier apps - people will buy more RAM and faster CPUs. Hey, the last time I checked the price of the computer memory it was not as its price in gold. Coder time spent is more precious than performance of the coded product.
Why bother learning malloc and pointers - there is a garbage collector to collect the garbage left behind the coder.

Release the resource as soon as I’m done is such an important
idiom that Python has built it into the language twice. First
was the try/finally block.

f = open(file)
try:

finally:
f.close()

Then starting in 2.5, we have the with statement.

with open(file) as f:

I would think that if you needed the resources returned you would want to be able to tell the GC code to go ahead and run and make sure you deal with the resource ASAP:

sqlConnection.Close;
GC.Collect;