Search code examples
aws-lambdajava-8garbage-collectionout-of-memory

Garbage collection in AWS lambda java 8


I have an AWS lambda application written in Java 8. Recently I was seeing the memory was reaching to almost 95%. Then I increased the memory by almost 100MB, and then this issue was not there. I wanted to understand one thing here, like in other applications like ECS or any general JAVA application, the GC comes into play once the memory reaches approx 90%, does that same behavior come up in case of lambdas as well ?

Or, is there any other way of memory cleanup happening in lambda applications written in java 8 ?


Solution

  • Enable logs if you want to be sure how it is working:

    Prior to Java 9, including Java 8, you configure the garbage collection logging as follows:

    JAVA_TOOL_OPTIONS=-XX:+PrintGCDetails -XX:+PrintGCDateStamps
    

    Java 11 uses the Unified Logging System (JEP 158 and JEP 271) which has been introduced in Java 9. Logging can be configured with the environment variable:

    JAVA_TOOL_OPTIONS=-Xlog:gc+metaspace,gc+heap,gc:stdout:time,tags
    

    The lifecycle of a JVM application on AWS Lambda

    Let’s first revisit the lifecycle of the AWS Lambda Java runtime and its JVM:

    • A Lambda function is invoked.
    • AWS Lambda launches an execution context. This is a temporary runtime environment based on the configuration settings you provide, like permissions, memory size, and environment variables.
    • AWS Lambda creates a new log stream in Amazon CloudWatch Logs for each instance of the execution context.
    • The execution context initializes the JVM and your handler’s code.

    AWS Lambda maintains the execution context for some time in anticipation of another Lambda function invocation. In effect, the service freezes the execution context after a Lambda function completes. It thaws the execution context when the Lambda function is invoked again if AWS Lambda chooses to reuse it.

    During invocations, the JVM also maintains garbage collection as usual. Outside of invocations, the JVM and its maintenance processes like garbage collection are also frozen.

    https://aws.amazon.com/blogs/architecture/field-notes-monitoring-the-java-virtual-machine-garbage-collection-on-aws-lambda/