SmtpClient leaking ThreadLocal data in Tomcat

After sending emails using SmtpClient in a web application, Tomcat’s memory-leak detector finds that there are a number of ThreadLocal variables not released when the application is shut down:


The web application [ROOT] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@d21e6e4]) and a value of type [com.aspose.email.internal.ah.g] (value [com.aspose.email.internal.ah.g@40c8791f]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
The web application [ROOT] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@10ae6106]) and a value of type [com.aspose.email.internal.al.j] (value [com.aspose.email.internal.al.j@132de28d]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.

We are uncertain if there is anything we can change in our calls to SmtpClient to make this go away. We are calling the SmtpClient.dispose() method after sending the email.

We are currently using the 6.9 version of Aspose Email.

Hi Patrik,


Could you also please use the MailMessage.dispose() method to dispose the email message object itself as well? MailMessage is the only object that may be left from disposing in this case. Please try it at your end and let us know your feedback.

PS: Aspose.Email for Java 16.10.0 is available for download as the latest version of the API.

That did not solve the problem but curiously it printed this exception to stderr:


Exception in thread “java.lang.ThreadGroup[name=RMI Runtime,maxpri=10]_14” java.lang.NullPointerException
at com.aspose.email.internal.ah.n.a(Unknown Source)
at com.aspose.email.internal.ah.h.a(Unknown Source)
at com.aspose.email.internal.ah.j.run(Unknown Source)
at java.lang.Thread.run(Thread.java:744)

The email was successfully delivered though. I attempted to send another email and that did not print this exception. On stopping the application Tomcat still printed these messages:

25-Oct-2016 21:08:48.201 SEVERE [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [ROOT] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@777a98d6]) and a value of type [com.aspose.email.internal.ah.g] (value [com.aspose.email.internal.ah.g@587200be]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
25-Oct-2016 21:08:48.202 SEVERE [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [ROOT] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@777a98d6]) and a value of type [com.aspose.email.internal.ah.g] (value [com.aspose.email.internal.ah.g@763975a3]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
25-Oct-2016 21:08:48.203 SEVERE [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [ROOT] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@6d62ceca]) and a value of type [com.aspose.email.internal.al.j] (value [com.aspose.email.internal.al.j@4e7d4798]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
25-Oct-2016 21:08:48.203 SEVERE [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [ROOT] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@777a98d6]) and a value of type [com.aspose.email.internal.ah.g] (value [com.aspose.email.internal.ah.g@6c2c919]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
25-Oct-2016 21:08:48.203 SEVERE [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [ROOT] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@6d62ceca]) and a value of type [com.aspose.email.internal.al.j] (value [com.aspose.email.internal.al.j@3b6b200f]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
25-Oct-2016 21:08:48.203 SEVERE [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [ROOT] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@777a98d6]) and a value of type [com.aspose.email.internal.ah.g] (value [com.aspose.email.internal.ah.g@4084bd36]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
25-Oct-2016 21:08:48.204 SEVERE [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [ROOT] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@6d62ceca]) and a value of type [com.aspose.email.internal.al.j] (value [com.aspose.email.internal.al.j@64617339]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
25-Oct-2016 21:08:48.204 SEVERE [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [ROOT] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@777a98d6]) and a value of type [com.aspose.email.internal.ah.g] (value [com.aspose.email.internal.ah.g@747be4ad]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
25-Oct-2016 21:08:48.204 SEVERE [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [ROOT] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@6d62ceca]) and a value of type [com.aspose.email.internal.al.j] (value [com.aspose.email.internal.al.j@16f2d975]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
25-Oct-2016 21:08:48.207 SEVERE [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [ROOT] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@777a98d6]) and a value of type [com.aspose.email.internal.ah.g] (value [com.aspose.email.internal.ah.g@73faab90]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.

We will surely upgrade to the latest version, but I didn’t see anything regarding this issue in the changelog.

Hi Patrik,


We have tested this at our end and could not face any exception at our end. Could you please share the environment details as to where to check for these error messages by Tomcat? Also, is it possible for you to check this on another pc? Please also share your sample application/code with us for our consideration.

I attach a minimal JSP page that you can drop into any web application on Tomcat. WEB-INF/lib needs to have aspose-email-6.9.0.0-jdk16.jar in it.

We are using Tomcat 8 for these tests.

Step by step repro:

  • Add the JSP to any simple web app with aspose email in it
  • Update the variables in the JSP file
  • Visit http://localhost:8080/test/send-email.jsp
  • An email should be sent
  • Stop tomcat
  • Read the tomcat logs
  • Look for ThreadLocal warnings

Please note that the exception print is not our main concern. It is the memory leak we worry about.

This happens on all our machines.

Hi Patrik,


Thank you for sharing your sample JSP file.

We included this and used Gmail as SMTP server for sending out email with the latest version of the API. The email was sent out properly and no such issue was noticed in the log file of Tomcat. We have used Tomcat server on Windows 7 for testing this problem at our end. The issue seems specific to your environment. Is it possible for you to create a VM, configure Tomcat server on it and share it with us where the issue is reproducible? This will help us investigate the issue at our end for assisting you further.

We can try to provide a VM in a while, but can you please check your source code too?
The library is obfuscated, but you must have a way to determine which these classes are:

com.aspose.email.internal.al.j
com.aspose.email.internal.ah.g

In both these classes there are ThreadLocal variables that aren't cleared for the use case I included in the JSP. You absolutely have to be able to reproduce this.

I just tried running with the 16.10 version of the library and it shows the exact same behavior.

Hi Patrick,


Thank you for sharing your feedback. We have logged this issue as EMAILJAVA-34206 for further investigation by our Product team. We’ll update you here once there is some information available in this regard.

The issues you have found earlier (filed as EMAILJAVA-34206) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.