You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Bill Davidson <bi...@gmail.com> on 2010/08/30 22:50:53 UTC
iCal4j and ThreadLocal
Sigh. Using Tomcat 6.0.26 and trying to use iCal4j to generate calendar
files so that date/time events in my app can be exported to the user's
calendar.
iCal4j uses static ThreadLocal's to protect SimpleDateFormat and some
other things. When I shut down Tomcat I get these disturbing messages:
Aug 27, 2010 4:40:02 PM org.apache.catalina.loader.WebappClassLoader
clearThreadLocalMap
SEVERE: A web application created a ThreadLocal with key of type
[net.fortuna.ical4j.model.DateTime$1] (value
[net.fortuna.ical4j.model.DateTime$1@19ed13da]) and a value of type
[java.text.SimpleDateFormat] (value
[java.text.SimpleDateFormat@6f64295a]) but failed to remove it when the
web application was stopped. To prevent a memory leak, the ThreadLocal
has been forcibly removed.
Aug 27, 2010 4:40:02 PM org.apache.catalina.loader.WebappClassLoader
clearThreadLocalMap
SEVERE: A web application created a ThreadLocal with key of type
[net.fortuna.ical4j.util.CompatibilityHints$1] (value
[net.fortuna.ical4j.util.CompatibilityHints$1@1bb25a82]) and a value of
type [java.util.HashMap] (value [{}]) but failed to remove it when the
web application was stopped. To prevent a memory leak, the ThreadLocal
has been forcibly removed.
Aug 27, 2010 4:40:02 PM org.apache.catalina.loader.WebappClassLoader
clearThreadLocalMap
SEVERE: A web application created a ThreadLocal with key of type
[net.fortuna.ical4j.model.DateTime$3] (value
[net.fortuna.ical4j.model.DateTime$3@5a541b02]) and a value of type
[java.text.SimpleDateFormat] (value
[java.text.SimpleDateFormat@d70c6dda]) but failed to remove it when the
web application was stopped. To prevent a memory leak, the ThreadLocal
has been forcibly removed.
The iCal4j author assures me that this isn't really a memory leak.
It's just creating some small static variables on per thread basis.
Of course, I run up to a couple hundred threads per server so I'm not
so sure. I know I don't like seeing these messages in my log. They are
particularly annoying for development because I look at my logs a lot
and I don't like the extra useless noise.
I'm not really clear on how ThreadLocal works. Is there no way for me
to kill these?
Alternatively, if it really is harmless, is there a way to turn off
these messages?
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: iCal4j and ThreadLocal
Posted by Jess Holle <je...@ptc.com>.
Overall I think this is a rock-and-a-hard-place issue.
ThreadLocals work wonderfully behind opaque library walls in most cases.
The exception is when there are threads used with your library whose
lifecycle is beyond that of the classloader which loads your library --
as in Tomcat web apps, for instance. Working around this can force
library authors to have init() and destroy() methods for their library
to clean up ThreadLocals, which isn't at all nice for library authors
(as no try { /* stuff setting thread locals */ } finally { /* remove
thread locals */ } isn't really always a workable pattern for all
libraries).
This is a pervasive enough pain that I'm beginning to think that there
should be a web app reload mode which marks all existing threads as
"dirty", i.e. to be replaced at the end of the current request in
progress (or immediately if no request is in progress). Sure, this cuts
down on the performance of web app reloading, but it solves the problem
in its entirety without asking all library authors to rewrite their
libraries!
--
Jess Holle
On 8/30/2010 4:08 PM, Caldarale, Charles R wrote:
>> From: Bill Davidson [mailto:billdsd@gmail.com]
>> Subject: iCal4j and ThreadLocal
>> The iCal4j author assures me that this isn't really a memory leak.
> I suppose it depends on your definition of memory leak, but I'd certainly consider it one. It adds to the heap, and doesn't go away automatically when the application is undeployed (and likely prevents complete undeployment). Furthermore, when the application is redeployed, you'll get an additional set per thread, since the net.fortuna.ical4j.* classes will be loaded from a new classloader.
>
>> It's just creating some small static variables on per thread basis.
> "All I changed was..." No, it's doing a lot more than that.
>
>> I'm not really clear on how ThreadLocal works.
> http://download.oracle.com/javase/6/docs/api/java/lang/ThreadLocal.html
>
> And, as usual, GIYF.
>
>> Is there no way for me to kill these?
> Not easily. Most uses of ThreadLocal seem to be blissfully (sometimes arrogantly) unaware of thread-pooling mechanisms and app servers. Ideally, these ThreadLocal instances would instead be created in a pool for the webapp to use, rather than being tied to individual threads.
>
>> Alternatively, if it really is harmless, is there a way to
>> turn off these messages?
> It's not harmless. I think you can inhibit the checks by disabling the JreMemoryLeakPreventionListener:
>
> http://tomcat.apache.org/tomcat-6.0-doc/config/listeners.html
>
> but that pretty much means that, for safety's sake, you should restart Tomcat on every webapp deployment.
>
> - Chuck
>
>
> THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: iCal4j and ThreadLocal
Posted by Pid <pi...@pidster.com>.
On 31/08/2010 04:38, Caldarale, Charles R wrote:
>> From: Jess Holle [mailto:jessh@ptc.com]
>> Subject: Re: iCal4j and ThreadLocal
>
>> Rather replacing all the threads ASAP upon any reload
>> seems like a much more forgiving implementation.
>
> There was an effort underway to do just that a few months ago for Tomcat 7; not sure if it ever got fully implemented. Mark or Pid might respond a bit later after the sun comes up in the UK.
Sylvaint Laurent offered a patch which recreated the whole threadpool.
https://issues.apache.org/bugzilla/show_bug.cgi?id=49159
I've been playing with some ideas around the Executor but haven't got
one I'm happy with yet. It's still on my mind though.
p
> - Chuck
>
>
> THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
RE: iCal4j and ThreadLocal
Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Jess Holle [mailto:jessh@ptc.com]
> Subject: Re: iCal4j and ThreadLocal
> Rather replacing all the threads ASAP upon any reload
> seems like a much more forgiving implementation.
There was an effort underway to do just that a few months ago for Tomcat 7; not sure if it ever got fully implemented. Mark or Pid might respond a bit later after the sun comes up in the UK.
- Chuck
THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: iCal4j and ThreadLocal
Posted by Jess Holle <je...@ptc.com>.
On 8/30/2010 5:25 PM, Mark Thomas wrote:
> 'Fraid not.
>
> The right solution here is for the iCal4j library to fix the leak or
> provide a mechanism to enable clients to trigger clean-up on shutdown
> (which can then be called from a Servlet context listener). Generally,
> library authors have moved quickly to fix the leaks when reported.
> log4j, Spring Batch, commons-lang are the ones I can recall and I am
> sure there have been others.
I'm not sure asking all the authors to rework things is necessarily
right -- even if they do so quickly in most cases.
Rather replacing all the threads ASAP upon any reload seems like a much
more forgiving implementation.
--
Jess Holle
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: iCal4j and ThreadLocal
Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Bill,
On 8/31/2010 4:49 PM, Bill Davidson wrote:
> On 8/31/2010 12:16 PM, Christopher Schultz wrote:
>> Or, just remove the ThreadLocal manually. Since you know it's name, it
>> should be easy to remove. There are two obvious ways to remove these
>> ThreadLocals in a webapp:
>>
>> 1. Modify all the code that uses the iCal4j library so that, after
>> performing it's iCal4j operations, you manually remove the ThreadLocals.
>>
>
> I don't understand how to do that. I have local objects that I create from
> the library. Once I'm done with them, they go out of scope and are given
> to the garbage collector. However, the ThreadLocal's are static to the
> class, and the library offers no way to access them. Is there some way to
> find them?
>
> Hmm, now that I think about it, Tomcat found them. I guess I should
> look at the Tomcat source.
The most convenient thing would be for the library author to provide a
"cleanup" method. Tomcat uses reflective tricks to probe the ThreadLocal
map and purge it.
>> 2. Write a Filter that removes any ThreadLocals once the request has
>> been completed.
>
> Again, I'm not sure how to do that.
Once you figure out how to do the above, writing a Filter is simple:
just implement the javax.servlet.Filter interface and configure your
class in web.xml. Your "doFilter" method just includes whatever code
works above.
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAkx+aToACgkQ9CaO5/Lv0PAU8ACZAcSQ0I+ZuGMaHwNfzTP90CuX
/10AoIxYnF4xon31W8dqd8mC/ieYZEKi
=ktUb
-----END PGP SIGNATURE-----
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: iCal4j and ThreadLocal
Posted by Bill Davidson <bi...@gmail.com>.
On 8/31/2010 12:16 PM, Christopher Schultz wrote:
> Or, just remove the ThreadLocal manually. Since you know it's name, it
> should be easy to remove. There are two obvious ways to remove these
> ThreadLocals in a webapp:
>
> 1. Modify all the code that uses the iCal4j library so that, after
> performing it's iCal4j operations, you manually remove the ThreadLocals.
>
I don't understand how to do that. I have local objects that I create from
the library. Once I'm done with them, they go out of scope and are given
to the garbage collector. However, the ThreadLocal's are static to the
class, and the library offers no way to access them. Is there some way to
find them?
Hmm, now that I think about it, Tomcat found them. I guess I should
look at the Tomcat source.
> 2. Write a Filter that removes any ThreadLocals once the request has
> been completed.
>
Again, I'm not sure how to do that.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: iCal4j and ThreadLocal
Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Ognjen,
On 8/31/2010 4:51 AM, Ognjen Blagojevic wrote:
>>>> Is there no way for me to kill these?
>>>
>>> Not easily. Most uses of ThreadLocal seem to be blissfully
>>> (sometimes arrogantly) unaware of thread-pooling mechanisms and app
>>> servers. Ideally, these ThreadLocal instances would instead be
>>> created in a pool for the webapp to use, rather than being tied to
>>> individual threads.
>>
>> 6.0.26 removes those by default. 6.0.29, removing them is optional - see
>> the clearReferencesThreadLocals attribute on the context.
>>
>> The only way to get rid of the messages is to fix the memory leak.
>
> Other solution is to create a new thread by yourself and delegate the
> iCal4j work to it.
Or, just remove the ThreadLocal manually. Since you know it's name, it
should be easy to remove. There are two obvious ways to remove these
ThreadLocals in a webapp:
1. Modify all the code that uses the iCal4j library so that, after
performing it's iCal4j operations, you manually remove the ThreadLocals.
2. Write a Filter that removes any ThreadLocals once the request has
been completed.
I would favor the latter for safety's sake, but the former seems cleaner
to me.
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAkx9VPwACgkQ9CaO5/Lv0PBpyQCgxFzber2gATsTTRF8vqTddI2p
JG4AoJNXutzxkHu1z9pVSkotBs7tJuGa
=hJOz
-----END PGP SIGNATURE-----
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: iCal4j and ThreadLocal
Posted by Pid <pi...@pidster.com>.
On 31/08/2010 11:30, Jess Holle wrote:
> On 8/31/2010 4:03 AM, Leon Rosenberg wrote:
>> On Tue, Aug 31, 2010 at 10:51 AM, Ognjen Blagojevic
>> <og...@gmail.com> wrote:
>>>>>> Is there no way for me to kill these?
>>>>> Not easily. Most uses of ThreadLocal seem to be blissfully (sometimes
>>>>> arrogantly) unaware of thread-pooling mechanisms and app servers.
>>>>> Ideally,
>>>>> these ThreadLocal instances would instead be created in a pool for the
>>>>> webapp to use, rather than being tied to individual threads.
>>>> 6.0.26 removes those by default. 6.0.29, removing them is optional -
>>>> see
>>>> the clearReferencesThreadLocals attribute on the context.
>>>>
>>>> The only way to get rid of the messages is to fix the memory leak.
>>> Other solution is to create a new thread by yourself and delegate the
>>> iCal4j
>>> work to it. Once the thread is finished, ThreadLocal variables are also
>>> released. We managed to work around similar leak in older JasperReports
>>> library like that.
>>>
>>> Be aware, however, that creation of threads takes some resources, and
>>> that
>>> you should take necessary steps to clean up those threads in all
>>> cases (when
>>> exception is thrown in the thread, or when the server shuts down
>>> while your
>>> thread is still active).
>> The more I think about the issue, the more I feel that this is a bug
>> in the container which defines two different lifecycles one for
>>
>> webapps and one for threads which use the webapps, nd, in case of
>> ThreadLocal, contain the webapp code. As a library developer I can not
>> be aware that I'm running in a container and how this particular
>> container defines its and my lifecycles. Providing a
>> ServletContextListener for each container along with my small logging
>> library (example) seems a little overkill.
I disagree. It doesn't matter whether you're in a container or not, you
should always clean up after yourself.
>> The cleanest solution would be to either have an executor service per
>> webapp (which will be tricky to become effective) or to replace all
>> threads in the thread pool
>> on redeploy. Correct me if I'm wrong, but hot-redeploy isn't meant as
>> production system feature anyway (at least it wasn't time ago), and
>> the performance impact for
>> thread pool renewal on a staging system is negligible.
> While I think it would be *nice* of a container to replace threads on a
> web app redeploy/restart, I wouldn't go so far as to say that not doing
> so is a bug. Do OSGi containers raise similar issues with module
> redeployment? I'd guess so -- and guess that replacing threads might be
> harder in such cases.
>
> That said, replacing threads in a web app container seems fairly
> straightforward and seems like a more friendly approach than telling
> library authors to rework their code or trying to remove thread locals
> in a non-thread-safe fashion.
Only with an Executor is it possible to remove threads from the pool,
in 6+ AFAIK.
The Executor does not currently listen to events from the Context, ie
redeployment.
The solution I was/am exploring is to compare creation times of threads
and webapps, and replace the thread if it's older than the webapp,
rather than recreate the entire ThreadPool in one go.
This could occur incrementally during usage, or via a pool sweeper.
p
> --
> Jess Holle
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
Re: iCal4j and ThreadLocal
Posted by Jess Holle <je...@ptc.com>.
On 8/31/2010 4:03 AM, Leon Rosenberg wrote:
> On Tue, Aug 31, 2010 at 10:51 AM, Ognjen Blagojevic
> <og...@gmail.com> wrote:
>>>>> Is there no way for me to kill these?
>>>> Not easily. Most uses of ThreadLocal seem to be blissfully (sometimes
>>>> arrogantly) unaware of thread-pooling mechanisms and app servers. Ideally,
>>>> these ThreadLocal instances would instead be created in a pool for the
>>>> webapp to use, rather than being tied to individual threads.
>>> 6.0.26 removes those by default. 6.0.29, removing them is optional - see
>>> the clearReferencesThreadLocals attribute on the context.
>>>
>>> The only way to get rid of the messages is to fix the memory leak.
>> Other solution is to create a new thread by yourself and delegate the iCal4j
>> work to it. Once the thread is finished, ThreadLocal variables are also
>> released. We managed to work around similar leak in older JasperReports
>> library like that.
>>
>> Be aware, however, that creation of threads takes some resources, and that
>> you should take necessary steps to clean up those threads in all cases (when
>> exception is thrown in the thread, or when the server shuts down while your
>> thread is still active).
> The more I think about the issue, the more I feel that this is a bug
> in the container which defines two different lifecycles one for
> webapps and one for threads which use the webapps, and, in case of
> ThreadLocal, contain the webapp code. As a library developer I can not
> be aware that I'm running in a container and how this particular
> container defines its and my lifecycles. Providing a
> ServletContextListener for each container along with my small logging
> library (example) seems a little overkill.
>
> The cleanest solution would be to either have an executor service per
> webapp (which will be tricky to become effective) or to replace all
> threads in the thread pool
> on redeploy. Correct me if I'm wrong, but hot-redeploy isn't meant as
> production system feature anyway (at least it wasn't time ago), and
> the performance impact for
> thread pool renewal on a staging system is negligible.
While I think it would be *nice* of a container to replace threads on a
web app redeploy/restart, I wouldn't go so far as to say that not doing
so is a bug. Do OSGi containers raise similar issues with module
redeployment? I'd guess so -- and guess that replacing threads might be
harder in such cases.
That said, replacing threads in a web app container seems fairly
straightforward and seems like a more friendly approach than telling
library authors to rework their code or trying to remove thread locals
in a non-thread-safe fashion.
--
Jess Holle
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: iCal4j and ThreadLocal
Posted by Leon Rosenberg <ro...@gmail.com>.
On Tue, Aug 31, 2010 at 10:51 AM, Ognjen Blagojevic
<og...@gmail.com> wrote:
>>>> Is there no way for me to kill these?
>>>
>>> Not easily. Most uses of ThreadLocal seem to be blissfully (sometimes
>>> arrogantly) unaware of thread-pooling mechanisms and app servers. Ideally,
>>> these ThreadLocal instances would instead be created in a pool for the
>>> webapp to use, rather than being tied to individual threads.
>>
>> 6.0.26 removes those by default. 6.0.29, removing them is optional - see
>> the clearReferencesThreadLocals attribute on the context.
>>
>> The only way to get rid of the messages is to fix the memory leak.
>
> Other solution is to create a new thread by yourself and delegate the iCal4j
> work to it. Once the thread is finished, ThreadLocal variables are also
> released. We managed to work around similar leak in older JasperReports
> library like that.
>
> Be aware, however, that creation of threads takes some resources, and that
> you should take necessary steps to clean up those threads in all cases (when
> exception is thrown in the thread, or when the server shuts down while your
> thread is still active).
The more I think about the issue, the more I feel that this is a bug
in the container which defines two different lifecycles one for
webapps and one for threads which use the webapps, and, in case of
ThreadLocal, contain the webapp code. As a library developer I can not
be aware that I'm running in a container and how this particular
container defines its and my lifecycles. Providing a
ServletContextListener for each container along with my small logging
library (example) seems a little overkill.
The cleanest solution would be to either have an executor service per
webapp (which will be tricky to become effective) or to replace all
threads in the thread pool
on redeploy. Correct me if I'm wrong, but hot-redeploy isn't meant as
production system feature anyway (at least it wasn't time ago), and
the performance impact for
thread pool renewal on a staging system is negligible.
2 cents.
regards
Leon
>
> Regards,
> Ognjen
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: iCal4j and ThreadLocal
Posted by Ognjen Blagojevic <og...@gmail.com>.
>>> Is there no way for me to kill these?
>>
>> Not easily. Most uses of ThreadLocal seem to be blissfully (sometimes arrogantly) unaware of thread-pooling mechanisms and app servers. Ideally, these ThreadLocal instances would instead be created in a pool for the webapp to use, rather than being tied to individual threads.
>
> 6.0.26 removes those by default. 6.0.29, removing them is optional - see
> the clearReferencesThreadLocals attribute on the context.
>
> The only way to get rid of the messages is to fix the memory leak.
Other solution is to create a new thread by yourself and delegate the
iCal4j work to it. Once the thread is finished, ThreadLocal variables
are also released. We managed to work around similar leak in older
JasperReports library like that.
Be aware, however, that creation of threads takes some resources, and
that you should take necessary steps to clean up those threads in all
cases (when exception is thrown in the thread, or when the server shuts
down while your thread is still active).
Regards,
Ognjen
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: iCal4j and ThreadLocal
Posted by Mark Thomas <ma...@apache.org>.
On 30/08/2010 22:08, Caldarale, Charles R wrote:
>> From: Bill Davidson [mailto:billdsd@gmail.com]
>> The iCal4j author assures me that this isn't really a memory leak.
>
> I suppose it depends on your definition of memory leak, but I'd certainly consider it one. It adds to the heap, and doesn't go away automatically when the application is undeployed (and likely prevents complete undeployment). Furthermore, when the application is redeployed, you'll get an additional set per thread, since the net.fortuna.ical4j.* classes will be loaded from a new classloader.
It is going to pin every class loaded by your web application in memory
when the app is reloaded. By pretty much any definition of the term,
that is a memory leak in PermGen.
>> Is there no way for me to kill these?
>
> Not easily. Most uses of ThreadLocal seem to be blissfully (sometimes arrogantly) unaware of thread-pooling mechanisms and app servers. Ideally, these ThreadLocal instances would instead be created in a pool for the webapp to use, rather than being tied to individual threads.
6.0.26 removes those by default. 6.0.29, removing them is optional - see
the clearReferencesThreadLocals attribute on the context.
The only way to get rid of the messages is to fix the memory leak.
>> Alternatively, if it really is harmless, is there a way to
>> turn off these messages?
>
> It's not harmless. I think you can inhibit the checks by disabling the JreMemoryLeakPreventionListener:
'Fraid not.
The right solution here is for the iCal4j library to fix the leak or
provide a mechanism to enable clients to trigger clean-up on shutdown
(which can then be called from a Servlet context listener). Generally,
library authors have moved quickly to fix the leaks when reported.
log4j, Spring Batch, commons-lang are the ones I can recall and I am
sure there have been others.
Mark
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: iCal4j and ThreadLocal
Posted by Jess Holle <je...@ptc.com>.
On 8/31/2010 1:21 PM, Caldarale, Charles R wrote:
>> It seems to me that using static ThreadLocal's isn't going to
>> save that much overhead vs. just creating a regular local object
>> each time you run.
> The above is even more true in modern JVMs with method inlining and escape analysis - such an object might not even be created on the heap, just on the stack and immediately discarded when the method creating it returns.
There are plenty of other use cases for thread locals -- and there are
cases where it is not desirable to do the try { /* add and use thread
local */ } finally { /* remove thread local */ } pattern. To
interoperate with a container like Tomcat one then has to provide init()
and destroy() methods for one's library -- and provide these methods
with knowledge of all thread locals throughout one's entire library. Yuck!
That said, I can see that Java's ThreadLocal implementation gives some
containers little to no choice. Thus a really good library should
provide such hooks. Tomcat clearly has a choice, however, of replacing
threads in a pool as they become idle -- and thus as a really good
container it should be so nice :-)
--
Jess Holle
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
RE: iCal4j and ThreadLocal
Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Bill Davidson [mailto:billdsd@gmail.com]
> Subject: Re: iCal4j and ThreadLocal
> It seems to me that using static ThreadLocal's isn't going to
> save that much overhead vs. just creating a regular local object
> each time you run.
The above is even more true in modern JVMs with method inlining and escape analysis - such an object might not even be created on the heap, just on the stack and immediately discarded when the method creating it returns.
- Chuck
THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: iCal4j and ThreadLocal
Posted by Bill Davidson <bi...@gmail.com>.
On 8/30/2010 9:18 PM, Caldarale, Charles R wrote:
> There's a lot of baggage implemented to support ThreadLocal. It's one of those deceptively easy-to-use Java concepts that utilizes a lot of plumbing underneath the covers (e.g., a specialized per-thread expandable hash map, weak references). The basic implementation is that of a hash map per thread, using the ThreadLocal object as the key, and the reference from the argument to ThreadLocal.set() as the value. There's no actual requirement that the values are in fact thread-local, but they usually are so that synchronization can be avoided.
>
There needs to be a balance between caching/pooling and abuse
of memory. Caching/pooling needs to be worth the trouble of
doing it and the memory it takes up.
For example in iCal4j with SimpleDateFormat, I don't really see why
it's so important to have it static. It seems to me that using static
ThreadLocal's isn't going to save that much overhead vs. just creating
a regular local object each time you run. It seems to me that things
like that should be reserved for something that's more expensive to
create, like something that requires doing I/O. A SimpleDateFormat
doesn't require any I/O to be created. Its format string might come
from a ResourceBundle but I believe those only do I/O the first time
they are loaded.
I do some static caching in my app but it's of data pulled from a database
on another host, making it expensive to create. It's also data that gets
used frequently but changes rarely. Synchronization around a HashMap
or Hashtable is cheaper than hitting the database.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
RE: iCal4j and ThreadLocal
Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Bill Davidson [mailto:billdsd@gmail.com]
> Subject: Re: iCal4j and ThreadLocal
> It still felt a bit fuzzy to me. It's getting a little more
> in focus but it's kind of a weird thing.
There's a lot of baggage implemented to support ThreadLocal. It's one of those deceptively easy-to-use Java concepts that utilizes a lot of plumbing underneath the covers (e.g., a specialized per-thread expandable hash map, weak references). The basic implementation is that of a hash map per thread, using the ThreadLocal object as the key, and the reference from the argument to ThreadLocal.set() as the value. There's no actual requirement that the values are in fact thread-local, but they usually are so that synchronization can be avoided.
Then there's InheritableThreadLocal, which explicitly shares nominally thread-local values across threads...
- Chuck
THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: iCal4j and ThreadLocal
Posted by Bill Davidson <bi...@gmail.com>.
On 8/30/2010 2:08 PM, Caldarale, Charles R wrote:
>> I'm not really clear on how ThreadLocal works.
>>
> http://download.oracle.com/javase/6/docs/api/java/lang/ThreadLocal.html
>
> And, as usual, GIYF.
>
I actually had already read that, and a few other things I found in Google.
It still felt a bit fuzzy to me. It's getting a little more in focus
but it's kind
of a weird thing.
Thanks for the response. I've forwarded that information and Mark's to the
author.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
RE: iCal4j and ThreadLocal
Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Bill Davidson [mailto:billdsd@gmail.com]
> Subject: iCal4j and ThreadLocal
> The iCal4j author assures me that this isn't really a memory leak.
I suppose it depends on your definition of memory leak, but I'd certainly consider it one. It adds to the heap, and doesn't go away automatically when the application is undeployed (and likely prevents complete undeployment). Furthermore, when the application is redeployed, you'll get an additional set per thread, since the net.fortuna.ical4j.* classes will be loaded from a new classloader.
> It's just creating some small static variables on per thread basis.
"All I changed was..." No, it's doing a lot more than that.
> I'm not really clear on how ThreadLocal works.
http://download.oracle.com/javase/6/docs/api/java/lang/ThreadLocal.html
And, as usual, GIYF.
> Is there no way for me to kill these?
Not easily. Most uses of ThreadLocal seem to be blissfully (sometimes arrogantly) unaware of thread-pooling mechanisms and app servers. Ideally, these ThreadLocal instances would instead be created in a pool for the webapp to use, rather than being tied to individual threads.
> Alternatively, if it really is harmless, is there a way to
> turn off these messages?
It's not harmless. I think you can inhibit the checks by disabling the JreMemoryLeakPreventionListener:
http://tomcat.apache.org/tomcat-6.0-doc/config/listeners.html
but that pretty much means that, for safety's sake, you should restart Tomcat on every webapp deployment.
- Chuck
THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org