Affects Version/s: None
Fix Version/s: None
DateConverter creates a new SimpleDateFormat for every instance for every thread the instance is used on and never cleans them up. This may cause a high memory usage, if lots of those converters are created instead of reused.
Two approaches to get rid of them are on my mind:
- Create a new SimpleDateFormat within the toString and fromString methods as method variable instead of a class field.
- Use java.time.DateTimeFormatter as class field, because it is immutable and thread-safe, and do some object conversion from java.util.Date to/from java.time.ZonedDateTime.
I did some JMH performance tests for both (full project can be found here https://github.com/elexx/dateconverter-benchmark):
In this quick test it looks like creating a new SimpleDateFormatter in each method is quite slow (1308ns/op + 1848ns/op).
Reusing the SimpleDateFormatter is faster (392ns/op + 1121ns/op), but no option because it is not thread-safe.
Reusing the Java8-DateTimeFormatter is equivalent (496ns/op + 990ns/op) to Reusing SimpleDateFormatter (parsing is faster, formatting is slower, avg is about the same)
And just for completeness: Creating a Java8-DateTimeFormatter, which is nonsense, because it is immutable and thread-safe.