Skip to content

Instantly share code, notes, and snippets.

View melix's full-sized avatar

Cédric Champeau melix

View GitHub Profile

If you take a look at the dump here: https://dl.dropboxusercontent.com/u/20288797/temp/memory-dump.tar.bz2

And that you look at the classes, you will see several copies of the class org.codenarc.rule.Violationororg.codehaus.groovy.antlr.parser.GroovyLexer`. The multiple copies come from different classloaders, which is fine. What is not is that those classes are not retained anymore, nor is their classloader (they are all softly or weakly reachable), but they are not garbage collected, leading to the leak.

To reproduce the PermGen leak, first make sure you use JDK 7 or JDK 6, but the two should be tested independently because Groovy doesn't use the same way to store global class information (ClassValue since JDK 7).

@melix
melix / bench_markupbuilder.groovy
Created February 14, 2014 11:30
Micro-Benchmarks for the MarkupTemplateEngine in Groovy
@Grab(group='org.gperfutils', module='gbench', version='0.4.2-groovy-2.1')
@Grab('org.freemarker:freemarker:2.3.9')
import groovy.text.markup.MarkupTemplateEngine
import freemarker.template.*
MarkupTemplateEngine engine = new MarkupTemplateEngine()
def mkpTemplate1 = engine.createTemplate '''
html {
body('It works!')
}
@melix
melix / taglib_reuse.groovy
Created February 25, 2014 17:40
Reuse of Grails taglibs in the markup template engine
void testGrailsTagLibCompatibility() {
TemplateConfiguration config = new TemplateConfiguration()
MarkupTemplateEngine engine = new MarkupTemplateEngine(this.class.classLoader, config)
def template = engine.createTemplate '''g.emoticon(happy:'true') { 'Hi John' }
'''
StringWriter rendered = new StringWriter()
def model = [:]
def tpl = template.make(model)
model.g = new TagLibAdapter(tpl)
model.g.registerTagLib(SimpleTagLib)
@melix
melix / TransformLocalVariable.groovy
Created March 6, 2014 18:31
A demonstration on how to transform a local variable using a compilation customizer
import org.codehaus.groovy.ast.ClassCodeExpressionTransformer
import org.codehaus.groovy.ast.ClassNode
import org.codehaus.groovy.ast.expr.ConstantExpression
import org.codehaus.groovy.ast.expr.DeclarationExpression
import org.codehaus.groovy.ast.expr.Expression
import org.codehaus.groovy.classgen.GeneratorContext
import org.codehaus.groovy.control.CompilationFailedException
import org.codehaus.groovy.control.CompilePhase
import org.codehaus.groovy.control.CompilerConfiguration
import org.codehaus.groovy.control.SourceUnit
@melix
melix / ConversionTest.java
Created April 23, 2014 20:01
Conversion of MethodHandle
/*
* Copyright 2003-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.0'
}
}
apply plugin: 'android'
def "lazy constant evaluation"() {
given:
def config = new CompilerConfiguration()
config.optimizationOptions.indy = true
def shell = new GroovyShell(config)
when:
def method = shell.evaluate '''import groovy.transform.CompileStatic
import java.lang.invoke.CallSite
import java.lang.invoke.ConstantCallSite
@melix
melix / caching.groovy
Created May 21, 2014 14:51
Caching with invokedynamic
import groovy.transform.CompileStatic
import java.lang.invoke.CallSite
import java.lang.invoke.ConstantCallSite
import java.lang.invoke.MethodHandle
import java.lang.invoke.MethodHandles
import java.lang.invoke.MethodType
import java.lang.invoke.SwitchPoint
import static groovyjarjarasm.asm.Opcodes.*
@melix
melix / hodorlang.groovy
Last active August 29, 2015 14:01
The Hodor DSL
import org.codehaus.groovy.control.CompilerConfiguration
/*
* Copyright 2003-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
@melix
melix / infiniteloop.groovy
Created June 11, 2014 19:54
Infinite loop
import static Double.POSITIVE_INFINITY as ∞
∞.times {
println it
Thread.sleep(1000)
}