You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Paul King (JIRA)" <ji...@apache.org> on 2017/02/01 23:17:53 UTC

[jira] [Closed] (GROOVY-7886) Memory leak when variable is assigned multiple times within the same scope

     [ https://issues.apache.org/jira/browse/GROOVY-7886?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Paul King closed GROOVY-7886.
-----------------------------

> Memory leak when variable is assigned multiple times within the same scope
> --------------------------------------------------------------------------
>
>                 Key: GROOVY-7886
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7886
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 2.4.7
>         Environment: Linux, CentOS 6, amd64, Java 7 and 8
>            Reporter: Cody Boisclair
>         Attachments: AssignmentLeakDemo.groovy
>
>
> While troubleshooting some code that seemed to be severely leaking memory, I discovered something odd in how Groovy handles variable assignments. From what I can tell, when a variable is reassigned multiple times within a scope, all of the previous assignments are still retained on the heap until the end of the scope.
> As a demonstration, I've written a Groovy class which is also valid Java. It creates a really large StringBuilder. It calls a method which assigns the return value of the StringBuilder's {{toString}} method to a class variable repeatedly, both inside and outside of loops; after each assignment, garbage collection is run, and the size of the heap is output. Finally, after that method is finished, it garbage-collects once again and prints the final heap size.
> When compiled with {{javac}} and run as Java, there's no leak; the reported heap sizes stay fairly constant. Things get considerably more interesting, however, when it's run as a Groovy script:
> {noformat}
> After initial assignment
> Current heap: 62514408
> Reassigning once inside loop
> Current heap: 81562064
> Current heap: 81464464
> Current heap: 81464464
> Current heap: 81464464
> Current heap: 81464464
> Current heap: 81464464
> Current heap: 81464464
> Current heap: 81464464
> Current heap: 81464464
> Current heap: 81464464
> Reassigning 3x inside loop
> Current heap: 81464552
> Current heap: 101464624
> Current heap: 121464696
> Current heap: 81464616
> Current heap: 101464656
> Current heap: 121464696
> Current heap: 81464616
> Current heap: 101464656
> Current heap: 121464696
> Current heap: 81464616
> Current heap: 101464656
> Current heap: 121464696
> Reassigning 10x outside loop
> Current heap: 81464704
> Current heap: 101464776
> Current heap: 121464848
> Current heap: 141464920
> Current heap: 161464992
> Current heap: 181465064
> Current heap: 201465136
> Current heap: 221465208
> Current heap: 241465280
> Current heap: 261465352
> After end of doReassignments
> Current heap: 81465048
> {noformat}
> It seems that the older values of the variable only get garbage-collected at the end of each scope, and never within any given scope. This is true both for the {{for}} loops (the stale values are only deallocated at the end of each iteration), and for the {{doReassignments}} method as a whole.
> Is this normal behavior for Groovy? It *feels* like a bug, but is there any valid reason that the older values for a variable outside the scope might actually need to be retained?



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)