Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4203912

JTextArea uses too much memory (if large byte array passed to cto=

    • b28
    • generic, x86
    • generic, windows_nt, windows_2000



      Name: krT82822 Date: 01/18/99

      =20
      While writing a simple file viewing class, I
      encountered a problem regarding the necessary
      memory. For example, if I do something like ...

      //---------------------------------------------
      // 'sb' is a byte array with some text
      // read from a file
      //
          JTextArea ta=3Dnew JTextArea(new String(sb));=20
      //
      // OR !
      //
          JTextArea ta=3Dnew JTextArea();
          ta.setText(new String(sb));
      //
      //---------------------------------------------

      the amount of memory needed is incredible.

      Let=B4s say, the size of 'sb' is 2 MB, then the
      memory allocated for the JTextArea instance is
      something like 25,8 MB. This memory usage is
      almost linear, a text twice the size (4 MB)
      causes 51,4 MB to be allocated.=20

      The same happens, if I use a JEditorPane instead
      of a JTextArea.

      I consider this to be a bug, because this amount
      of memory is just too much to be reasonable,
      considering that only plain text is displayed.

      I experience this behaviour in JDK 1.1.5 with
      Swing 1.0.2, JDK 1.1.7 and 1.2RC1.
      (Review ID: 43400)
      ======================================================================

      Name: skT88420 Date: 05/27/99


      Swing Text Components(JTextArea, JTextField...) seem to leak
      memory as measured by WindowsNT Task Manager Memory Usage
      History when given focus. Leak does not occur when focus is lost
      or when their AWT counterparts are used, as demonstrated in the
      following code:

      import java.awt.*;
      import javax.swing.*;

      public class SwingTextMemoryTest extends JFrame{

      private JPanel panel;
      private JTextField jTextField;
      private TextField textField;

      public SwingTextMemoryTest()
      {
      super();
      panel = new JPanel();
                       jTextField = new JTextField("JTextField",15);
      textField = new TextField("AWT TextField",15);
      panel.add(textField); // Stable with focus
      panel.add(jTextField); // Leaks when given focus
      this.getContentPane().add(panel);
      setSize(500,500);
      setVisible(true);
      }

      public static void main(String[] args)
      { SwingTextMemoryTest test = new SwingTextMemoryTest(); }

      }

      Thank you for your attention.

      - Mark Horwath
      (Review ID: 83582)
      ======================================================================

      Name: tb29552 Date: 12/24/99


      java version "1.2.2"
      Classic VM (build JDK-1.2.2-W, native threads, symcjit)


      When calling setText() on a JTextArea, * with a huge string as the argument * (
      say 2.6 MB ), the memory usage shoots up by atleast 30-35 MB.

      The code I enclosed :- There is a JFrame, with a JPanel containing a
      JTextArea ( enclosed in a JScrollPane ) as the contentpane. A file name is
      specified as the command line argument. The program opens the specified file,
      reads its contents, and calls setText() on the JTextArea. When the input file
      is small ( say around 1 MB ), there is no problem, but when I gave a input file
      of size 2.6 MB, the memory usage went up to 45 MB. This happened on JDK1.2.2
      both in Windows NT and Solaris. ( I have 128 MB RAM in my machine ).


      Code: ////////////////////////////////////////////////////////////////////////

      import java.awt.* ;
      import java.awt.event.* ;

      import javax.swing.* ;
      import javax.swing.event.* ;

      import java.io.* ;

      class JTextAreaTest {
      JFrame f = new JFrame("Test") ;
      JTextArea ta = new JTextArea(30,30) ;
      JPanel mainPanel = new JPanel() ;
      String m_fileName ;
      StringBuffer contents = new StringBuffer() ;

      JTextAreaTest(String fileName) {
      m_fileName = fileName ;
      setupTextArea() ;
      JScrollPane tempPanel = new JScrollPane(ta) ;
      f.getContentPane().add(tempPanel);

      f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
      f.dispose() ;
      }
      });

      f.pack() ;
      f.show() ;
      }


      private void setupTextArea() {
      BufferedReader in = null ;
      try {
      in = new BufferedReader(new FileReader(m_fileName));
      }
      catch (FileNotFoundException e) {
      e.printStackTrace() ;
      return ;
      }

      String curLine = null ;

      while (true) {
      try {
      curLine = in.readLine() ;

      }
      catch (IOException e) {
      e.printStackTrace() ;
      }

      if (curLine == null) {
      break ;
      }
      contents.append(curLine);
      contents.append("\n");
      }
      try {
      in.close();
      }
      catch (IOException e) {
      e.printStackTrace() ;
      }

      ta.setText(contents.toString());
      }

      public static void main(String[] args) {
      if ( args.length != 1) {
      System.out.println("Usage: java JTextAreaTest
      <fileName>" );
      System.exit(1);
      }
      JTextAreaTest taTest = new JTextAreaTest(args[0]) ;
      }
      }

      /////////////////////////////////////////////////////////////////////////
      (Review ID: 99331)
      ======================================================================

            naasunw Naa Naa (Inactive)
            kryansunw Kevin Ryan (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: