Alexander Falb created JOHNZON-293:

             Summary: Potential Memleak in DateConverter
                 Key: JOHNZON-293
             Project: Johnzon
          Issue Type: Bug
          Components: Mapper
            Reporter: Alexander Falb

 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 


I did some JMH performance tests for both (full project can be found here 
Benchmark                                         Mode  Cnt     Score     Error 
JavaTimeDateFormatterBenchmark.formatNewFormat    avgt    5   828,623 ±   8,836 
JavaTimeDateFormatterBenchmark.formatReuseFormat  avgt    5   496,916 ±   5,150 
JavaTimeDateFormatterBenchmark.parseNewFormat     avgt    5  1430,276 ±  11,084 
JavaTimeDateFormatterBenchmark.parseReuseFormat   avgt    5   990,648 ± 280,983 
SimpleDateFormatterBenchmark.formatNewFormat      avgt    5  1308,144 ±  13,993 
SimpleDateFormatterBenchmark.formatReuseFormat    avgt    5   392,236 ±   3,219 
SimpleDateFormatterBenchmark.parseNewFormat       avgt    5  1848,772 ±  19,412 
SimpleDateFormatterBenchmark.parseReuseFormat     avgt    5  1121,955 ±  12,417 
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.

This message was sent by Atlassian Jira

Reply via email to