File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Analysing Problem with Static Singleton Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Analysing Problem with Static Singleton" Watch "Analysing Problem with Static Singleton" New topic

Analysing Problem with Static Singleton

Mohamed Farouk
Ranch Hand

Joined: Jun 08, 2005
Posts: 249
Please help, I am trying to figure out a problem in live with the following piece of code.

This is unusual coding style where in someone has created an Instance of an Singleton object using an local instance inside the method first and then finally assigning to static (Singleton) instance.

But what i find is Finalizer thread running frequently on these local instance which will destroy PinServer31 object which is not expected to happen by design

I understand that code should use static instance rather than local instance to sovle any problem.
But i am not able to explain why currently the below code makes local instances to be garbage collected please can you explain.

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

The only way you'd see instances being garbage collected is if getInstance() is returning a new instance of the class each time. This would happen if the condition (instance._customerSet != customerSet) wasn't true for the static instance -- either never true, or true at first but then becoming untrue. Check out whether getInstance() ever returns a new instance when the old one exists by adding print or log statements or by running the code in a debugger.

[Jess in Action][AskingGoodQuestions]
sammaiah kyatham
Ranch Hand

Joined: Aug 03, 2003
Posts: 104
One thing I can't understand, why do you need to assign it to local instance and compare..
I've slighly modified you code.. hope this will work for you!!!

private PinServer31 _pinServer31;
private static PinServer CLASS_INSTANCE;
// Connect to the PIN Server

void connect() throws PinServerException {
_pinServer31 = PinServer31.getInstance(run_mode, _customerSet);

public static synchronized PinServer getInstance() throws inServerException {

if ((CLASS_INSTANCE == null) {
CLASS_INSTANCE = new PinServer();
} // Connect

Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Sam is right - the local variable doesn't serve any purpose.

The "instance._customerSet != customerSet" looks suspicious to me, too. Can you tell us more about what it might be intended to mean?

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Mohamed Farouk
Ranch Hand

Joined: Jun 08, 2005
Posts: 249
Hello Guys
Thanks for your replies, I as well dont understand the reason why the code was written like this. This is a existing piece of code in live which is causing trouble as I can see finalizer thread being run ever minute to clean up something which destroyes PinServer31 causing huge performance implications which I have found.

I am sure there is something wrong and as well I understand that the way to fix is to remove the local instance from the getInstance method.

What I dont understand is why is the local instance garbage collected every minute when there is a reference to it by the static instance?

I need to explain the problem as to why this code is incorrect, to fix it.

Please help! how will you reason out why it is incorrect and why does the local reference to be garbage collected often?

Mohamed Farouk
Mohamed Farouk
Ranch Hand

Joined: Jun 08, 2005
Posts: 249
Please do not worry about customer set , as i forgot to remove that line before posting it. The code in live contains that attribute as well but i thaught i can remove it for clarity. So please do not worry about it.

Mohamed Farouk
Ranch Hand

Joined: Jun 08, 2005
Posts: 249
Sorry Guys I figured out what is going on myself
I agree. Here's the link:
subject: Analysing Problem with Static Singleton
It's not a secret anymore!