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

Memory leak in HTMLEditorKit

XMLWordPrintable

    • b86
    • x86
    • generic, windows_xp

      Consider the following very simple program.

      ======================== HtmlEditorKitLeak.java ============
      import javax.swing.text.html.*;
      import javax.swing.text.*;
      import javax.swing.*;

      public class HtmlEditorKitLeak {

          public static void main(String[] args) {
              try {
                  main2();
              } catch (Throwable t) {
                  t.printStackTrace();
              }
          }
         
         private static void main2(){
            while (true) {
                JEditorPane htmlArea = new JEditorPane();
                StyleSheet style = new HTMLEditorKit().getStyleSheet();
                htmlArea.setDocument(new HTMLDocument(style));
                
                htmlArea = null;
                
                System.out.print(".");

                System.gc();
                System.runFinalization();
            }
         }
      }
      ==================================================

      This program eventually consumes all allowed memory and fails.

      Looking at the heap dump, it's clear that static field defaultStyles has
      a reference to ever-growing array of listeners, keeping all created documents in memory forever.

      Static reference from javax.swing.text.html.HTMLEditorKit.defaultStyles (from class javax.swing.text.html.HTMLEditorKit) :
      --> javax.swing.text.html.StyleSheet@0x8e8a33c8 (57 bytes) (field styles:)
      --> javax.swing.text.StyleContext$NamedStyle@0x8e8ad118 (24 bytes) (field listenerList:)
      --> javax.swing.event.EventListenerList@0x8e83e7b0 (12 bytes) (field listenerList:)
      --> [Ljava.lang.Object;@0x8e354618 (17208 bytes) (Element 2909 of [Ljava.lang.Object;@0x8e354618:)
      --> javax.swing.text.DefaultStyledDocument$StyleContextChangeHandler@0x8fcbfc10 (12 bytes) (field this$0:)
      --> javax.swing.text.html.HTMLDocument@0x8fcbd710 (88 bytes)

            gsm Sergey Groznyh (Inactive)
            vsizikov Vladimir Sizikov
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: