|
|
|
Back to newsletter 116 contents
One study found that 86% of downtime in production systems was due to resource leaks. That covers memory leaks, obviously, but also files not closed, connection pools that get depleted, runnning out of disk space, locks that aren't released, and so on. Of course there are many more issues that occur in production, but those other issues tend to cause other types of incidents rather than downtime.
Not all resources can be managed by the JVM, but it can certainly try and help out where possible. So Josh Bloch's "Automatic Resource Management" proposal for Java 7 is spot on in trying to target this area. The actual implementation targets objects which define Closeable or AutoCloseable, automatically closing the object when it is finished being used.
When I first heard about this applying to Closeables, I misunderstood it to mean that all classes that implement Closeable will automatically have the close method called on the instance when the object goes out of scope. But that is already easily done by simply adding a finalize method for the class which calls close (and is implemented just that way for many classes, e.g. FileOutputStream). No, this proposal is about having the compiler do what the programmer should do but often forgets - closing resources right after you have finished using them.
To achieve this the language is changing very slightly in JDK 7 to add in a new form of the try block where you can define and create a Closeable instance:
try (Something x = new Something()) { ...
(Have a look at the example here).
Usually I'm quite neutral about language changes, but this time I'm really enthusiastic about this one. The improvement in readability is great, but that's just the icing on the cake of excellent proper handling of Closeable resources.
Now on with this month's newsletter. We have all our usual Java performance tools, news, and article links, though still no Javva The Hutt as sadly Glastonbury proved too much for his constitution and he's not yet recovered. Over at fasterj we have a new cartoon showing ClassNotFoundException; and, as usual, we have extracted tips from all of this month's referenced articles.
Java performance tuning related news.
Java performance tuning related tools.
Back to newsletter 116 contents