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