jdk/test/javax/swing/JTextArea/TextViewOOM/TextViewOOM.java
author serb
Wed, 13 May 2015 18:06:19 +0300
changeset 30930 855dbf52e45d
child 40128 e635645d2a8a
permissions -rw-r--r--
8072775: Tremendous memory usage by JTextArea Reviewed-by: vadim, prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30930
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
     1
/*
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
     4
 *
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
     8
 *
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    13
 * accompanied this code).
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    14
 *
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    18
 *
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    21
 * questions.
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    22
 */
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    23
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    24
import java.awt.EventQueue;
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    25
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    26
import javax.swing.JFrame;
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    27
import javax.swing.JScrollPane;
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    28
import javax.swing.JTextArea;
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    29
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    30
/**
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    31
 * @test
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    32
 * @bug 8072775
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    33
 * @run main/othervm -Xmx80m TextViewOOM
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    34
 */
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    35
public class TextViewOOM {
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    36
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    37
    private static JFrame frame;
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    38
    private static JTextArea ta;
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    39
    private static final String STRING = "\uDC00\uD802\uDFFF";
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    40
    private static final int N = 5000;
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    41
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    42
    private static void createAndShowGUI() {
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    43
        frame = new JFrame();
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    44
        final JScrollPane jScrollPane1 = new JScrollPane();
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    45
        ta = new JTextArea();
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    46
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    47
        ta.setEditable(false);
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    48
        ta.setColumns(20);
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    49
        ta.setRows(5);
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    50
        jScrollPane1.setViewportView(ta);
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    51
        frame.add(ta);
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    52
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    53
        frame.pack();
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    54
        frame.setLocationRelativeTo(null);
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    55
        frame.setVisible(true);
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    56
    }
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    57
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    58
    public static void main(final String[] args) throws Exception {
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    59
        /* Create and display the form */
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    60
        EventQueue.invokeAndWait(TextViewOOM::createAndShowGUI);
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    61
        for (int i = 0; i < 10; i++) {
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    62
            System.gc();
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    63
            Thread.sleep(1000);
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    64
        }
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    65
        long mem = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    66
        System.err.println("Memory before creating the text: "+mem);
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    67
        final StringBuilder sb = new StringBuilder(N * STRING.length());
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    68
        for (int i = 0; i < N; i++) {
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    69
            sb.append(STRING);
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    70
        }
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    71
        for (int i = 0; i < 10; i++) {
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    72
            System.gc();
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    73
            Thread.sleep(1000);
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    74
        }
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    75
        mem = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    76
        System.err.println("Memory after  creating the text: "+mem);
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    77
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    78
        EventQueue.invokeAndWait(() -> {
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    79
            ta.setText(sb.toString());
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    80
            for (int i = 0; i < 10; i++) {
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    81
                System.gc();
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    82
                try {Thread.sleep(200);} catch (InterruptedException iex) {}
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    83
            }
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    84
            long mem1 = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    85
            System.err.println("Memory after  setting the text: " + mem1);
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    86
        });
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    87
        for (int i = 0; i < 10; i++) {
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    88
            System.gc();
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    89
            Thread.sleep(1000);
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    90
        }
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    91
        mem = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    92
        System.err.println("Final memory  after everything: " + mem);
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    93
        EventQueue.invokeAndWait(frame::dispose);
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    94
    }
855dbf52e45d 8072775: Tremendous memory usage by JTextArea
serb
parents:
diff changeset
    95
}