Thursday, 6 June 2019

Java: ReferenceQueue


Registered reference objects are appended by the garbage collector after the appropriate reachability changes are detected.

Below table summarizes the methods provided by ReferenceQueue class.
Method
Description
public Reference<? extends T> poll()
Polls this queue to see if a reference object is available. If one is available without further delay then it is removed from the queue and returned. Otherwise this method immediately returns null.
public Reference<? extends T> remove(long timeout)
Returns a reference object, if one was available within the specified timeout period, otherwise null
public Reference<? extends T> remove()
Removes the next reference object in this queue, block until one becomes available.

If Garbage collector detects a weekly reachable object, then
a.    Weak reference object referent field is set to null.
b.    The heap object that is referenced by the WeakReference is declared finalizable.
c.    When the heap object's finalize() method is run and its memory freed, the WeakReferenceobject is added to its ReferenceQueue, if it exists.

If Garbage collector wants to free memory of softly reachable object, then
a.    The soft reference object referent field is set to null.
b.    The heap object referenced by the SoftReference is declared finalizable.
c.    When the heap object's finalize() method is run and its memory freed, the SoftReferenceobject is added to its ReferenceQueue, if it exists.

Garbage Collector interaction with Phantom reachable object
a.    The heap object that is referenced by the PhantomReference is declared finalizable.
b.    Unlike soft and weak references, the PhantomReference is added to its ReferenceQueue before the heap object is freed. This allows for some needed actions to be taken before the heap object is reclaime

Find the below working application.

App.java
package com.sample.app;

import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.util.concurrent.TimeUnit;

public class App {

 public static class EmployeeWeakReference extends WeakReference<Employee> {
  EmployeeCleanup cleanup;

  public EmployeeWeakReference(Employee emp, EmployeeCleanup cleanup, ReferenceQueue<Employee> refQueue) {
   super(emp, refQueue);
   this.cleanup = cleanup;
  }

  public void closeResources() {
   cleanup.closeResource();
  }
 }

 public static class EmployeeCleanup {
  int id;

  public EmployeeCleanup(int id) {
   this.id = id;
  }

  public void closeResource() {
   System.out.println("Cleaning up : " + id);
  }
 }

 public static class Employee {

 }

 public static void main(String args[]) throws InterruptedException {
  Employee emp1 = new Employee();
  EmployeeCleanup cleanUp = new EmployeeCleanup(1);
  ReferenceQueue<Employee> refQueue = new ReferenceQueue<>();

  EmployeeWeakReference weakReference = new EmployeeWeakReference(emp1, cleanUp, refQueue);

  emp1 = null;

  System.gc();
  TimeUnit.SECONDS.sleep(5);

  EmployeeWeakReference empWeakReference = (EmployeeWeakReference) refQueue.remove();
  empWeakReference.closeResources();

 }
}

Output
Cleaning up : 1


Previous                                                 Next                                                 Home

No comments:

Post a Comment