groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "t.schoellhorn" <t.schoellh...@plattform-gmbh.de>
Subject TemplateEngines and Out of Memory
Date Sat, 25 Jun 2016 06:12:20 GMT
Hi, we are using Groovy in our web-application. There we create a "lot" of
dynamic String and are handling that with the TemplateEngine as we need more
complex expressions there. Now we noticed that we are running into a kind of
weird OOM situation. I could narrow the problem down to a quite simple
reproducible example: 
package kos.tools.template;import java.util.HashMap;import
java.util.Map;import javax.script.Bindings;import
javax.script.ScriptEngine;import javax.script.ScriptEngineManager;import
javax.script.SimpleBindings;public class Test {	public static void
main(String[] args) throws Exception {		ScriptEngineManager factory = new
ScriptEngineManager();		ScriptEngine engine =
factory.getEngineByName("groovy"); 				String template = "step-${i}";			
String groovy = 			"def engine = new groovy.text.GStringTemplateEngine();\n"
+			"def res = engine.createTemplate(template).make(bindings);\n" + 		
"return res.toString();";		for (int i = 0; i < (10000000); i++) {			Bindings
vars = new SimpleBindings();			vars.put("template", template);					
Map<String, Object> templateObjects = new HashMap<>();		
vars.put("bindings", templateObjects);			templateObjects.put("i", i);		
Object res = engine.eval(groovy, vars);						if (i % 100 == 0) {			
System.out.println("->" + res);			}		}	}	}
Running that example with an Oracle JDK 1.8 and a quite small memory (just
for keeping the running time small) of 32M leads to an OOM at around 2500
steps. I opened up a disussion on Stackoverflow where John Vint gave me some
hints ( see here
<http://stackoverflow.com/questions/37992822/groovy-templateengines-and-outofmemory-possible-memory-leak?>

) and it might be also a bug of the GarbageCollector itself. But in essence
I think there should be a way to use the TemplateEngine in a stable way. I
am happy for any suggestions and examples of how to solve this situation.
Tino



--
View this message in context: http://groovy.329449.n5.nabble.com/TemplateEngines-and-Out-of-Memory-tp5733514.html
Sent from the Groovy Users mailing list archive at Nabble.com.
Mime
View raw message