Pending.java revision 2495
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4530538
* @summary Basic unit test of
* RuntimeMXBean.getObjectPendingFinalizationCount()
* 1. GC and runFinalization() to get the current pending number
* 2. Create some number of objects with reference and without ref.
* 3. Clear all the references
* 4. GC and runFinalization() and the finalizable objects should
* be garbage collected.
* @author Alexei Guibadoulline and Mandy Chung
*
*/
public class Pending {
final static int NO_REF_COUNT = 600;
final static int REF_COUNT = 600;
private static int finalized = 0;
private static MemoryMXBean mbean
private static void printFinalizerInstanceCount() {
if (!trace) return;
}
private static boolean trace = false;
trace = true;
}
try {
if (trace) {
// Turn on verbose:gc to track GC
mbean.setVerbose(true);
}
test();
} finally {
if (trace) {
mbean.setVerbose(false);
}
}
}
// Clean the memory and remove all objects that are pending
// finalization
// Let the finalizer to finish
try {
} catch (Exception e) {
throw e;
}
// Create a number of new objects but no references to them
" finalized = " + finalized);
for (int i = 0; i < NO_REF_COUNT; i++) {
new MyObject();
}
for (int i = 0; i < REF_COUNT; i++) {
}
snapshot = getSnapshot();
// Now check the expected count - GC and runFinalization will be
// invoked.
// Clean the memory and remove all objects that are pending
// finalization again
snapshot = getSnapshot();
snapshot = getSnapshot();
// Check the mbean now
throw new RuntimeException("Wrong number of finalized objects "
+ snapshot + ". Expected "
}
throw new RuntimeException("Wrong number of objects pending "
+ "finalization start = " + startCount
+ " end = " + snapshot);
}
}
throws Exception {
int prevCount = -1;
throw new RuntimeException(
"Unexpected finalized objects: " + snapshot +
" but expected = " + curFinalized);
}
int MAX_GC_LOOP = 6;
for (int i = 1;
i++) {
// Pause to give a chance to Finalizer thread to run
pause();
// Race condition may occur; attempt to check this
// a few times before throwing exception.
for (int j = 0; j < 5; j++) {
// poll for another current pending count
snapshot = getSnapshot();
break;
}
}
// Pause to give a chance to Finalizer thread to run
pause();
snapshot = getSnapshot();
throw new RuntimeException(
"Unexpected current number of objects pending for " +
}
try {
} catch (Exception e) {
throw e;
}
}
throw new RuntimeException(
"Unexpected current number of objects pending for " +
}
}
private static class MyObject {
public void finalize () {
synchronized (lock) {
finalized++;
}
}
}
static class Snapshot {
public int curFinalized;
public int curPending;
Snapshot(int f, int p) {
curFinalized = f;
curPending = p;
}
return "Current finalized = " + curFinalized +
" Current pending = " + curPending;
}
}
private static Snapshot getSnapshot() {
synchronized (lock) {
}
}
private static void pause() {
// Enter lock a without blocking
synchronized (pauseObj) {
try {
// may need to tune this timeout for different platforms
} catch (Exception e) {
}
}
}
}