Wednesday, February 10, 2010

Out of Scope

Why is it that every time I opine aloud that Java would benefit from a destructor, something that gets called the moment an object goes out of scope, people start whining about memory leaks and saying memory management is hard? I didn't say ditch the garbage collector. Since you have brought up the subject, let me say again that memory management is not hard.

2 comments:

  1. That would be because they completely don't understand what destructors are for. I am constantly amazed at how little some "programmers" actually understand about their environment.

    Here's the thing: Destructors are for cleaning up precious resources that the client programmer might not be trusted to do properly. Yes memory falls into this category, but it is hardly the most precious resource in the machine.

    Has anyone ever seen a garbage-collector for database connections? Didn't think so. That's a job that could be beautifully excuted by a destructor.

    Ownership of a serial port? A network socket? A file? All of these are examples of precious resources that are left hanging around accidently all the time by Java programmers that have simply forgotten to call close() on the resource in question.

    ReplyDelete
  2. Java needs a destructor. Java needs other things too.

    C# needs a destructor that works. The C# destructor is mired in garbage collection entanglements that make it nearly worthless. Don't get me started with the 'Dispose' method. I hate the IDispose interface crap. This is a poor substitute for an actual destructor.

    ReplyDelete