You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Cody Boisclair (JIRA)" <ji...@apache.org> on 2016/07/14 16:20:20 UTC

[jira] [Comment Edited] (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:comment-tabpanel&focusedCommentId=15377212#comment-15377212 ] 

Cody Boisclair edited comment on GROOVY-7886 at 7/14/16 4:20 PM:
-----------------------------------------------------------------

The result from compiling the same script as a Java class and running it:

{noformat}
After initial assignment
Current heap: 58314696
Reassigning once inside loop
Current heap: 58201536
Current heap: 58126960
Current heap: 58077664
Current heap: 58045064
Current heap: 58023512
Current heap: 58009264
Current heap: 57999840
Current heap: 57993608
Current heap: 57989488
Current heap: 57986760
Reassigning 3x inside loop
Current heap: 57984960
Current heap: 57983768
Current heap: 57983496
Current heap: 57983496
Current heap: 57983496
Current heap: 57983496
Current heap: 57983496
Current heap: 57983496
Current heap: 57983496
Current heap: 57983496
Current heap: 57983496
Current heap: 57983496
Reassigning 10x outside loop
Current heap: 57983496
Current heap: 57983496
Current heap: 57983496
Current heap: 57983496
Current heap: 57983496
Current heap: 57983496
Current heap: 57983496
Current heap: 57983496
Current heap: 57983496
Current heap: 57983496
After end of doReassignments
Current heap: 57983496
{noformat}

(Again, note that both the Groovy and Java results are *with* garbage collection explicitly run after each reassignment.)


was (Author: codeman38):
The result from compiling the same script as a Java class and running it:

{noformat}
After initial assignment
Current heap: 58314696
Reassigning once inside loop
Current heap: 58201536
Current heap: 58126960
Current heap: 58077664
Current heap: 58045064
Current heap: 58023512
Current heap: 58009264
Current heap: 57999840
Current heap: 57993608
Current heap: 57989488
Current heap: 57986760
Reassigning 3x inside loop
Current heap: 57984960
Current heap: 57983768
Current heap: 57983496
Current heap: 57983496
Current heap: 57983496
Current heap: 57983496
Current heap: 57983496
Current heap: 57983496
Current heap: 57983496
Current heap: 57983496
Current heap: 57983496
Current heap: 57983496
Reassigning 10x outside loop
Current heap: 57983496
Current heap: 57983496
Current heap: 57983496
Current heap: 57983496
Current heap: 57983496
Current heap: 57983496
Current heap: 57983496
Current heap: 57983496
Current heap: 57983496
Current heap: 57983496
After end of doReassignments
Current heap: 57983496
{noformat}

> 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.4#6332)