Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: what is the best practice for frequently changed text? #1203

Open
zkycaesar opened this issue Oct 30, 2023 · 5 comments
Open

Question: what is the best practice for frequently changed text? #1203

zkycaesar opened this issue Oct 30, 2023 · 5 comments

Comments

@zkycaesar
Copy link

zkycaesar commented Oct 30, 2023

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?

@Jugen
Copy link
Collaborator

Jugen commented Oct 30, 2023

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 );

@zkycaesar
Copy link
Author

@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?

@Jugen
Copy link
Collaborator

Jugen commented Oct 30, 2023

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.

@zkycaesar
Copy link
Author

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?

image

If I don't need to apply any customed styles, is it possible to reduce the memory consumption?

@Jugen
Copy link
Collaborator

Jugen commented Oct 31, 2023

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 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants