Effectively running Java on ServerlessTiered Compilation1 min

With the Java 17 runtime, this feature is enabled by default.

Java uses Just In Time (JIT) compilers to compile bytecode to native code for frequently executed sections to boost performance. There are two types of JIT compilers; C1 is optimized for faster start-up times, and C2 is optimized for better overall performance. With the option of these two JIT compilers, Java supports five levels of compilations allowing us to optimize for faster startup at one end and faster performance at the other end. For lower method complexity typical for Lambda functions, it is recommended that we explicitly set the compilation level to 1, where the compiler quickly produces optimized native code but does not generate any profiling data. This reduces cold start times.

Compilation level is set by adding the JAVA_TOOL_OPTIONS environment variable to value -XX:+TieredCompilation -XX:TieredStopAtLevel=1

Here is a code snippet for CDK:

Function.Builder.create(this, "MyLambdaFunction")
  // ... Other parameters are omitted for brevity
  .environment(Map.of(
    "JAVA_TOOL_OPTIONS", "-XX:+TieredCompilation -XX:TieredStopAtLevel=1"
  ))
  .build();

Created by:

Vipan Kumar
Vipan KumarSr. Solutions Architect