8217385: JTREG: Clean up, make sure to close resources
authorlkorinth
Mon, 18 Feb 2019 10:48:48 +0100
changeset 53787 88b84fc64d34
parent 53786 2a78b2963793
child 53788 9e7e9506bee2
8217385: JTREG: Clean up, make sure to close resources Reviewed-by: tschatzl, sangheki
test/hotspot/jtreg/gc/g1/humongousObjects/TestHumongousClassLoader.java
test/hotspot/jtreg/gc/g1/plab/lib/LogParser.java
test/hotspot/jtreg/gc/stress/gcbasher/TestGCBasher.java
--- a/test/hotspot/jtreg/gc/g1/humongousObjects/TestHumongousClassLoader.java	Wed Feb 13 13:27:17 2019 +0100
+++ b/test/hotspot/jtreg/gc/g1/humongousObjects/TestHumongousClassLoader.java	Mon Feb 18 10:48:48 2019 +0100
@@ -156,6 +156,7 @@
         URLClassLoader urlLoader = new URLClassLoader(url);
 
         Class<?> simpleClassLoaderClass = urlLoader.loadClass(SIMPLE_CLASSLOADER_NAME);
+        urlLoader.close();
 
         ClassLoader simpleClassLoader = (ClassLoader) simpleClassLoaderClass
                 .getConstructor(java.lang.ClassLoader.class)
--- a/test/hotspot/jtreg/gc/g1/plab/lib/LogParser.java	Wed Feb 13 13:27:17 2019 +0100
+++ b/test/hotspot/jtreg/gc/g1/plab/lib/LogParser.java	Mon Feb 18 10:48:48 2019 +0100
@@ -95,36 +95,37 @@
     }
 
     private PlabReport parseLines() throws NumberFormatException {
-        Scanner lineScanner = new Scanner(log);
-        PlabReport plabReport = new PlabReport();
-        Optional<Long> gc_id;
-        while (lineScanner.hasNextLine()) {
-            String line = lineScanner.nextLine();
-            gc_id = getGcId(line, GC_ID_PATTERN);
-            if (gc_id.isPresent()) {
-                Matcher matcher = PAIRS_PATTERN.matcher(line);
-                if (matcher.find()) {
-                    if (!plabReport.containsKey(gc_id.get())) {
-                        plabReport.put(gc_id.get(), new PlabGCStatistics());
+        try (Scanner lineScanner = new Scanner(log)) {
+            PlabReport plabReport = new PlabReport();
+            Optional<Long> gc_id;
+            while (lineScanner.hasNextLine()) {
+                String line = lineScanner.nextLine();
+                gc_id = getGcId(line, GC_ID_PATTERN);
+                if (gc_id.isPresent()) {
+                    Matcher matcher = PAIRS_PATTERN.matcher(line);
+                    if (matcher.find()) {
+                        if (!plabReport.containsKey(gc_id.get())) {
+                            plabReport.put(gc_id.get(), new PlabGCStatistics());
+                        }
+                        ReportType reportType = line.contains("Young") ? ReportType.SURVIVOR_STATS : ReportType.OLD_STATS;
+
+                        PlabGCStatistics gcStat = plabReport.get(gc_id.get());
+                        if (!gcStat.containsKey(reportType)) {
+                            gcStat.put(reportType, new PlabInfo());
+                        }
+
+                        // Extract all pairs from log.
+                        PlabInfo plabInfo = gcStat.get(reportType);
+                        do {
+                            String pair = matcher.group();
+                            String[] nameValue = pair.replaceAll(": ", ":").split(":");
+                            plabInfo.put(nameValue[0].trim(), Long.parseLong(nameValue[1]));
+                        } while (matcher.find());
                     }
-                    ReportType reportType = line.contains("Young") ? ReportType.SURVIVOR_STATS : ReportType.OLD_STATS;
-
-                    PlabGCStatistics gcStat = plabReport.get(gc_id.get());
-                    if (!gcStat.containsKey(reportType)) {
-                        gcStat.put(reportType, new PlabInfo());
-                    }
-
-                    // Extract all pairs from log.
-                    PlabInfo plabInfo = gcStat.get(reportType);
-                    do {
-                        String pair = matcher.group();
-                        String[] nameValue = pair.replaceAll(": ", ":").split(":");
-                        plabInfo.put(nameValue[0].trim(), Long.parseLong(nameValue[1]));
-                    } while (matcher.find());
                 }
             }
+            return plabReport;
         }
-        return plabReport;
     }
 
     private static Optional<Long> getGcId(String line, Pattern pattern) {
--- a/test/hotspot/jtreg/gc/stress/gcbasher/TestGCBasher.java	Wed Feb 13 13:27:17 2019 +0100
+++ b/test/hotspot/jtreg/gc/stress/gcbasher/TestGCBasher.java	Mon Feb 18 10:48:48 2019 +0100
@@ -38,14 +38,15 @@
         HashMap<String, ClassInfo> deps = new HashMap<>();
 
         FileSystem fs = FileSystems.getFileSystem(URI.create("jrt:/"));
-        Stream<Path> s = Files.walk(fs.getPath("/"));
-        for (Path p : (Iterable<Path>)s::iterator) {
-            if (p.toString().endsWith(".class") &&
-                !p.getFileName().toString().equals("module-info.class")) {
-                byte[] data = Files.readAllBytes(p);
-                Decompiler d = new Decompiler(data);
-                ClassInfo ci = d.getClassInfo();
-                deps.put(ci.getName(), ci);
+        try (Stream<Path> s = Files.walk(fs.getPath("/"))) {
+            for (Path p : (Iterable<Path>)s::iterator) {
+                if (p.toString().endsWith(".class") &&
+                    !p.getFileName().toString().equals("module-info.class")) {
+                    byte[] data = Files.readAllBytes(p);
+                    Decompiler d = new Decompiler(data);
+                    ClassInfo ci = d.getClassInfo();
+                    deps.put(ci.getName(), ci);
+                }
             }
         }
     }