< prev index next >

test/hotspot/jtreg/vmTestbase/nsk/jdi/ReferenceType/instances/instances002/instances002.java

Print this page

        

@@ -107,12 +107,26 @@
         if (!(referenceType instanceof ArrayType)) {
             setSuccess(false);
             log.complain("Unexpected reference type: " + referenceType.getClass().getName() + ", expected is ArrayType");
             return;
         }
+        // There are potentially other non-test Java threads allocating objects and triggering GC's.
+        debuggee.suspend();
 
-        baseInstances = referenceType.instances(0).size();
+        List<ObjectReference> baseReferences = new LinkedList<>();
+        // We need to call disableCollection() on each object returned by referenceType.instances()
+        // to deal with the case when GC was triggered before the suspend. Otherwise, these objects can
+        // be potentially collected.
+        for (ObjectReference objRef : referenceType.instances(0)) {
+            try {
+                objRef.disableCollection();
+                baseReferences.add(objRef);
+            } catch (ObjectCollectedException e) {
+                // skip this reference
+            }
+        }
+        baseInstances = baseReferences.size();
 
         int createInstanceCount = 100;
         int arraySize = 1;
 
         ArrayType arrayType = (ArrayType) referenceType;

@@ -127,14 +141,21 @@
             objectReferences.add(arrayReference);
         }
 
         checkDebugeeAnswer_instances(className, createInstanceCount + baseInstances);
 
-        for (ArrayReference arrayReference : objectReferences)
+        for (ArrayReference arrayReference : objectReferences) {
             arrayReference.enableCollection();
     }
 
+        for (ObjectReference baseRef : baseReferences) {
+            baseRef.enableCollection();
+        }
+
+        debuggee.resume();
+    }
+
     // test method ClassType.newInstance
     public void testClassType(String className) {
         // create some instances in target VM, just to get ReferenceType object
         int baseInstances = 10;
 
< prev index next >