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