1 
   2 import java.awt.AWTException;
   3 import java.awt.FlowLayout;
   4 import java.awt.Robot;
   5 import java.lang.ref.Reference;
   6 import java.lang.ref.WeakReference;
   7 import java.util.ArrayList;
   8 import java.util.List;
   9 import java.util.logging.Level;
  10 import java.util.logging.Logger;
  11 import javax.swing.JButton;
  12 import javax.swing.JFrame;
  13 import javax.swing.JPanel;
  14 import javax.swing.SwingUtilities;
  15 import test.java.awt.regtesthelpers.Util;
  16 
  17 /*
  18  @test
  19  @bug 7079260
  20  @summary XInputContext leaks memory by needRecetXXIClient field
  21  @library ../regtesthelpers
  22  @build Util
  23  @compile LWDispatcherMemoryLeakTest.java
  24  @run main/othervm -Xmx10M LWDispatcherMemoryLeakTest
  25  */
  26 public class LWDispatcherMemoryLeakTest {
  27 
  28     private static JFrame frame;
  29     private static WeakReference<JButton> button;
  30     private static WeakReference<JPanel> p;
  31 
  32     public static void init() throws Throwable {
  33         SwingUtilities.invokeAndWait(new Runnable() {
  34             @Override
  35             public void run() {
  36                 frame = new JFrame();
  37                 frame.setLayout(new FlowLayout());
  38                 button = new WeakReference<JButton>(new JButton("Text"));
  39                 p = new WeakReference<JPanel>(new JPanel(new FlowLayout()));
  40                 p.get().add(button.get());
  41                 frame.add(p.get());
  42 
  43                 frame.setBounds(500, 400, 200, 200);
  44                 frame.setVisible(true);
  45             }
  46         });
  47 
  48         Util.waitTillShown(button.get());
  49         Util.clickOnComp(button.get(), new Robot());
  50 
  51         SwingUtilities.invokeAndWait(new Runnable() {
  52             @Override
  53             public void run() {
  54                 frame.remove(p.get());
  55             }
  56         });
  57 
  58         Util.waitForIdle(null);
  59         assertGC();
  60     }
  61 
  62     public static void assertGC() throws Throwable {
  63         List<byte[]> alloc = new ArrayList<byte[]>();
  64         int size = 10 * 1024;
  65         while (true) {
  66             try {
  67                 alloc.add(new byte[size]);
  68             } catch (OutOfMemoryError err) {
  69                 break;
  70             }
  71         }
  72         alloc = null;
  73         if (button.get() != null) {
  74             throw new Exception("Test failed: JButton was not collected");
  75         }
  76     }
  77 
  78     public static void main(String args[]) throws Throwable {
  79         init();
  80     }
  81 }