[ 
https://issues.apache.org/jira/browse/GROOVY-7683?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15030280#comment-15030280
 ] 

John Wagenleitner commented on GROOVY-7683:
-------------------------------------------

{quote}there are always 20000 different script classes loaded (Script1 to 
Script20000), even though there's only 10 variations of the script. Is this the 
expected behavior?{quote}

Since the test project creates 20,000 engines and since the script class cache 
is per engine this will create 20k classes.  This also creates lots of 
classloader instances and global cached methods.  In this particular case it 
seems like creating a single engine with a logger binding and then using the 
{{context}} bean parameter to pass the {{taskId}} and receive the {{result}} 
would be the way to go.  I realize the test project may have crafted to show 
the leak and not a real world example.  A global class cache would help here, 
but I'm not sure if there are reasons (i.e., classloader) to keep it per 
instance.  I'm not that familiar with JSR-223 so may be misunderstanding the 
use-case here.

As for the large number of uncollected {{MetaMethodIndex$Entry}} instances I 
wonder if this might be similar to the issue reported in GROOVY-7083, since the 
[script metaclass is 
modified|https://github.com/jwagenleitner/incubator-groovy/blob/aa5fa948de5222abc570d9e1c41dcea6799964cf/subprojects/groovy-jsr223/src/main/java/org/codehaus/groovy/jsr223/GroovyScriptEngineImpl.java#L317]
 and this might be why the classes are not unloaded.

> Memory leak when using Groovy as JSR-223 scripting language.
> ------------------------------------------------------------
>
>                 Key: GROOVY-7683
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7683
>             Project: Groovy
>          Issue Type: Bug
>          Components: GroovyScriptEngine
>    Affects Versions: 2.4.5
>         Environment: OS: tested on Mac OS X El Capitan and Windows 10.
> JVM: tested on 1.8.0_60 and 1.8.0_65.
>            Reporter: Arkadiusz Gasinski
>              Labels: jsr-223
>
> We have a Java EE 7 web application in production that when handling single 
> HTTP request can load and execute up to several Groovy scripts using the 
> jsr-223 API. This application is deployed to GlassFish 4.1 server cluster 
> with 4 instances, each having 8 GB of RAM available (Xmx=8g). We have to 
> restart them every couple of days (3-4), because of leaking memory. After 
> analyzing a couple of heap dumps, our main suspect is Groovy with its 
> MetaMethodIndex$Entry class (the below table shows the top object from one of 
> the heap dumps).
> ||Class Name||Objects||Shallow Heap||Retained Heap||
> |MetaMethodIndex$Entry| 3 360 001 |  188 160 056 | >= 305 408 024
> To confirm our suspicions, I created simple Maven project with a single test 
> case. The project is available on 
> [GitHub|https://github.com/jigga/groovy-jsr223-leak]. The test case executes 
> 10 different scripts (minimal differences) obtained from a single template 
> 20000 times in 64 worker threads (the main thread is put to sleep for 10 
> seconds before starting worker threads, so that one can attach JVisualVM to 
> the test process). After all threads are done, System.gc() is called to 
> provoke full GC. Attaching to the process in which tests are run with 
> JVisualVM reveals that the memory is not reclaimed.
> To run the test in your local environment, simply clone the 
> [GitHub|https://github.com/jigga/groovy-jsr223-leak] project and run:
> {code}
> mvn test
> {code}
> The same test can be run with the *-Dlanguage=javascript* system option, 
> which switches ScriptEngine from Groovy to Nashorn and uses slightly modified 
> script template (only syntactical differences).
> {code}
> mvn -Dlanguage=javascript test
> {code}
> Running the test case using built-in Nashorn engine reveals no problems - all 
> allocated memory is reclaimed after full GC.
> I know that the test case is run in Java SE environment, but I guess that it 
> clearly reflects the issue. If it's not enough, I can create an arquillian 
> test case.
> This may be a possible duplicate of 
> [GROOVY-7109|https://issues.apache.org/jira/browse/GROOVY-7109].
> Any workarounds for this issue would be greatly appreciated.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to