Path: blob/master/test/jdk/java/awt/LightweightDispatcher/LWDispatcherMemoryLeakTest.java
41149 views
/*1* Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.AWTException;24import java.awt.FlowLayout;25import java.awt.Robot;26import java.lang.ref.Reference;27import java.lang.ref.WeakReference;28import java.util.ArrayList;29import java.util.List;30import java.util.logging.Level;31import java.util.logging.Logger;32import javax.swing.JButton;33import javax.swing.JFrame;34import javax.swing.JPanel;35import javax.swing.SwingUtilities;36import test.java.awt.regtesthelpers.Util;3738/*39@test40@key headful41@bug 7079254 816326142@summary Toolkit eventListener leaks memory43@library ../regtesthelpers44@build Util45@compile LWDispatcherMemoryLeakTest.java46@run main/othervm -Xmx10M LWDispatcherMemoryLeakTest47*/48public class LWDispatcherMemoryLeakTest {4950private static JFrame frame;51private static WeakReference<JButton> button;52private static WeakReference<JPanel> p;5354public static void init() throws Throwable {55SwingUtilities.invokeAndWait(new Runnable() {56@Override57public void run() {58frame = new JFrame();59frame.setLayout(new FlowLayout());60button = new WeakReference<JButton>(new JButton("Text"));61p = new WeakReference<JPanel>(new JPanel(new FlowLayout()));62p.get().add(button.get());63frame.add(p.get());6465frame.setBounds(500, 400, 200, 200);66frame.setVisible(true);67}68});6970Util.waitTillShown(button.get());71Util.clickOnComp(button.get(), new Robot());7273SwingUtilities.invokeAndWait(new Runnable() {74@Override75public void run() {76frame.remove(p.get());77}78});7980Util.waitForIdle(null);81assertGC();82}8384public static void assertGC() throws Throwable {85List<byte[]> alloc = new ArrayList<byte[]>();86int size = 10 * 1024;87while (true) {88try {89alloc.add(new byte[size]);90} catch (OutOfMemoryError err) {91break;92}93}94alloc = null;95String leakObjs = "";96if (button.get() != null) {97leakObjs = "JButton";98}99if (p.get() != null) {100leakObjs += " JPanel";101}102if (leakObjs != "") {103throw new Exception("Test failed: " + leakObjs + " not collected");104}105}106107public static void main(String args[]) throws Throwable {108init();109}110}111112113