WeakReference common mistake

Posted in java by pedro | Wednesday, April 1st, 2009 at 4:55 am

Last weekend I was on NetBeans Platform Training conducted by Geertjan and Karol Harezlak from Sun Microsystem. This was a great training. One slot was about Netbeans Issuezilla and how to contribute, provide patches and so on. Karol showed us his daily work, and he showed one of the bugs he had reported and how looks all the process from issue report to make issue resolved.

All attendees try to help Karol to do this, I’ve got some fillings that the solution is still wrong but I wasn’t sure. So after training I’m came home and try this.

Code Snapshot:

codesnapshot

Wrong method : The problem is that after checking that  we have a reference and it is not null, and using it we may have gc executed and another call to refString.get() is null !!!

wrong-method

Good method: We crate strong reference during all method execution so both refString.get() is null or not during all method body.

good-method

To run this you should adjust -Xmx parameter. -Xmx256m works for me.

So the problem is that even we check reference is not null, garbage collector can start and after check we’ve got  NullPointerException. The solution is easy, we must make strong reference (just for the method execution).

I hope this will help you in daily life.  Here is source file, so you can check it on yout own.

One Response to “WeakReference common mistake”

  1. [...] outcome of NetBeans training I was mention in previous post was information about java.util.ServiceLoader [...]

Leave a Reply

about me

My name is Sebastian Pietrowski. I've finished Warsaw University as Master degree. During my studies I started work for merlin.pl. The primary language I use is Java but I have also programmed in Python, Ruby and Scala. I worked as a technical solution architect at merlin.pl. infrastructure when we were moving from PL/SQL to J2EE. I engineering a great performance optimized solution that made the application 10 times faster than requirements and 85 times faster as original solution.

Currently, I am working as a Senior Expert at F.Hoffmann-La Roche to help define future roadmap in design and development of Enterprise software at Roche and Genentech and build adoption for new technologies. I'm continuously mentoring new developers, helping them understand how important test driven development is and empowering them to get better at their daily job. I'm involved in many activities which brings new technologies for better and faster development. You can find more details on my LinkedIn profile.

But don’t get me wrong, I am not your typical nerd. I'm a pleasant guy that you can drink a glass of wine with me and talk about a range of topics with. My leisure activities include playing basketball, soccer and listening to music. I try to be pragmatic while staying focused on application performance and tuning with success in my daily work.

My favorite quote from Yoda's and my life’s motto is: Do, or do not. There is no try.