1/*
2 * Copyright (c) 2013, 2016, 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
24import java.awt.AWTException;
25import java.awt.FlowLayout;
26import java.awt.Robot;
27import java.lang.ref.Reference;
28import java.lang.ref.WeakReference;
29import java.util.ArrayList;
30import java.util.List;
31import java.util.logging.Level;
32import java.util.logging.Logger;
33import javax.swing.JButton;
34import javax.swing.JFrame;
35import javax.swing.JPanel;
36import javax.swing.SwingUtilities;
37import test.java.awt.regtesthelpers.Util;
38
39/*
40 @test
41 @key headful
42 @bug 7079254 8163261
43 @summary Toolkit eventListener leaks memory
44 @library ../regtesthelpers
45 @build Util
46 @compile LWDispatcherMemoryLeakTest.java
47 @run main/othervm -Xmx10M LWDispatcherMemoryLeakTest
48 */
49public 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        String leakObjs = "";
97        if (button.get() != null) {
98            leakObjs = "JButton";
99        }
100        if (p.get() != null) {
101            leakObjs += " JPanel";
102        }
103        if (leakObjs != "") {
104            throw new Exception("Test failed: " + leakObjs + " not collected");
105        }
106    }
107
108    public static void main(String args[]) throws Throwable {
109        init();
110    }
111}
112