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

Memory leak in JTextPane with filling and removing elements

XMLWordPrintable

      FULL PRODUCT VERSION :


      A DESCRIPTION OF THE PROBLEM :
      Filling a JTextPane with HTMLEditorKit with simple HTML Elements and removing them at the same time keeps the app growing memory and becoming bigger and bigger.

      http://i.imgur.com/pOgvAH4.png

      REGRESSION. Last worked in version 8u141


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      /*
       * To change this license header, choose License Headers in Project Properties.
       * To change this template file, choose Tools | Templates
       * and open the template in the editor.
       */
      package javaapplication26;

      import java.io.IOException;
      import java.util.logging.Level;
      import java.util.logging.Logger;
      import javax.swing.JTextPane;
      import javax.swing.text.BadLocationException;
      import javax.swing.text.DefaultCaret;
      import javax.swing.text.Element;
      import javax.swing.text.html.HTMLDocument;
      import javax.swing.text.html.HTMLEditorKit;
      import javax.swing.text.html.StyleSheet;

      public class NewJFrame extends javax.swing.JFrame {

          private long globalCount = 0;
          
          /**
           * Creates new form NewJFrame
           */
          public NewJFrame() {
              
              initComponents();
              
              this.setSize(500, 200);
              this.setLocationRelativeTo(null);
              
              HTMLEditorKit editorKit = new HTMLEditorKit();
              this.jTextPane1.setEditorKit(editorKit);
              this.jTextPane1.setContentType("text/html");
              
              //
              StyleSheet styleSheet = editorKit.getStyleSheet();
              styleSheet.addRule("body {color:#000; font-family:times; margin: 4px; }");
              styleSheet.addRule("h1 {color: blue;}");
              styleSheet.addRule("h2 {color: #ff0000;}");
              styleSheet.addRule("div {font : 10px monaco; color : black; background-color : #fafafa; }");
              //
              
              this.jTextPane1.setText("<html><body><div id=\"GLOBALDIV\"></div></body></html>");
              
              this.jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
              this.jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
              
              DefaultCaret caret = (DefaultCaret) this.jTextPane1.getCaret();
              caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
              
              this.jScrollPane1.setAutoscrolls(false);
              this.jTextPane1.setAutoscrolls(false);
              
              Thread fillThread = new Thread() {

                  @Override
                  public void run() {

                      while (!interrupted()) {

                          try {
                              
                              removeFromPane(jTextPane1);
                              insertHTMLToPane(jTextPane1);
                              Thread.sleep(1);
                          } catch (InterruptedException ex) {
                              Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
                              break;
                          }
                      }
                  }
              };

              fillThread.start();
          }

          private void removeFromPane(JTextPane pane) {
              
              HTMLDocument doc = (HTMLDocument) pane.getDocument();
              Element element = doc.getElement("ID" + (this.globalCount - 10));

              if (element != null) {
                  doc.removeElement(element);
              }
          }
          
          private void insertHTMLToPane(JTextPane pane) {

              this.globalCount++;
              
              HTMLDocument doc = (HTMLDocument) pane.getDocument();

              Element element = doc.getElement("GLOBALDIV");
              
              if (element != null) {

                  try {
                      doc.insertBeforeEnd(element, "<div id=\"ID"+this.globalCount+"\">" + "<div> " + this.globalCount + " </div>" + "</div>");
                  } catch (BadLocationException | IOException ex) {
                      Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
                  }
              }
          }
          
          /**
           * This method is called from within the constructor to initialize the form.
           * WARNING: Do NOT modify this code. The content of this method is always
           * regenerated by the Form Editor.
           */
          @SuppressWarnings("unchecked")
          // <editor-fold defaultstate="collapsed" desc="Generated Code">
          private void initComponents() {

              jPanel1 = new javax.swing.JPanel();
              jScrollPane1 = new javax.swing.JScrollPane();
              jTextPane1 = new javax.swing.JTextPane();

              setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

              jPanel1.setLayout(new java.awt.BorderLayout());

              jScrollPane1.setViewportView(jTextPane1);

              jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);

              getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

              pack();
          }// </editor-fold>

          /**
           * @param args the command line arguments
           */
          public static void main(String args[]) {
              /* Set the Nimbus look and feel */
              //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
              /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
               * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
               */
              try {
                  for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                      if ("Nimbus".equals(info.getName())) {
                          javax.swing.UIManager.setLookAndFeel(info.getClassName());
                          break;
                      }
                  }
              } catch (ClassNotFoundException ex) {
                  java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
              } catch (InstantiationException ex) {
                  java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
              } catch (IllegalAccessException ex) {
                  java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
              } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                  java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
              }
              //</editor-fold>

              /* Create and display the form */
              java.awt.EventQueue.invokeLater(new Runnable() {
                  public void run() {
                      new NewJFrame().setVisible(true);
                  }
              });
          }

          // Variables declaration - do not modify
          private javax.swing.JPanel jPanel1;
          private javax.swing.JScrollPane jScrollPane1;
          private javax.swing.JTextPane jTextPane1;
          // End of variables declaration
      }

      ---------- END SOURCE ----------

            psadhukhan Prasanta Sadhukhan
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: