Git Product home page Git Product logo

groovy-memory-leak's Introduction

Groovy Memory Leak

VM Options

 -Dgroovy.use.classvalue=true 
 -XX:+ClassUnloadingWithConcurrentMark 
 -XX:+CMSScavengeBeforeRemark 
 -XX:+ScavengeBeforeFullGC 
 -XX:ParallelGCThreads=2 
 -XX:MetaspaceSize=50M 
 -XX:MaxMetaspaceSize=50M 
 -XX:CompressedClassSpaceSize=30M 
 -XX:MinMetaspaceFreeRatio=0 
 -XX:MaxMetaspaceFreeRatio=100

Unsafe Groovy Script Execution

Test executed with class GroovyMemoryLeakWithoutClose which execute a dynamic groovy script without nothing safely close operation

    GroovyCodeSource groovyCodeSource = new GroovyCodeSource(script, "SCRIPT", GroovyShell.DEFAULT_CODE_BASE);
    GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
    Class groovyClass = groovyClassLoader.parseClass(groovyCodeSource, false);
    Script groovyScript = InvokerHelper.createScript(groovyClass, binding);
    groovyScript.run();

Screenshot

Safe Groovy Script Execution

Test executed with class GroovyMemoryLeakWithClose which execute a dynamic groovy script with some safely close operation

Used these methods from GroovySystem at startup

    GroovySystem.setKeepJavaMetaClasses(false);
    GroovySystem.stopThreadedReferenceManager();

A short description of follow code:

  1. Create a temp file with the script groovy
  2. Create an empty UrlClassLoader
  3. Create a new GroovyClassLoader from UrlClassLoader
  4. Load groovy script from file
  5. Execute groovy script
  6. Close all classloader
  7. Delete the temp file
    String groovyFilename = "script-" + UUID.randomUUID() + ".groovy";
    FileOutputStream fileOutputStream = new FileOutputStream(groovyFilename);
    fileOutputStream.write(script.getBytes(StandardCharsets.UTF_8));
    fileOutputStream.close();

    GroovyCodeSource groovyCodeSource = new GroovyCodeSource(new File(groovyFilename));
    groovyCodeSource.setCachable(false);

    URLClassLoader urlClassLoader = URLClassLoader.newInstance(new URL[]{});
    GroovyClassLoader groovyClassLoader = new GroovyClassLoader(urlClassLoader);
    Class groovyClass = groovyClassLoader.parseClass(groovyCodeSource, false);
    Script groovyScript = InvokerHelper.createScript(groovyClass, binding);
    groovyScript.run();

    GroovySystem.getMetaClassRegistry().removeMetaClass(groovyScript.getMetaClass().getTheClass());
    GroovySystem.getMetaClassRegistry().removeMetaClass(groovyClass);
    groovyClassLoader.clearCache();
    groovyClassLoader.clearAssertionStatus();
    groovyClassLoader.close();
    urlClassLoader.clearAssertionStatus();
    urlClassLoader.close();

    Files.deleteIfExists(Paths.get(groovyFilename));

Screenshot

groovy-memory-leak's People

Contributors

robertonav20 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.