2

You have one big object in java. it has got 4 or five references. you don't know all those references. At time on deletion you know only one reference and you want to delete that object completely. How to achieve that? and also if you want to know other references then to what is the best way to do that.

9 Answers 9

4

It is not in our hand.. You can just nullify it from your end..

Object a = new Object();
a = null; // after that, if there is no live thread which is accessing members,it will be deleted by garbage collector
Sign up to request clarification or add additional context in comments.

1 Comment

what about strings
1

You could try Finalize() or System.runFinalization() but frankly, if there are references still pointing to the object, then I think the GC will ignore your request.

Comments

1

It is not possible in Java.

If you have strong reference referring your object, you cannot force JVM to GC that object. It simply cannot guarantee the program will work.

If codes of all other references are in your control, consider changing them to use WeakReference or SoftReference

Comments

0

There are some things that are not in our hands and its better to leave it to the JRE to handle it. All we can do that we make sure that the we make them null explicitly after using them.

{
  // Some block
  HugeObject obj = HugeObject.getInstance();
  // Use it 
  obj = null;
}
// end of block

Comments

0

Java memory handling is just built to prevent that. An object is guaranteed to live as long as a reference to this object exists. As far as I know there is no (official) way to get to know the other references to an object (and there should be no need for that).

Comments

0

In Java GC(Garbage collector) handles heap cleanup. If an Object has no live references to it then it will automatically be cleaned up. So you need to make sure there are no live references to the Object.

Making it null is one of the way. But it will not guarantee it's cleanup if there is some other Object pointing to the same reference. That is why writing good code involves closing all the resources after use which includes making it to null.

If you are running low on heap you can try increasing heap size or calling System.gc() but again calling gc manually does not guarantee gc will actually be performed. it depends on lot of parameters which are JVM dependent.

Comments

0

What kind of references are these to the object? Are these references created by you and at runtime you don't keep track of of those references. If this is the case, you can wrap your references to the object in soft/ weak reference and then explicitly run the GC request. Otherwise, on runtime, if any live thread has access to the object. GC shall not delete that object.

Comments

0

It is hard to answer no knowing your use case, but if there is one location that you want to be able to remove it from then you can store every other reference to it as a WeakReference. Java normally uses strong refrences when referencing objects and the GC will only clear something when it has no more strong references. However, if you use WeakRefrences and your strong refrence ever goes out of scope there is no guarantee that your data will remain even if it is still needed.

I could be mistaken about this though, as I haven't used this class in a year or two.

On WeakReferences: http://docs.oracle.com/javase/7/docs/api/java/lang/ref/WeakReference.html

Comments

0

You can declare your objects as WeakReference and add them in ReferenceQueue. In this way , whenever your object will not be further referenced , it will be liable for GC.

    /**
        Initialize the reference queue , even if you don't do it , no problem . Default reference queue will be taken.
        **/
        ReferenceQueue<? super Object> testReferenceQueue = new ReferenceQueue<Object>();

       Map<String,String> demoHashMap = new HashMap<String,String>();

       demoHashMap.put("SomeValue","testValue");
       // Declare the object as weak object and put it in reference queue 
       WeakReference<?> weakObject = new WeakReference<Object>(demoHashMap,testReferenceQueue );

    demoHashMap.clear();  
    demoHashMap = null; // This object is not referenced from anywhere

     if(weakObject!=null){
     System.out.println("Object is not GCd yet");
      }else{
       System.out.println("It is already garbage collected");
      }

Comments

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.