1 /*
   2  * Copyright (c) 2013, 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 
  24 import java.awt.AWTException;
  25 import java.awt.FlowLayout;
  26 import java.awt.Robot;
  27 import java.lang.ref.Reference;
  28 import java.lang.ref.WeakReference;
  29 import java.util.ArrayList;
  30 import java.util.List;
  31 import java.util.logging.Level;
  32 import java.util.logging.Logger;
  33 import javax.swing.JButton;
  34 import javax.swing.JFrame;
  35 import javax.swing.JPanel;
  36 import javax.swing.SwingUtilities;
  37 import test.java.awt.regtesthelpers.Util;
  38 
  39 /*
  40  @test
  41  @bug 7079254
  42  @summary Toolkit eventListener leaks memory
  43  @library ../regtesthelpers
  44  @build Util
  45  @compile LWDispatcherMemoryLeakTest.java
  46  @run main/othervm -Xmx10M LWDispatcherMemoryLeakTest
  47  */
  48 public class LWDispatcherMemoryLeakTest {
  49 
  50     private static JFrame frame;
  51     private static WeakReference<JButton> button;
  52     private static WeakReference<JPanel> p;
  53 
  54     public static void init() throws Throwable {
  55         SwingUtilities.invokeAndWait(new Runnable() {
  56             @Override
  57             public void run() {
  58                 frame = new JFrame();
  59                 frame.setLayout(new FlowLayout());
  60                 button = new WeakReference<JButton>(new JButton("Text"));
  61                 p = new WeakReference<JPanel>(new JPanel(new FlowLayout()));
  62                 p.get().add(button.get());
  63                 frame.add(p.get());
  64 
  65                 frame.setBounds(500, 400, 200, 200);
  66                 frame.setVisible(true);
  67             }
  68         });
  69 
  70         Util.waitTillShown(button.get());
  71         Util.clickOnComp(button.get(), new Robot());
  72 
  73         SwingUtilities.invokeAndWait(new Runnable() {
  74             @Override
  75             public void run() {
  76                 frame.remove(p.get());
  77             }
  78         });
  79 
  80         Util.waitForIdle(null);
  81         assertGC();
  82     }
  83 
  84     public static void assertGC() throws Throwable {
  85         List<byte[]> alloc = new ArrayList<byte[]>();
  86         int size = 10 * 1024;
  87         while (true) {
  88             try {
  89                 alloc.add(new byte[size]);
  90             } catch (OutOfMemoryError err) {
  91                 break;
  92             }
  93         }
  94         alloc = null;
  95         if (button.get() != null) {
  96             throw new Exception("Test failed: JButton was not collected");
  97         }
  98     }
  99 
 100     public static void main(String args[]) throws Throwable {
 101         init();
 102     }
 103 }