jlibs.core.lang.ref
Class Finalizer

java.lang.Object
  extended by java.lang.ref.ReferenceQueue
      extended by jlibs.core.lang.ref.Finalizer
All Implemented Interfaces:
Runnable

public class Finalizer
extends ReferenceQueue
implements Runnable

This class provides guaranteed finalization for java objects.

This is singleton class and its instance can be accessed as Finalizer.INSTANCE

you can tell Finalizer to execute given runnable when a particular object gets garbage collected:

 Object myObject = ...;
 Finalizer.INSTANCE.track(myObject, new Runnable(){
    public void run(){
        System.out.println("myObject is garbage collected");
    }
 });
 
Finalizer creates a WeakReference for the object that needs to be tracked. you can also tell which type of Reference to use as below:
 Object myObject = ...;
 Finalizer.INSTANCE.track(myObject, SoftReference.class, new Runnable(){
    public void run(){
        System.out.println("myObject is garbage collected");
    }
 });
 
All the track(...) methods return the Reference object created.

You can also use Finalizer as a simple profiler.

 Object myObject = ...;
 Finalizer.INSTANCE.trackGC(myObject, "myObject is garbage collected");
 
the above code says to print "myObject is garbage collected" to console, when myObject gets garbage collected.

This class starts a Thread named "JLibsFinalizer with Thread.MIN_PRIORITY

Author:
Santhosh Kumar T

Field Summary
static Finalizer INSTANCE
           
 
Method Summary
 void run()
           
<T,R extends Reference<T>>
R
track(T obj, Class<R> type, Runnable runnable)
          tracks the given obj using specified reference type.
Specified runnable is executed when obj gets garbage collected.
<T> WeakReference<T>
track(T obj, Runnable runnable)
          tracks the given obj using WeakReference.
Specified runnable is executed when obj gets garbage collected.
 void trackGC(Object obj)
          Prints message to System.out when obj gets garbage collected.
 void trackGC(Object obj, String message)
          Prints message to System.out when obj gets garbage collected
 
Methods inherited from class java.lang.ref.ReferenceQueue
poll, remove, remove
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INSTANCE

public static final Finalizer INSTANCE
Method Detail

run

public void run()
Specified by:
run in interface Runnable

track

public <T> WeakReference<T> track(T obj,
                                  Runnable runnable)
tracks the given obj using WeakReference.
Specified runnable is executed when obj gets garbage collected.

Parameters:
obj - object needs to be tracked
runnable - runnable to be executed when obj gets garbage collected
Returns:
the WeakReference created for obj
See Also:
track(Object, Class, Runnable)

track

public <T,R extends Reference<T>> R track(T obj,
                                          Class<R> type,
                                          Runnable runnable)
tracks the given obj using specified reference type.
Specified runnable is executed when obj gets garbage collected.

Parameters:
obj - object needs to be tracked
type - type of reference to be used for tracking
runnable - runnable to be executed when obj gets garbage collected
Returns:
the Reference created for obj
See Also:
track(Object, Runnable)

trackGC

public void trackGC(Object obj)
Prints message to System.out when obj gets garbage collected.

The message will be obj.getClass().getName()+'@'+System.identityHashCode(obj)

Parameters:
obj - object needs to be tracked
See Also:
trackGC(Object, String)

trackGC

public void trackGC(Object obj,
                    String message)
Prints message to System.out when obj gets garbage collected

Parameters:
obj - object needs to be tracked
message - message to be printed when obj gets garbage collected. if null, the message will be obj.getClass().getName()+'@'+System.identityHashCode(obj)


Copyright © 2018. All rights reserved.