Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit JTextPane memory usage

I have an application which continuously receives data on a socket, and then logs this data to a file while also displaying this data in a JTextPane. Naturally, as data is written to the underlying document of the JTextPane the memory usage continues to increase.

Is there a simple way of limiting the memory which the JTextPane is allowed use? I would like the JTextPane to work similar to how a typical command shell's command history works.

like image 824
tjansson Avatar asked Jan 05 '10 14:01

tjansson


1 Answers

just check the content and wipe it accordingly to a maximum buffer size.. since it's a JTextPane you would work on document class used by textpane:

void clampBuffer(int incomingDataSize)
{
   Document doc = textPane.getStyledDocument();
   int overLength = doc.getLength() + incomingDataSize - BUFFER_SIZE;

   if (overLength > 0)
   {
      doc.remove(0, over_length);
   }
}

This is just a snippet I wrote, didn't check it personally.. it's just to give you the idea. Of course it should be run before adding text to textPane.

Btw if you are not using the rich editor capabilities of the JTextPane I suggest you to use a JTextArea that is much ligher.

like image 145
Jack Avatar answered Oct 10 '22 23:10

Jack