RichTextFX
RichTextFX copied to clipboard
Question: what is the best practice for frequently changed text?
I'm working on a log text displayer: a worker thread frequently appends new log text to a file, and the displayer should display the latest log text ASAP.
For now, every time the displayer detect a change occured, it will read the log file and replace the whole text by using the replaceText
method. The code is like this:
StyleClassedTextArea outputTextArea = new StyleClassedTextArea();
outputTextArea.setWrapText(false);
outputTextArea.setEditable(false);
outputTextArea.setParagraphGraphicFactory(LineNumberFactory.get(outputTextArea));
outputTextArea.setUndoManager(null);
SimpleStringProperty stringProperty = new SimpleStringProperty("");
simpleStringProperty.addListener((observableValue, old, output) -> {
outputTextArea.replaceText(output);
outputTextArea.end(NavigationActions.SelectionPolicy.CLEAR);
outputTextArea.requestFollowCaret();
});
However, if the log changes too frequently, the memory usage will be extremely high, and the scene refresh pretty slow.
The reason that I'm using the replaceText
method is that I don't want the log text too big, so I'm maintaining a fix-size local String cache, to append new text and remove old text. So every time, I replace the whole text.
So I'm asking for help to deal with this situation. Is this RichTextFX
suitable for frequently changed text? If so, what is the proper way?
Ideally you would like to only append the text to outputTextArea that was appended to the log file and not the whole log file every time. To control the size of your displayed log you could track how many paragraphs there are after each append and remove the first few that exceed your set limit, something like:
outputTextArea.appendText( newLogLines );
int excessLines = outputTextArea.getParagraphs().size() - maxLines;
if ( excessLines > 0 ) outputTextArea.deleteText( 0, 0, excessLines, 0 );
@Jugen Thanks for the reply.
The another reason why I replace the whole text is that, there maybe several log files. Everytime the users can see only one of these files, but they may switch between these files frequently. And it seems the memory usage of the StyleClassedTextArea
is pretty high (I wonder what caused that). So I thought, maybe it's best to maintain only one StyleClassedTextArea
, and replace the log text whenever the user switch the log file.
So in your opintion, what should I do? Maintain only one StyleClassedTextArea
, or one for each log file? And is there a way to reduce the memory usage?
By what you've described I would just maintain one. The memory usage may come down if instead of replacing everything for the current log being viewed you rather use the mechanism I described above to update the text area.
I noticed that for a 44MB string, the StyleClassedTextArea
will consume more than 300MB memory, and quite some temporary memory. Is this the normal case?
If I don't need to apply any customed styles, is it possible to reduce the memory consumption?
Is this the normal case?
It seems to be ? I tried with an 8MB text file and the memory used was 200MB.
However it does feel a bit excessive though doesn't it ? I also tried the same 8MB text file using an ordinary TextArea and that used a whopping 1GB !!!
Still it seems strange, I wonder why Paragraph and ReadOnlyStyledDocument seem to need so much memory ?