Memory Leak in ThreadLocal Storage When reusing threads

Request for help on DynamicReports

Moderator: ricardo

Memory Leak in ThreadLocal Storage When reusing threads

Postby Wallyk » Tue Mar 14, 2017 2:29 pm

We are using dynamic reports and we are facing a similar problem like

In our case we are using a compute engine that has a thread pool for execution of tasks, we generate reports using Dynamic reports on top of this threadpool which implies the same threads are being reused.
The compute engine heap size is growing with time and eventually crashing and i have found in the heap dump a huge amount of retained information in the java.lang.ThreadLocal$ThreadLocalMap$Entry[] where most entries in this map are from:
- class: java.lang.ThreadLocal$ThreadLocalMap$Entry[], value: java.lang.ThreadLocal$ThreadLocalMap$Entry #15795
-> value - class: java.lang.ThreadLocal$ThreadLocalMap$Entry, value: net.sf.dynamicreports.jasper.base.JasperScriptlet #13897

It appears that dynamic reports is adding information to ThreadLocal without removing them at the end of execution and since thread local holds weak references and we are reusing the same threads they remain in memory and GC will not collect them causing a crash eventually.
We have recently upgraded to Dynamic Reports 5.0.0 which appears to be causing the problem daily, (we used to face the same problem weekly on 4.1.1 but i cannot confirm its the same problem), we will try for now to revert back to 4.1.1 to see if it resolves the problem.
Unfortunately I cannot clean the ThreadLocal myself as in the above discussion since I do not have access to the execution thread, Are there any plans for fixing this issue?
Posts: 2
Joined: Sun Jul 05, 2015 11:09 am

Re: Memory Leak in ThreadLocal Storage When reusing threads

Postby EduFrazao » Tue Aug 01, 2017 1:12 pm

Hi. I'm facing this problem too, but in my case, it occours almost imediatelly when generating a large report (with a datasource of more or less 220MB of data, from a jdbc resultset).
I've tried to use a disk ReportVirtualizer, but it does not solve the problem.

This occours in a WebApplication, and it creates large objects holded in Tomcat Threads.

I've attached a memory dump image

There is any workaround?
Memory dump image
memory.png (54.5 KiB) Viewed 614 times
Posts: 3
Joined: Mon Feb 16, 2015 1:31 am

Re: Memory Leak in ThreadLocal Storage When reusing threads

Postby joker » Wed Oct 11, 2017 3:24 pm

I experience the exact same problem.

I saw that in the class JasperCustomValues (Line 139) the ThreadLocal "localJasperScript" is set (see below). This thread local is not removed anymore. As in my case the thread will be reused later on the memory leaks so that when running multiple reports on the same thread the vm runs out of memory.

Code: Select all
protected void setJasperScriptlet(JasperScriptlet jasperScriptlet) {

Is there any workaround? I assess this bug as critical.
Posts: 1
Joined: Wed Oct 11, 2017 3:20 pm

Re: Memory Leak in ThreadLocal Storage When reusing threads

Postby enricog84 » Fri Nov 10, 2017 1:01 pm

I am also experiencing this issue, is there anything we can do to help fixing it?

I was able to apply the workaround from the linked thread in OP by reimplementing:

- net.sf.dynamicreports.jasper.builder.JasperReportBuilder
---- Did override the "toJasperReportDesign" method to return my custom JasperReportDesign
- net.sf.dynamicreports.jasper.base.JasperReportDesign
---- Did replace the JasperCustomValues with my implementation
- net.sf.dynamicreports.jasper.base.JasperCustomValues
---- Added the method as in OP link to remove it from the Thread

Tough this really doesn't feel like clean solution. Was actually just trying to check if it would solve the issue, which it did.

Edit: Using the method from here this stackoverflow post:
also seem to solve it. Tough I have absolutely no idea what possible side effects this could have.
Posts: 1
Joined: Fri Jun 10, 2016 8:05 am

Return to Help

Who is online

Users browsing this forum: No registered users and 1 guest