Adding the following system property may help to extend the time before OOM:
groovy.use.classvalue=true At least for me on Groovy 2.4.7 with jdk8_91 using that property let the loop run 25 minutes (made about half the iterations) vs. ~2.5 minutes without the property. I used a 64m heap. I believe the problem is related to GROOVY-7683. On Fri, Jun 24, 2016 at 11:12 PM, t.schoellhorn < t.schoellh...@plattform-gmbh.de> wrote: > 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: TemplateEngines and Out of Memory > <http://groovy.329449.n5.nabble.com/TemplateEngines-and-Out-of-Memory-tp5733514.html> > Sent from the Groovy Users mailing list archive > <http://groovy.329449.n5.nabble.com/Groovy-Users-f329450.html> at > Nabble.com. >