com.corunet:groovy-mem-limiter

Groovy facilities to check and limit memory allocation on single-thread Groovy scripts

License

License

Categories

Categories

Groovy Languages Net
GroupId

GroupId

com.corunet
ArtifactId

ArtifactId

groovy-mem-limiter
Last Version

Last Version

1.2.0
Release Date

Release Date

Type

Type

jar
Description

Description

com.corunet:groovy-mem-limiter
Groovy facilities to check and limit memory allocation on single-thread Groovy scripts
Project URL

Project URL

https://github.com/corunet/groovy-memory-limiter/
Source Code Management

Source Code Management

https://github.com/corunet/groovy-memory-limiter/tree/master

Download groovy-mem-limiter

How to add to project

<!-- https://jarcasting.com/artifacts/com.corunet/groovy-mem-limiter/ -->
<dependency>
    <groupId>com.corunet</groupId>
    <artifactId>groovy-mem-limiter</artifactId>
    <version>1.2.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.corunet/groovy-mem-limiter/
implementation 'com.corunet:groovy-mem-limiter:1.2.0'
// https://jarcasting.com/artifacts/com.corunet/groovy-mem-limiter/
implementation ("com.corunet:groovy-mem-limiter:1.2.0")
'com.corunet:groovy-mem-limiter:jar:1.2.0'
<dependency org="com.corunet" name="groovy-mem-limiter" rev="1.2.0">
  <artifact name="groovy-mem-limiter" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.corunet', module='groovy-mem-limiter', version='1.2.0')
)
libraryDependencies += "com.corunet" % "groovy-mem-limiter" % "1.2.0"
[com.corunet/groovy-mem-limiter "1.2.0"]

Dependencies

compile (2)

Group / Artifact Type Version
org.codehaus.groovy : groovy-all jar 2.4.6
javax.validation : validation-api jar 2.0.1.Final

test (1)

Group / Artifact Type Version
org.junit.jupiter : junit-jupiter jar 5.7.0-M1

Project Modules

There are no modules declared in this project.

Build Status

groovy-memory-limiter

Limits memory allocation in a Groovy script by regularly checking allocated memory. It also offers stats on script memory use.

Checking memory

The limiter will regularly check for the amount of memory allocated by the script's executing thread. If it is exceeded, a callback will be executed to a method expecting a MemoryQuotaCheck instance, which can be queried to find out more about the infringement or used to reconfigure the limit.

If you are not interrupting the script in response to the quota infringement, you may want to disable further reporting via MemoryQuotaCheck.setEnabled().

Also, for added flexibility, the script's binding can also be retrieved via MemoryQuotaCheck.getScriptBinding().

From Java

The following code will call the QuotaInfringementHandler#handle method at the beginning of the Groovy method().

public class MemoryLimit {
    public static void main() {}
        Map<String, Object> map = new HashMap<>();
        map.put("limit", 1024 * 1024 * 2); // 1KB
        map.put("handlerClass", QuotaInfringementHandler.class);
        map.put("handlerMethod", "handle");
        CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
        compilerConfiguration.addCompilationCustomizers(new ASTTransformationCustomizer(map, CheckMemoryQuota.class));
        GroovyShell groovyShell = new GroovyShell(compilerConfiguration);
        //noinspection GroovyUnusedAssignment
        groovyShell.evaluate(
            "def garbage = new byte[1024 * 1024 * 3]\n"
                + "def method() { /* do nothing */ }\n"
                + "method() \n"
                + "return 5"
        );
    }
}
From Groovy

Just annotate your script with @CheckMemoryQuota(limit=bytes, handlerClass=Handler.class, handlerMethod="methodName").

Recovering stats after execution

Average and peak memory consumption can be recovered from the MemoryQuotaCheck instance after the script finishes execution. Just recover it from the Script object like this.

Script script = groovyShell.parse(yourGroovy);

script.run();
MemoryQuotaCheck memoryQuotaCheck =
    (MemoryQuotaCheck) script.getProperty(MemoryQuotaCheck.CHECKER_FIELD);

Caveats

This uses com.sun.management.ThreadMXBean to watch memory allocation of a thread and thus it will only run on JVM providing such class (ie. Oracle's JVM).

The quota only applies to the thread that first launches the Groovy script. This will not work on multi-threaded code.

Checks are inserted at the beginning of every loop iteration, closure and method call. If the memory limit is infringed during a library call or on a long row of assignments, the limiter will not be notified until one of those ocurrs.

Credit

This project is a fork of groovy-memory-limiter by Denis Kirpichenkov (d0k1).

com.corunet

Corunet

Versions

Version
1.2.0
1.1.0
1.0.3