< prev index next >
test/hotspot/jtreg/vmTestbase/nsk/jdi/ReferenceType/instances/instances003/instances003.java
Print this page
@@ -60,10 +60,11 @@
package nsk.jdi.ReferenceType.instances.instances003;
import java.io.PrintStream;
import java.util.*;
+import com.sun.jdi.ObjectCollectedException;
import com.sun.jdi.ObjectReference;
import com.sun.jdi.ReferenceType;
import nsk.share.Consts;
import nsk.share.ObjectInstancesManager;
@@ -120,31 +121,42 @@
// create 'createInstanceCount' instances of test class
// create temporary strong references to prevent the weakly referred instances being GCed
// during the time between creating them and disabling collection on them
+ boolean useTempStrongReference = needTempStongReference(referrerType);
pipe.println(HeapwalkingDebuggee.COMMAND_CREATE_INSTANCES + ":" + className + ":" + createInstanceCount +
":" + referrerCount + ":" + referrerType +
- (referrerType.equals(ObjectInstancesManager.WEAK_REFERENCE) ? "|" + ObjectInstancesManager.STRONG_REFERENCE : ""));
+ (useTempStrongReference ? "|" + ObjectInstancesManager.STRONG_REFERENCE : ""));
// Note! This test is broken, in the sense that it incorrectly assumes
// that no GC can happen before it walks the heap. In practice, it seems
// to only affect this test when using ZGC. However, this test will also
// fail when using other GCs if an explicit GC is done here.
// the instance counts should not be affected by creating multiple references
checkDebugeeAnswer_instanceCounts(className, createInstanceCount, objectsToFilter);
ReferenceType referenceType = debuggee.classByName(className);
- List<ObjectReference> instances = HeapwalkingDebugger.filterObjectReferrence(objectsToFilter, referenceType.instances(0));
+ List<ObjectReference> allInstances = HeapwalkingDebugger.filterObjectReferrence(objectsToFilter, referenceType.instances(0));
- for (ObjectReference or : instances) {
- or.disableCollection();
+ // There are potentially other non-test Java threads allocating objects and triggering GC's.
+ // We need to call disableCollection() on each object returned by referenceType.instances()
+ // to deal with the case when GC was triggered. Otherwise, these objects can
+ // be potentially collected.
+ List<ObjectReference> instances = new LinkedList<>();
+ for (ObjectReference objRef : allInstances) {
+ try {
+ objRef.disableCollection();
+ instances.add(objRef);
+ } catch (ObjectCollectedException ex) {
+ // skip this references
+ }
}
// remove the temporary strong references so the weak references can be properly tested
- if (referrerType.equals(ObjectInstancesManager.WEAK_REFERENCE)) {
+ if (useTempStrongReference) {
pipe.println(HeapwalkingDebuggee.COMMAND_DELETE_REFERRERS + ":" + className + ":" + referrerCount + ":" + ObjectInstancesManager.STRONG_REFERENCE);
if (!isDebuggeeReady()) {
return;
}
}
@@ -155,10 +167,11 @@
for (int i = 0; i < preventGCCount; i++) {
instances.get(i).enableCollection();
}
+
pipe.println(HeapwalkingDebuggee.COMMAND_DELETE_INSTANCES + ":" + className + ":" + createInstanceCount);
if (!isDebuggeeReady())
return;
@@ -181,6 +194,15 @@
for (String referenceType : HeapwalkingDebuggee.includedIntoInstancesCountTypes) {
for (String className : testClasses)
testClass(className, referenceType);
}
}
+
+
+ private static boolean needTempStongReference(String referenceType) {
+ return ObjectInstancesManager.WEAK_REFERENCE.equals(referenceType) ||
+ ObjectInstancesManager.JNI_WEAK_REFERENCE.equals(referenceType) ||
+ ObjectInstancesManager.PHANTOM_REFERENCE.equals(referenceType) ||
+ ObjectInstancesManager.SOFT_REFERENCE.equals(referenceType);
+
+ }
}
< prev index next >