gcRadar ObjectTreeMonitor Example
GCMoniterTest.java
/*This file is part of gcRadar.
gcRadar is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation version 3 of the License.
gcRadar is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with gcRadar. If not, see <http://www.gnu.org/licenses/>.
*/
package test;
import java.util.Set;
import test.ds.TreeTester_1;
import com.gcr.callbacks.GcRadarCallback;
import com.gcr.monitors.ObjectTreeMonitor;
import com.gcr.structs.AbstractObjectRefrenceKey;
import com.gcr.structs.annotation.GcRadarNotToInclude;
import com.gcr.structs.annotation.GcRadarToInclude;
public class GCMoniterTest implements GcRadarCallback
{
public static void main(String[] args)
{
GCMoniterTest gcMoniterTest = new GCMoniterTest();
System.out.println("========Starting Optimistic Test=========");
gcMoniterTest.doTestTree(true);
System.gc();
System.out.println("========Ending Optimistic Test=========");
System.out.println();
System.out.println("========Starting Pesimistic Test=========");
gcMoniterTest.doTestTree(false);
System.gc();
System.out.println("========Ending Pesimistic Test=========");
}
private void doTestTree(boolean isOptimistic)
{
System.out.println("GCMoniterTest.doTestTree(), isOptimistic - " + isOptimistic);
// Create complex object to be added to monitoring
TreeTester_3 tt3 = new TreeTester_3();
// Instantiate an ObjectTreeMonitor instance using the method parameter
// to set optimism
ObjectTreeMonitor oTreeMonitor = new ObjectTreeMonitor(isOptimistic);
// Add the complex object to the monitoring list, provide a string-key
// that will be used as an alias name for this object and its comprising
// members during monitoring.
oTreeMonitor.addObject(tt3, "tree_tester_3", this);
// Start the background monitoring thread. Events will be received only
// after this method call.
oTreeMonitor.startMonitoring();
System.out.println("Number of pending objects - " + oTreeMonitor.getPendingObjectsCount());
// Orphan the object so that the Garbage collector can collect it. This
// has been done artificially for this example
tt3 = null;
// wait for the garbage collector to work
try
{
Thread.sleep(5000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println("Number of pending objects - " + oTreeMonitor.getPendingObjectsCount());
// If any pending objecta are still left then print there string-keys
Set<AbstractObjectRefrenceKey<Object>> pendingObjects = oTreeMonitor.getPendingObjects();
if (pendingObjects.size() != 0)
{
System.out.println("Following are the keys of the pending objects ");
for (AbstractObjectRefrenceKey<Object> a : pendingObjects)
{
System.out.println(a.getObjRefrenceKey());
}
}
oTreeMonitor.stopMonitoring();
}
@Override
public <T> void objectReclaimedByGC(AbstractObjectRefrenceKey<T> refrenceKey)
{
System.out.println(" - GCed - " + refrenceKey.getObjRefrenceKey() + " at " + refrenceKey.getPhantomCallbackTime());
}
@Override
public <T> void noSurvivingRefrence(AbstractObjectRefrenceKey<T> objWrapper)
{
System.out.println(" - About to GC - " + objWrapper.getObjRefrenceKey() + " at " + objWrapper.getWeakCallbackTime());
}
// This annotation will cause the monitor to prevent the members of this
// class from being added to monitoring in the Optimistic mode
@GcRadarNotToInclude
class TreeTester_2 extends TreeTester_1
{
public int publicPrimInt_2;
private int privatePrimInt_2;
protected int protectedPrimInt_2;
int defaultPrimInt_2;
public Integer publicInt_2 = new Integer(5);
private Integer privateInt_2 = new Integer(5);
protected Integer protectedInt_2 = new Integer(5);
Integer defaultInt_2 = new Integer(5);
}
class TreeTester_3 extends TreeTester_2
{
public int publicPrimInt_3;
private int privatePrimInt_3;
protected int protectedPrimInt_3;
int defaultPrimInt_3;
// This annotation will cause this member to be included to monitoring
// in the pessimistic mode of operation
@GcRadarToInclude
public Integer publicInt_3 = new Integer(5);
private Integer privateInt_3 = new Integer(5);
protected Integer protectedInt_3 = new Integer(5);
Integer defaultInt_3 = new Integer(5);
}
}
TreeTester_1.java
package test.ds;
import com.gcr.structs.annotation.GcRadarToInclude;
public class TreeTester_1
{
public int publicPrimInt;
private int privatePrimInt;
protected int protectedPrimInt;
int defaultPrimInt;
@GcRadarToInclude
public Integer publicInt = new Integer(5);
private Integer privateInt = new Integer(5);
protected Integer protectedInt = new Integer(5);
Integer defaultInt = new Integer(5);
}