垃圾收集在AWS Lambda中的Java 8

huangapple go评论68阅读模式
英文:

Garbage collection in AWS lambda java 8

问题

我有一个用Java 8编写的AWS Lambda应用程序。最近,我发现内存使用率几乎达到了95%。然后,我将内存增加了大约100MB,然后这个问题就解决了。我想要了解一件事,在其他应用程序中,比如ECS或任何通用的JAVA应用程序,在内存使用率达到大约90%时,GC会开始运行,Lambda应用程序是否也会出现相同的行为?

或者,Java 8编写的Lambda应用程序中是否存在其他内存清理的方式?

英文:

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 ?

答案1

得分: 2

启用日志以确保其正常工作:

在Java 9之前,包括Java 8,在配置垃圾收集日志时,可以按如下操作:

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

Java 11使用统一日志系统(JEP 158和JEP 271),该系统已在Java 9中引入。可以使用环境变量配置日志记录:

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

AWS Lambda上JVM应用程序的生命周期

首先让我们重新审视AWS Lambda Java运行时及其JVM的生命周期:

  • 调用Lambda函数。
  • AWS Lambda启动执行上下文。这是一个临时的运行时环境,基于您提供的配置设置,如权限、内存大小和环境变量。
  • AWS Lambda为执行上下文的每个实例在Amazon CloudWatch Logs中创建一个新的日志流。
  • 执行上下文初始化JVM和您的处理程序代码。

AWS Lambda在预期将会有另一个Lambda函数调用时保持执行上下文一段时间。实际上,在Lambda函数完成后,服务会冻结执行上下文。如果AWS Lambda选择重用它,它将在再次调用Lambda函数时解冻执行上下文。

在调用期间,JVM也会像平常一样维护垃圾收集。在调用之外,JVM及其垃圾收集等维护过程也会被冻结。

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

英文:

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/

huangapple
  • 本文由 发表于 2023年5月30日 12:18:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76361589.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定