1 /* 2 * Copyright (c) 2013, 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 import java.awt.AWTException; 25 import java.awt.FlowLayout; 26 import java.awt.Robot; 27 import java.lang.ref.Reference; 28 import java.lang.ref.WeakReference; 29 import java.util.ArrayList; 30 import java.util.List; 31 import java.util.logging.Level; 32 import java.util.logging.Logger; 33 import javax.swing.JButton; 34 import javax.swing.JFrame; 35 import javax.swing.JPanel; 36 import javax.swing.SwingUtilities; 37 import test.java.awt.regtesthelpers.Util; 38 39 /* 40 @test 41 @key headful 42 @bug 7079254 43 @summary Toolkit eventListener leaks memory 44 @library ../regtesthelpers 45 @build Util 46 @compile LWDispatcherMemoryLeakTest.java 47 @run main/othervm -Xmx10M LWDispatcherMemoryLeakTest 48 */ 49 public class LWDispatcherMemoryLeakTest { 50 51 private static JFrame frame; 52 private static WeakReference<JButton> button; 53 private static WeakReference<JPanel> p; 54 55 public static void init() throws Throwable { 56 SwingUtilities.invokeAndWait(new Runnable() { 57 @Override 58 public void run() { 59 frame = new JFrame(); 60 frame.setLayout(new FlowLayout()); 61 button = new WeakReference<JButton>(new JButton("Text")); 62 p = new WeakReference<JPanel>(new JPanel(new FlowLayout())); 63 p.get().add(button.get()); 64 frame.add(p.get()); 65 66 frame.setBounds(500, 400, 200, 200); 67 frame.setVisible(true); 68 } 69 }); 70 71 Util.waitTillShown(button.get()); 72 Util.clickOnComp(button.get(), new Robot()); 73 74 SwingUtilities.invokeAndWait(new Runnable() { 75 @Override 76 public void run() { 77 frame.remove(p.get()); 78 } 79 }); 80 81 Util.waitForIdle(null); 82 assertGC(); 83 } 84 85 public static void assertGC() throws Throwable { 86 List<byte[]> alloc = new ArrayList<byte[]>(); 87 int size = 10 * 1024; 88 while (true) { 89 try { 90 alloc.add(new byte[size]); 91 } catch (OutOfMemoryError err) { 92 break; 93 } 94 } 95 alloc = null; 96 if (button.get() != null) { 97 throw new Exception("Test failed: JButton was not collected"); 98 } 99 } 100 101 public static void main(String args[]) throws Throwable { 102 init(); 103 } 104 }