cern::util::testhelpers::GarbageCollectionTestHelper Class Reference

Collaboration diagram for cern::util::testhelpers::GarbageCollectionTestHelper:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 GarbageCollectionTestHelper (Object refToSurveil)
boolean waitForGC ()
boolean waitForGC (long maxTimeout)
boolean complexWaitForGC (long maxTimeout)
void cancel ()

Private Attributes

Reference ref
ReferenceQueue refQ
volatile boolean cancelFlag = false
volatile Thread waitingThread

Static Private Attributes

static final long GC_SLICE_MILLIS = 200

Detailed Description

A helper class to check if Garbage Collection works properly. It allows you to check whether an object is freed by the Garbage Collector.

To check whether an object is garbage collected, create an instance of this class and pass it the reference the object in to be surveilled for GC. Then, call the method waitForGC() to wait until the object is garbage collected. Here is sample code:


      Object obj = new Object();
      GarbageCollectionTestHelper gch = new GarbageCollectionTestHelper(obj);
          obj = null; // enable garbage collection
          if (gch.waitForGC(maxTimeoutMillis)) {
              System.out.println("garbage collection ok");
          } else {
              System.err.println("garbage collection problem");
          }
 

You have to use one instance of this class per reference you want to surveil.

This implementation isbased on the java.lang.ref.* classes. It keeps a PhantomReference to the reference and waits until the instance has been garbage collected.

Version:
0.9.1
Author:
Vito Baggiolini

Constructor & Destructor Documentation

cern::util::testhelpers::GarbageCollectionTestHelper::GarbageCollectionTestHelper ( Object  refToSurveil  )  [inline]

References ref, and refQ.


Member Function Documentation

void cern::util::testhelpers::GarbageCollectionTestHelper::cancel (  )  [inline]

References cancelFlag, and waitingThread.

boolean cern::util::testhelpers::GarbageCollectionTestHelper::complexWaitForGC ( long  maxTimeout  )  [inline]

a more sophisticated algorithm to wait for Property Change Events

References GC_SLICE_MILLIS, and refQ.

boolean cern::util::testhelpers::GarbageCollectionTestHelper::waitForGC ( long  maxTimeout  )  [inline]

a simple algorithm to wait for GC

References refQ.

boolean cern::util::testhelpers::GarbageCollectionTestHelper::waitForGC (  )  [inline]

References cancelFlag, refQ, and waitingThread.


Member Data Documentation

Referenced by cancel(), and waitForGC().

Referenced by complexWaitForGC().

Referenced by cancel(), and waitForGC().


The documentation for this class was generated from the following file:

Generated by  doxygen 1.6.2