At 12:37 12.06.2001 -0500, Wes wrote:
>We just began stress testing our application, and found memory leaks.  Eventually, I 
>isolated the leak to the NDC push/pop that we were doing in each thread that was 
>created.  Then, after looking more carefully at the NDC class documentation, I saw 
>the following regarding the remove method:
> 
>"Remove the diagnostic context for this thread. 
>
>Each thread that created a diagnostic context by calling 
><file:///C:/wes/jakarta-log4j-1.1.2/docs/api/org/apache/log4j/NDC.html#pop()>pop() 
>should call this method before exiting. Otherwise, the memory used by the diagnostic 
>context for the thread cannot be reclaimed by the VM. 
>As this is such an important problem in heavy duty systems and because it is 
>difficult to always guarantee that the remove method is called before exiting a 
>thread, this method has been augmented to lazily remove references to dead threads. 
>In practice, this means that you can be a little sloppy and occasionally forget to 
>call 
><file:///C:/wes/jakarta-log4j-1.1.2/docs/api/org/apache/log4j/NDC.html#remove()>remove()
> before exiting a thread"
> 
>I had not known about the remove method, probably because I initially looked only at 
>a few examples, such as Trivial.java, and the remove was not used there.  I also 
>noticed that the documentation for the push and pop methods does not mention the 
>remove() method at all.
> 
>Furthermore, according to the remove() documentation, we should be OK anyway because 
>of the lazy cleanup mechanism, but according to our tests, it did not clean up 
>enough, as we eventually ran out of memory.

Wes, 

Lazy removal mechanism is invoked by the remove method. If you never invoke 
NDC>remove(), then lazy removal is never activated. You probably never invoked remove 
in your stress test, did you? Lazy removal is based on a probabilistic algorithm. The 
more dead threads you have the faster the algorithm will remove them. 

>So, if anyone else is currently doing push/pop in threads that come and go 
>frequently, be sure to call remove as the thread exits... and perhaps the remove 
>method could be mentioned in the documentation for push and pop.

That's a suggestion. However, there is no guarantee that you would have looked at the 
documentation of the remove method even if was mentioned in push() and pop(). Is 
there? Anyway, I'll add additional warnings. Thanks for sharing your experience with 
the rest of us. Cheers, Ceki


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to