3

I'm using a lot of SimpleDateFormat-objects within my Servlet. Unfortunately, SimpleDateFormat is not thread-safe. Thus, I thought about wrapping it wih ThreadLocal to foster the reuse of SimpleDateFormat-objects. I wrote an util-class to enable this:

public class DateUtil {
    private final static ThreadLocal<SimpleDateFormat> dateFormat = new ThreadLocal<SimpleDateFormat>() {
        return new SimpleDateFormat();
    }

    public static SimpleDateFormat get () {
        return dateFormat.get();
    }
}

Actually, this seems to lead to a memory-leak. When shutting down my webapp, Tomcat logs the following error message:

SEVERE: The web application [] created a ThreadLocal with key of type [null] (value [com.example.util.DateUtil$2@50242f7d]) and a value of type [java.text.SimpleDateFormat] (value [java.text.SimpleDateFormat@d91b489b]) but failed to remove it when the web application was stopped. This is very likely to create a memory leak.

I understand the reason for the memory-leak, but what is the best way to handle SimpleDateFormat-objects (or any other non-thread-safe objects) within Servlets?

Nick Bastin
  • 30,415
  • 7
  • 59
  • 78
Bob
  • 5,510
  • 9
  • 48
  • 80
  • I understand I am not the one supposed to be asking a question in a commnet, but what is the reason for memory leak? Thanks in advance to you. – Victor Jan 10 '11 at 22:19
  • The reason is that an instance SimpleDateFormat will be held against each request thread that happens to run this code, at no point will that instance be removed. – Gareth Davis Jan 10 '11 at 22:31
  • 10 years late on this, but won't the instance be removed when the webapp stops and the class is unloaded? Given it's a final static field – Siddhartha Jun 07 '20 at 00:04

2 Answers2

5

Aside from using an alternative implementation (commons-lang or joda) just create a new instance of SimpleDateFormat each time you what to use it.

I realise that this will make you feel dirty and in need of a bath, but it is very simple and doesn't require any effort on your part. The downside is that you will turn over a little bit more memory than before but in most normal web applications you are unlikely to notice against the noise of JDBC.

See my answer to ThreadLocal Resource Leak and WeakReference

Community
  • 1
  • 1
Gareth Davis
  • 27,701
  • 12
  • 73
  • 106
  • 1
    It should be mentioned, that `SimpleDateFormat` is expensive to create (it creates a `Calendar` instance internally). See: http://stackoverflow.com/questions/4107839/synchronizing-access-to-simpledateformat – Joe23 Jun 15 '11 at 09:33
  • 3
    expensive is a relative term, if like the user in http://www.thedwick.com/blog/2008/04/simpledateformat-performance-pig/ you are seemly doing nothing but parsing dates then it could be expensive, but if you are putting 20 dates on a html page after running a jdbc/hibernate query it really isn't going to be that important. Make sure your program is correct *first* then make it fast. – Gareth Davis Jun 15 '11 at 09:54
3

Create local objects or use FastDateFormat (FastDateFormat is a fast and thread-safe version of SimpleDateFormat.) from commons-lang. And joda-time is a common answer to all the date related questions ;-)

Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327