Class ResourceLimits

java.lang.Object
org.graalvm.polyglot.ResourceLimits

public final class ResourceLimits extends Object
Represents resource limits configuration that is used to configure contexts. Resource limit instances are created using the builder and activated by enabling them for a context. All configured limits are applied per context instance. Resource limits may be reset. If a resource limit is triggered the context that triggered the limit will automatically be closed and can no longer be used to evaluate code.

The following resource limits are supported:

  • Statement count limit per context. Allows to limit the amount of statements executed per context.

Statement Limit Example

 ResourceLimits limits = ResourceLimits.newBuilder()
                       .statementLimit(500, null)
                   .build();
 try (Context context = Context.newBuilder("js")
                            .resourceLimits(limits)
                        .build();) {
     try {
         context.eval("js", "while(true);");
         assert false;
     } catch (PolyglotException e) {
         // triggered after 500 iterations of while(true);
         // context is closed and can no longer be used
         assert e.isCancelled();
     }
 }
 
Since:
19.3
See Also: