0

I thought I wrote it right, but I dont follow where the mistake is. I create a singleton class (of ringbuffer using apache commons); this is used in mainactivity and guimanager (both are my implementation)-hence a singleton class for ringbuffer. Above classes get the reference for singleton by calling

    RingBuffer.getInstance() 

At some point, user wants to change the size of the ring buffer, so I recreate the singleton class by calling

   RingBuffer.recreateRingBuffer()

Question: After recreating the instance of singleton class, I was hoping that existing references in mainactivity and guimanager would be modified automatically with the new reference (of ringbuffer with different size). But no, I still have the old reference (the buffere with the old size). In other words, how can I make the return value of getInstance() modified automatically anytime the reference is modified (so the caller methods and the ringbuffer are working on the same copy of the object)

Here is my singleton class

 import java.util.Arrays;
 import org.apache.commons.collections.buffer.CircularFifoBuffer;
 import org.apache.commons.lang3.ArrayUtils;
 import org.apache.commons.math3.stat.StatUtils;
 import android.util.Log;

 public class RingBuffer {
    private CircularFifoBuffer buffer;
    private int size;
    private volatile static  /*final*/ RingBuffer instance = new RingBuffer(7);

private RingBuffer(int n) {
    size = n;
    buffer = new CircularFifoBuffer(size);
}

public static RingBuffer getInstance() {
    return instance;
}

public static RingBuffer recreateRingBuffer(int n) {
    Log.e(Constants.GLOBALTAG,"Recreating RingBuffer of Size "+n);
    instance=null;
    instance=new RingBuffer(n);
    return instance;
 }
}
1
  • Just never store references to the instance anywhere, call getInstance every time you need to access it instead. Commented Jun 7, 2015 at 5:53

2 Answers 2

1

You didn't add the code for mainactivity or guimanager, so it's anyone's guess.

However if the references to RingBuffer aren't "automatically" updated, it's probably because you store the reference in a variable, as such:

RingBuffer rb = RingBuffer.getInstance();

And then use rb variable.

Don't do that if you're going to be changing the reference object (you're basically creating a new object on the heap with a new address, all existing references will still hold the old address). Instance, always reference it using getInstance. Meaning, instead of doing

rb.foo();

Always do:

RingBuffer.getInstance().foo();

That way, anytime you reference RingBuffer's instance, you will reference the truly singleton object, and never have references to any old instances you may have created and discarded.

Sign up to request clarification or add additional context in comments.

Comments

0

After you call

public static RingBuffer recreateRingBuffer(int n) {
    Log.e(Constants.GLOBALTAG,"Recreating RingBuffer of Size "+n);
    instance=null;
    instance=new RingBuffer(n);
    return instance;
 }

You do have two objects. You are just setting reference to null for old object and creating a new one. Then setting old reference to this new object. But old object's reference is still used somewhere in your code. Unless all references are null it will not be GCed.

When you are providing provision to recreate you are defeating the very purpose f singleton. Better provide a setter to change whatever parameter you want to change and not just recreate it.

3 Comments

Garbage collection is irrelevant here and there is no reference being set to nulll but the rest of this answer is on the right track. You need to change values inside instance instead of creating a new RingBuffer.
When you are talking about singleton how is GC irrelevant? :) If you are saying your usecase is that only one instance of Object must reside in your JVM then if you are trying to recreate it first one should be GCed. For that to happen all references pointing to it should be null. OP is doing that instance=null however there are other references which are not null and do not magically start pointing to new object. Also if you point all references to null it is not is developers hand when it will get GCed.
Garbage collection happens after the object becomes unreferenced and is not triggered by the null being assigned to a variable or field. I think the language you have used is confusing. When you say "old object's reference is still used somewhere" I think you mean "the old object is still referenced somewhere". You have confused objects with references and empasised garbage collection over the referencing field "instance"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.