1/* 2 * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23/* 24 * @test 25 * @bug 4405807 26 * @modules java.base/java.util:open 27 * @run main/othervm -Xms10m ReferencesTest 28 * @summary Verify that references from ResourceBundle cache don't prevent 29 * class loader reclamation. 30 */ 31 32import java.io.File; 33import java.lang.ref.WeakReference; 34import java.lang.reflect.Field; 35import java.net.URL; 36import java.net.URLClassLoader; 37import java.util.Locale; 38import java.util.Map; 39import java.util.MissingResourceException; 40import java.util.ResourceBundle; 41 42/** 43 * This test relies on the current behavior of the garbage collector and is 44 * therefore no clear indicator of whether the fix for 4405807 works. 45 * If the test fails, it might indicate a regression, or it might just mean 46 * that a less aggressive garbage collector is used. 47 */ 48public class ReferencesTest { 49 50 private static final int CLASS_LOADER_COUNT = 20; 51 52 // These two parallel arrays have references to the same class loaders. 53 // The weakLoaders array lets us track whether class loaders are being 54 // reclaimed after the references in the loaders array are nulled out. 55 private static ClassLoader[] loaders = new ClassLoader[CLASS_LOADER_COUNT]; 56 private static WeakReference[] weakLoaders = new WeakReference[CLASS_LOADER_COUNT]; 57 58 public static void main(String[] args) throws Exception { 59 60 URL testDirectory = new File(System.getProperty("test.classes", ".")).toURL(); 61 62 for (int i = 0; i < loaders.length; i++) { 63 URL[] urls = { testDirectory }; 64 loaders[i] = new URLClassLoader(urls); 65 weakLoaders[i] = new WeakReference(loaders[i]); 66 } 67 68 // fill the ResourceBundle cache with entries for half the class loaders 69 loadBundles(0, CLASS_LOADER_COUNT / 2); 70 71 report("After loading resource bundles for first half of class loaders: "); 72 73 // release the first half of the class loaders 74 for (int i = 0; i < CLASS_LOADER_COUNT / 2; i++) { 75 loaders[i] = null; 76 } 77 78 System.gc(); 79 80 report("After releasing first half of class loaders: "); 81 82 // fill the ResourceBundle cache with entries for second half the class loaders 83 loadBundles(CLASS_LOADER_COUNT / 2, CLASS_LOADER_COUNT); 84 85 report("After loading resource bundles for second half of class loaders: "); 86 87 // release the second half of the class loaders 88 for (int i = CLASS_LOADER_COUNT / 2; i < CLASS_LOADER_COUNT; i++) { 89 loaders[i] = null; 90 } 91 92 System.gc(); 93 94 report("After releasing second half of class loaders: "); 95 96 // The garbage collector in Tiger actually has reclaimed all class 97 // loaders at this point, but in order not to become too dependent 98 // on the current behavior, we only require that the first half 99 // has been reclaimed. 100 if (countLoaders(0, CLASS_LOADER_COUNT / 2) > 0) { 101 throw new RuntimeException("Too many class loaders not reclaimed yet."); 102 } 103 } 104 105 private static void report(String when) throws Exception { 106 int first = countLoaders(0, CLASS_LOADER_COUNT / 2); 107 int second = countLoaders(CLASS_LOADER_COUNT / 2, CLASS_LOADER_COUNT); 108 109 Class clazz = ResourceBundle.class; 110 Field cacheList = clazz.getDeclaredField("cacheList"); 111 cacheList.setAccessible(true); 112 int cacheSize = ((Map)cacheList.get(clazz)).size(); 113 114 System.out.println(when); 115 System.out.println(" " + first + " loaders alive in first half"); 116 System.out.println(" " + second + " loaders alive in second half"); 117 System.out.println(" " + cacheSize + " entries in resource bundle cache"); 118 } 119 120 private static void loadBundles(int start, int end) throws Exception { 121 for (int i = start; i < end; i++) { 122 // There's no resource bundle for NonExistantBundle - this 123 // let's us test the case where a resource bundle is not found, 124 // which in the past created a SoftReference on the value side 125 // of the cache. 126 try { 127 ResourceBundle.getBundle("NonExistantBundle", Locale.US, loaders[i]); 128 } catch (MissingResourceException e) { 129 } 130 // There's a base resource bundle for ReferencesTestBundle - the 131 // normal case. 132 ResourceBundle.getBundle("ReferencesTestBundle", Locale.US, loaders[i]); 133 } 134 } 135 136 private static int countLoaders(int start, int end) { 137 int count = 0; 138 for (int i = start; i < end; i++) { 139 if (weakLoaders[i].get() != null) { 140 count++; 141 } 142 } 143 return count; 144 } 145} 146