Jack
Jack

Reputation: 121

How to delete a java object from Heap?

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.

Upvotes: 2

Views: 10761

Answers (9)

DukeLover
DukeLover

Reputation: 2457

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");
      }

Upvotes: 0

Andrew Vitkus
Andrew Vitkus

Reputation: 817

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

Upvotes: 0

GKS
GKS

Reputation: 319

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.

Upvotes: 0

Aniket Thakur
Aniket Thakur

Reputation: 68915

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.

Upvotes: 0

Adrian Shum
Adrian Shum

Reputation: 40036

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

Upvotes: 1

Uwe Allner
Uwe Allner

Reputation: 3467

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).

Upvotes: 0

Talha Ahmed Khan
Talha Ahmed Khan

Reputation: 15433

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

Upvotes: 0

TheLostMind
TheLostMind

Reputation: 36304

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.

Upvotes: 1

Niraj Patel
Niraj Patel

Reputation: 2208

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

Upvotes: 4

Related Questions