Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JOptionPane/6464022/bug6464022.java
38918 views
/*1* Copyright (c) 2011, 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*/2223/*24* @test25* @bug 646402226* @summary Memory leak in JOptionPane.createDialog27* @author Pavel Porvatov28* @library ../../regtesthelpers29* @build Util30* @run main bug646402231*/3233import javax.swing.*;34import java.lang.ref.WeakReference;35import java.util.ArrayList;36import java.util.List;3738public class bug6464022 {39private static JOptionPane pane;4041public static void main(String[] args) throws Exception {42final List<WeakReference<JDialog>> references = new ArrayList<WeakReference<JDialog>>();4344SwingUtilities.invokeAndWait(new Runnable() {45public void run() {46pane = new JOptionPane(null, JOptionPane.UNDEFINED_CONDITION);4748for (int i = 0; i < 10; i++) {49JDialog dialog = pane.createDialog(null, "Test " + i);5051references.add(new WeakReference<JDialog>(dialog));5253dialog.dispose();5455System.out.println("Disposing Dialog:" + dialog.hashCode());56}57}58});5960Util.generateOOME();6162SwingUtilities.invokeAndWait(new Runnable() {63public void run() {64int allocatedCount = 0;6566for (WeakReference<JDialog> ref : references) {67if (ref.get() != null) {68allocatedCount++;6970System.out.println(ref.get().hashCode() + " is still allocated");71}72}7374if (allocatedCount > 0) {75throw new RuntimeException("Some dialogs still exist in memory. Test failed");76} else {77System.out.println("All dialogs were GCed. Test passed.");78}79}80});81}82}838485