1 /* 2 * Copyright (c) 2009, 2010, 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 /* 25 * @test 26 * @bug 6795356 27 * @summary Leak caused by javax.swing.UIDefaults.ProxyLazyValue.acc 28 * @author Alexander Potochkin 29 * @run main bug6795356 30 */ 31 32 import java.lang.ref.WeakReference; 33 import java.security.ProtectionDomain; 34 import java.security.AccessController; 35 import java.security.PrivilegedAction; 36 import java.security.AccessControlContext; 37 import java.util.LinkedList; 38 import java.util.List; 39 import javax.swing.*; 40 41 public class bug6795356 { 42 volatile static WeakReference<ProtectionDomain> weakRef; 43 44 public static void main(String[] args) throws Exception { 45 46 ProtectionDomain domain = new ProtectionDomain(null, null); 47 48 AccessController.doPrivileged(new PrivilegedAction<Object>() { 49 public Object run() { 50 51 // this initialize ProxyLazyValues 52 UIManager.getLookAndFeel(); 53 54 return null; 55 } 56 }, new AccessControlContext(new ProtectionDomain[]{domain})); 57 58 weakRef = new WeakReference<ProtectionDomain>(domain); 59 domain = null; 60 61 // Generate OutOfMemory and check the weak ref 62 generateOOME(); 63 64 if (weakRef.get() != null) { 65 throw new RuntimeException("Memory leak found!"); 66 } 67 System.out.println("Test passed"); 68 } 69 70 static void generateOOME() { 71 List<Object> bigLeak = new LinkedList<Object>(); 72 boolean oome = false; 73 System.out.print("Filling the heap"); 74 try { 75 for(int i = 0; true ; i++) { 76 // Now, use up all RAM 77 bigLeak.add(new byte[1024 * 1024]); 78 System.out.print("."); 79 80 // Give the GC a change at that weakref 81 if (i % 10 == 0) { 82 System.gc(); 83 try { 84 Thread.sleep(100); 85 } catch (InterruptedException e) { 86 e.printStackTrace(); 87 } 88 } 89 } 90 } catch (OutOfMemoryError e) { 91 bigLeak = null; 92 oome = true; 93 } 94 System.out.println(""); 95 if (!oome) { 96 throw new RuntimeException("Problem with test case - never got OOME"); 97 } 98 System.out.println("Got OOME"); 99 } 100 }