
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 |
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.
| cern::util::testhelpers::GarbageCollectionTestHelper::GarbageCollectionTestHelper | ( | Object | refToSurveil | ) | [inline] |
| 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.
volatile boolean cern::util::testhelpers::GarbageCollectionTestHelper::cancelFlag = false [private] |
Referenced by cancel(), and waitForGC().
final long cern::util::testhelpers::GarbageCollectionTestHelper::GC_SLICE_MILLIS = 200 [static, private] |
Referenced by complexWaitForGC().
Reference cern::util::testhelpers::GarbageCollectionTestHelper::ref [private] |
Referenced by GarbageCollectionTestHelper().
ReferenceQueue cern::util::testhelpers::GarbageCollectionTestHelper::refQ [private] |
Referenced by complexWaitForGC(), GarbageCollectionTestHelper(), and waitForGC().
volatile Thread cern::util::testhelpers::GarbageCollectionTestHelper::waitingThread [private] |
Referenced by cancel(), and waitForGC().
1.6.2