Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/LightweightDispatcher/LWDispatcherMemoryLeakTest.java
48425 views
/*1* Copyright (c) 2013, 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@bug 707925441@summary Toolkit eventListener leaks memory42@library ../regtesthelpers43@build Util44@compile LWDispatcherMemoryLeakTest.java45@run main/othervm -Xmx10M LWDispatcherMemoryLeakTest46*/47public class LWDispatcherMemoryLeakTest {4849private static JFrame frame;50private static WeakReference<JButton> button;51private static WeakReference<JPanel> p;5253public static void init() throws Throwable {54SwingUtilities.invokeAndWait(new Runnable() {55@Override56public void run() {57frame = new JFrame();58frame.setLayout(new FlowLayout());59button = new WeakReference<JButton>(new JButton("Text"));60p = new WeakReference<JPanel>(new JPanel(new FlowLayout()));61p.get().add(button.get());62frame.add(p.get());6364frame.setBounds(500, 400, 200, 200);65frame.setVisible(true);66}67});6869Util.waitTillShown(button.get());70Util.clickOnComp(button.get(), new Robot());7172SwingUtilities.invokeAndWait(new Runnable() {73@Override74public void run() {75frame.remove(p.get());76}77});7879Util.waitForIdle(null);80assertGC();81}8283public static void assertGC() throws Throwable {84List<byte[]> alloc = new ArrayList<byte[]>();85int size = 10 * 1024;86while (true) {87try {88alloc.add(new byte[size]);89} catch (OutOfMemoryError err) {90break;91}92}93alloc = null;94if (button.get() != null) {95throw new Exception("Test failed: JButton was not collected");96}97}9899public static void main(String args[]) throws Throwable {100init();101}102}103104105