1 /*
   2  * Copyright (c) 2015, 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.BorderLayout;
  25 import java.awt.Dimension;
  26 import java.awt.EventQueue;
  27 import java.awt.GraphicsEnvironment;
  28 
  29 import javax.swing.JFrame;
  30 import javax.swing.JLabel;
  31 
  32 import sun.java2d.SunGraphicsEnvironment;
  33 
  34 /**
  35  * @test
  36  * @bug 8041654
  37  * @run main/othervm -Xmx80m DisplayListenerLeak
  38  */
  39 public final class DisplayListenerLeak {
  40 
  41     private static JFrame frame;
  42     private volatile static boolean failed = false;
  43 
  44     private static void createAndShowGUI() {
  45         Thread.currentThread().setUncaughtExceptionHandler((t, e) -> {
  46             e.printStackTrace();
  47             failed = true;
  48         });
  49         frame = new JFrame();
  50         JLabel emptyLabel = new JLabel("");
  51         emptyLabel.setPreferredSize(new Dimension(600, 400));
  52         frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
  53         frame.pack();
  54         frame.setVisible(true);
  55     }
  56 
  57     public static void main(final String[] args) throws Exception {
  58         GraphicsEnvironment ge =
  59                 GraphicsEnvironment.getLocalGraphicsEnvironment();
  60         if (!(ge instanceof SunGraphicsEnvironment)) {
  61             return;
  62         }
  63         EventQueue.invokeAndWait(() -> createAndShowGUI());
  64         SunGraphicsEnvironment sge = (SunGraphicsEnvironment) ge;
  65         final long startTime = System.nanoTime();
  66         while (!failed) {
  67             if (System.nanoTime() - startTime > 60_000_000_000L) {
  68                 break;
  69             }
  70             System.gc(); // clear all weak references
  71             EventQueue.invokeAndWait(() -> {
  72                 frame.setSize(frame.getHeight(), frame.getWidth());
  73                 frame.pack();
  74             });
  75             EventQueue.invokeAndWait(sge::displayChanged);
  76         }
  77         EventQueue.invokeAndWait(frame::dispose);
  78         if (failed) {
  79             throw new RuntimeException();
  80         }
  81     }
  82 }