Bug 66521 - ThreadLocal memory leak in Tomcat
Summary: ThreadLocal memory leak in Tomcat
Alias: None
Product: POI
Classification: Unclassified
Component: HSSF (show other bugs)
Version: 5.2.3-FINAL
Hardware: PC All
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
Depends on:
Reported: 2023-03-08 13:50 UTC by Nicola Isotta
Modified: 2023-03-11 15:03 UTC (History)
0 users

tomcat reported memory leak (150.19 KB, image/jpeg)
2023-03-08 13:50 UTC, Nicola Isotta

Note You need to log in before you can comment on or make changes to this bug.
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