Path: blob/master/test/jdk/javax/swing/JOptionPane/6464022/bug6464022.java
41153 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* @key headful26* @bug 646402227* @summary Memory leak in JOptionPane.createDialog28* @author Pavel Porvatov29* @library ../../regtesthelpers30* @build Util31* @run main/othervm -mx128m bug646402232*/3334import javax.swing.*;35import java.lang.ref.WeakReference;36import java.util.ArrayList;37import java.util.List;3839public class bug6464022 {40private static JOptionPane pane;4142public static void main(String[] args) throws Exception {43final List<WeakReference<JDialog>> references = new ArrayList<WeakReference<JDialog>>();4445SwingUtilities.invokeAndWait(new Runnable() {46public void run() {47pane = new JOptionPane(null, JOptionPane.UNDEFINED_CONDITION);4849for (int i = 0; i < 10; i++) {50JDialog dialog = pane.createDialog(null, "Test " + i);5152references.add(new WeakReference<JDialog>(dialog));5354dialog.dispose();5556System.out.println("Disposing Dialog:" + dialog.hashCode());57}58}59});6061Util.generateOOME();6263SwingUtilities.invokeAndWait(new Runnable() {64public void run() {65int allocatedCount = 0;6667for (WeakReference<JDialog> ref : references) {68if (ref.get() != null) {69allocatedCount++;7071System.out.println(ref.get().hashCode() + " is still allocated");72}73}7475if (allocatedCount > 0) {76throw new RuntimeException("Some dialogs still exist in memory. Test failed");77} else {78System.out.println("All dialogs were GCed. Test passed.");79}80}81});82}83}848586