Closed mattco98 closed 6 years ago
Thanks, Matthew! I was not aware of this memory leak.
@mattco98: Text-IO 3.1.4 includes your fix and is now available in Maven Central and JCenter.
I replaced hashCode()
with a new method called getStyleName()
, because there is a very small (but non-zero) probability that two different styles produce the same hashCode.
Makes sense, I didn't consider that. Certainly a more readable identifier, at the very least. Glad I could help!
When a
SwingTextTerminal
callsgetStyle(StyleData)
, it always adds a new style to it'sDocument
. After a couple thousand lines of printed text, the unnecessary references start to cause issues. This change modifies the style name to be a hashcode of the style, and only adds a new style to its document whendocument.getStyle(<hash>)
returns null.Test code (which, in a perfect world, takes 20 seconds to run):
Output before PR:
Elapsed time: 141.755707654
Output after PR:
Elapsed time: 20.540818568
Note: I'm not quite sure if this change breaks anything else, as I don't have the most knowledge of Swing. I was experiencing trouble in an application I'm developing with this library and used YourKit to analyze the program, and it led me straight to the
getStyle()
method. As soon as I implemented this change, the problem went away, and as far as I could tell, there weren't any side effects. Let me know if there's anything else I need to modify to improve this PR.