Path: blob/master/test/jdk/javax/swing/JTextArea/TextViewOOM/TextViewOOM.java
41154 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.EventQueue;2425import javax.swing.JFrame;26import javax.swing.JScrollPane;27import javax.swing.JTextArea;2829/**30* @test31* @key headful32* @bug 807277533* @run main/othervm -Xmx80m TextViewOOM34*/35public class TextViewOOM {3637private static JFrame frame;38private static JTextArea ta;39private static final String STRING = "\uDC00\uD802\uDFFF";40private static final int N = 5000;4142private static void createAndShowGUI() {43frame = new JFrame();44final JScrollPane jScrollPane1 = new JScrollPane();45ta = new JTextArea();4647ta.setEditable(false);48ta.setColumns(20);49ta.setRows(5);50jScrollPane1.setViewportView(ta);51frame.add(ta);5253frame.pack();54frame.setLocationRelativeTo(null);55frame.setVisible(true);56}5758public static void main(final String[] args) throws Exception {59/* Create and display the form */60EventQueue.invokeAndWait(TextViewOOM::createAndShowGUI);61for (int i = 0; i < 10; i++) {62System.gc();63Thread.sleep(1000);64}65long mem = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();66System.err.println("Memory before creating the text: "+mem);67final StringBuilder sb = new StringBuilder(N * STRING.length());68for (int i = 0; i < N; i++) {69sb.append(STRING);70}71for (int i = 0; i < 10; i++) {72System.gc();73Thread.sleep(1000);74}75mem = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();76System.err.println("Memory after creating the text: "+mem);7778EventQueue.invokeAndWait(() -> {79ta.setText(sb.toString());80for (int i = 0; i < 10; i++) {81System.gc();82try {Thread.sleep(200);} catch (InterruptedException iex) {}83}84long mem1 = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();85System.err.println("Memory after setting the text: " + mem1);86});87for (int i = 0; i < 10; i++) {88System.gc();89Thread.sleep(1000);90}91mem = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();92System.err.println("Final memory after everything: " + mem);93EventQueue.invokeAndWait(frame::dispose);94}95}969798