[ https://issues.apache.org/jira/browse/GROOVY-7621?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15092220#comment-15092220 ]
Kimball C Sampson commented on GROOVY-7621: ------------------------------------------- Thanks Jochen, I tried your suggestions but still fill up memory. Here is a test script that you can run to see the problem. I'm running in Windows and Groovy 2.3.7. I am not able to use setMetaClass(Object,Metaclass) I get a MissingMethod. Here is code. If you can get this code to run (in spirit the same way it does) that would be awesome. note this is a simplified script of what I am doing in practice. This script is used in application with million plus of records being processed. package com.enterworks.groovy.test import org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl def sampleSchema = [:] sampleSchema.table1 = [column1: [name:'column1', dataType : 'varchar'] , column2: [name:'column2' ,dataType : 'integer']] def propMissing = { metaThings -> def pMissing = null; pMissing = { pName -> def innerProp = delegate.hasProperty("outerType") if (!innerProp ){ if ( sampleSchema[pName] ){ def resolved = "->"+pName resolved.metaClass.propertyMissing = pMissing resolved.metaClass.outerType = "table" resolved.metaClass.outer = pName metaThings << resolved return resolved } } else if (delegate.outerType == "table" ) { if ( sampleSchema[delegate.outer][pName] ){ def resCol = "->"+pName resCol.metaClass.propertyMissing = pMissing resCol.metaClass.outerType = "column" resCol.metaClass.outer = sampleSchema[delegate.outer][pName] metaThings << resCol return resCol } } else if (delegate.outerType == "column"){ // No metaClasses because no more nesting... if ( pName == "dataType" ){ return delegate.outer.dataType } else if ( pName == "name"){ return delegate.outer.name } } throw new MissingPropertyException(pName) } return pMissing } def sampleScript = 'def result = table1.column1.dataType\n if ( !(++counter % 1000)) println "${counter}. ${result}"' def gs = new GroovyShell(); def binding = new Binding() binding.counter = 0 def script = gs.parse( sampleScript ) script.setBinding( binding) def needsCleanup = [] script.metaClass.propertyMissing = propMissing( needsCleanup ) for ( int i = 0; i < 1000000; i++){ script.run() if ( !(i % 100) ){ // needsCleanup ... how to? I tried remove from metaClassRegistry to no avail... // // this gives errro: //Caught: groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl.removeMetaClass() is applicable for argument types: (java.lang.String, null) values: [->table1, null] //Possible solutions: removeMetaClass(java.lang.Class) //needsCleanup.each{ // GroovySystem.metaClassRegistry.removeMetaClass(it,null) //} needsCleanup.each{ it.metaClass = null } needsCleanup = [] } } > Memory Leak (metaClassRegistry) unable to remove metaClass based on instances > ----------------------------------------------------------------------------- > > Key: GROOVY-7621 > URL: https://issues.apache.org/jira/browse/GROOVY-7621 > Project: Groovy > Issue Type: Bug > Components: groovy-runtime > Affects Versions: 2.3.7 > Environment: Running from Eclipse, JBoss with jdk 1.6 > Reporter: Kimball C Sampson > > I'm using the GroovyScriptEngine in a web server environment where the > scripts are provided properties on-the-fly from database objects. This is > done by implementing script.metaClass.propertyMissing. When I return a > value, I set the metaClass to give the returned value even more > sub-properties. After processing 100k records or so, I run out of memory. I > tried to write code to remove entries from the metaClassRepository, but > there's no way to do it for an object instanced based metaClass. Also, I've > outputed the GroovySystem.metaClassRepository.iterator().size() and it kept > growing. The values are out of scope so they should get garbage collected, > but the metaClasses aren't getting cleaned up. > The workaround to this problem was to implement a groovy Proxy, though, I > liked the metaClass solution better. -- This message was sent by Atlassian JIRA (v6.3.4#6332)