Bug 66521

Summary: ThreadLocal memory leak in Tomcat
Product: POI Reporter: Nicola Isotta <nicola.isotta>
Component: HSSFAssignee: POI Developers List <dev>
Severity: normal    
Priority: P2    
Version: 5.2.3-FINAL   
Target Milestone: ---   
Hardware: PC   
OS: All   
Attachments: tomcat reported memory leak

Description Nicola Isotta 2023-03-08 13:50:51 UTC
Created attachment 38521 [details]
tomcat reported memory leak

We are getting memory leaks in Tomcat, and they seem to be caused by HSSFCellStyle (https://github.com/apache/poi/blob/aa9069c7df4c57088e68d884045f4066fa9ae098/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java#L123-L125)
Comment 1 Dominik Stadler 2023-03-11 06:54:20 UTC
Apache POI uses thread-locals to improve performance of some operations. 

These are not real memory-leaks, as only a fixed amount of memory is allocated and used per thread.

However Tomcat seems to perform some check on threads that are "returned to the pool" and thus warns about these ThreadLocals still holding onto some objects. 

We have now added a method org.apache.poi.util.ThreadLocalUtil#clearAllThreadLocals() which you can call to clear out all thread-locals in Apache POI before finishing work inside the Tomcat web-application.

This will be included in the next release.

See r1908263 for the implementation.

FYI, there is a similar method in XMLBeans which you may also need to call if you are using code from module poi-ooxml.
Comment 2 Dominik Stadler 2023-03-11 09:56:28 UTC
Also added a FAQ entry for this via r1908266 at https://poi.apache.org/help/faq.html
Comment 3 Nicola Isotta 2023-03-11 15:03:09 UTC