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.EventQueue;
  25 
  26 import javax.swing.JFrame;
  27 import javax.swing.JScrollPane;
  28 import javax.swing.JTextArea;
  29 
  30 /**
  31  * @test
  32  * @bug 8072775
  33  * @run main/othervm -Xmx80m TextViewOOM
  34  */
  35 public class TextViewOOM {
  36 
  37     private static JFrame frame;
  38     private static JTextArea ta;
  39     private static final String STRING = "\uDC00\uD802\uDFFF";
  40     private static final int N = 5000;
  41 
  42     private static void createAndShowGUI() {
  43         frame = new JFrame();
  44         final JScrollPane jScrollPane1 = new JScrollPane();
  45         ta = new JTextArea();
  46 
  47         ta.setEditable(false);
  48         ta.setColumns(20);
  49         ta.setRows(5);
  50         jScrollPane1.setViewportView(ta);
  51         frame.add(ta);
  52 
  53         frame.pack();
  54         frame.setLocationRelativeTo(null);
  55         frame.setVisible(true);
  56     }
  57 
  58     public static void main(final String[] args) throws Exception {
  59         /* Create and display the form */
  60         EventQueue.invokeAndWait(TextViewOOM::createAndShowGUI);
  61         for (int i = 0; i < 10; i++) {
  62             System.gc();
  63             Thread.sleep(1000);
  64         }
  65         long mem = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
  66         System.err.println("Memory before creating the text: "+mem);
  67         final StringBuilder sb = new StringBuilder(N * STRING.length());
  68         for (int i = 0; i < N; i++) {
  69             sb.append(STRING);
  70         }
  71         for (int i = 0; i < 10; i++) {
  72             System.gc();
  73             Thread.sleep(1000);
  74         }
  75         mem = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
  76         System.err.println("Memory after  creating the text: "+mem);
  77 
  78         EventQueue.invokeAndWait(() -> {
  79             ta.setText(sb.toString());
  80             for (int i = 0; i < 10; i++) {
  81                 System.gc();
  82                 try {Thread.sleep(200);} catch (InterruptedException iex) {}
  83             }
  84             long mem1 = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
  85             System.err.println("Memory after  setting the text: " + mem1);
  86         });
  87         for (int i = 0; i < 10; i++) {
  88             System.gc();
  89             Thread.sleep(1000);
  90         }
  91         mem = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
  92         System.err.println("Final memory  after everything: " + mem);
  93         EventQueue.invokeAndWait(frame::dispose);
  94     }
  95 }