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 ?
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:
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.