3

I'm finding that any time I set text on a JLabel with HTML, an 8KB buffer gets allocated, even though my label text may be very short. Digging in a bit, it looks like the culprit lies in DefaultStyledDocument:

public static final int BUFFER_SIZE_DEFAULT = 4096;

It looks like every time the text is changed, a property change listener fires, and a new HTMLDocument is allocated, with a GapContent of that default buffer size. 4k characters in UTF-16 gives me 8K worth of memory for a tiny little label.

As far as I can tell, I can't change change that value before the doc gets allocated. I thought reflection might come in handy, per Change private static final field using Java reflection, but it looks like I'm out of luck according to the second answer. I've thought about resizing the gap buffer after the fact, but that seems pretty tricky.

Anybody come across this before? I expect I'm going to have to get very hackity to even stand a chance...

Thanks!

Community
  • 1
  • 1
AndyI
  • 133
  • 1
  • 5

1 Answers1

1

I haven't run into this, but could you use this constructor using a GapContent with a specified initial length? The default constructor uses the default buffer size.

public DefaultStyledDocument()
{
    this(new GapContent(BUFFER_SIZE_DEFAULT), new StyleContext());
}

So just use new HTMLDocument(new GapContent(/*whatever works for you*/), new StyleContext())

dcpomero
  • 979
  • 1
  • 6
  • 17
  • Sure, though I can't figure out an easy way to substitute my own HTMLDocument in the JLabel. It seems like simply calling setText with HTML content will trigger a chain of events that creates the GapContent with the 4096 BUFFER_SIZE_DEFAULT, and generates a View from that Document. It'd be nice if there were a simple way to replace the Document with something smaller, or to tell it to truncate its GapContent! – AndyI Jun 28 '12 at 22:56