java 运行 Spring Boot 应用程序的初始内存量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35371411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:04:30  来源:igfitidea点击:

Initial amount of memory to run a Spring Boot application

javaspringtomcatspring-bootundertow

提问by Carlos Alberto

Is there any initial amount of memory I should set to run a Spring Boot application?

我应该设置任何初始内存量来运行 Spring Boot 应用程序吗?

For example, to run a embedded Tomcat/Undertow application we could set a typical amount of memory for -Xms and -Xmx java options.

例如,要运行嵌入式 Tomcat/Undertow 应用程序,我们可以为 -Xms 和 -Xmx java 选项设置典型的内存量。

How can I find out which values I need to set for running my application?

如何找出运行应用程序需要设置哪些值?

回答by pczeus

You should run the JavaMissionControl or another Profiler to see your actual memory requirements. You can run the JavaMissionControl on the server where Java is installed (and in the path) by running 'jmc' from a command prompt, then attaching to your local JVM. You can attach to a remote JVM as well. For more information, refer to this link: http://www.oracle.com/technetwork/java/javaseproducts/mission-control/index.html

您应该运行 JavaMissionControl 或其他 Profiler 来查看您的实际内存需求。您可以通过从命令提示符运行“jmc”,然后附加到本地 JVM,在安装了 Java 的服务器上(以及在路径中)运行 JavaMissionControl。您也可以附加到远程 JVM。有关更多信息,请参阅此链接:http: //www.oracle.com/technetwork/java/javaseproducts/mission-control/index.html

Typically what I do is set the max memory higher than needed, max = 1GB seems like a good starting point. You will want to watch the memory profile of your application over a period of typical activity. Realize that when you set an initial heap size of 64mb, the garbage collector is running a scheme that will not start a major garbage collection until necessary for performance reasons (garbage collection is expensive). As a result, you will see the memory creep up over time to around 80% of the heap size (this % is adjustable). Once the threshold is reached, garbage collection kicks in and you will see the used JVM memory drop very low again.

通常我所做的是将最大内存设置为高于所需,max = 1GB 似乎是一个很好的起点。您将需要在一段典型活动期间观察应用程序的内存配置文件。请注意,当您将初始堆大小设置为 64mb 时,垃圾收集器正在运行一个方案,除非出于性能原因(垃圾收集很昂贵),否则不会启动主要垃圾收集。结果,您会看到内存随着时间的推移逐渐增加到堆大小的 80% 左右(这个百分比是可调整的)。一旦达到阈值,垃圾收集就会开始,您将看到使用的 JVM 内存再次下降到非常低的水平。

Normally what I am looking for is the level of memory that is in use consistently after a series of major garbage collections. So let's say after running your application you see that the solid state memory usage after a major garbage collection consistently drops to around 40Mb. Therefore, I typically will then set the min JVM size to around this amount, since I know I always need at least that much as a minimum.

通常我正在寻找的是在一系列主要垃圾收集之后始终使用的内存级别。因此,假设在运行您的应用程序后,您看到主要垃圾收集后的固态内存使用量始终下降到 40Mb 左右。因此,我通常会将最小 JVM 大小设置为大约这个数量,因为我知道我总是至少需要那么多。

Now you know a decent starting point for your minimum requirement, so what would be the maximum memory needed? Well, this takes a little more profiling and trial and error. What I do is begin to reduce the maximum memory setting lower and profile for a while. What you are looking for is the frequency of major garbage collections and the JVM CPU utilization reaching consistently high values.

现在您知道了最低要求的合适起点,那么所需的最大内存是多少?嗯,这需要更多的分析和反复试验。我所做的是开始降低最大内存设置并降低配置文件一段时间。您正在寻找的是主要垃圾收集的频率和 JVM CPU 利用率达到始终如一的高值。

I would continually reduce the memory until I see major garbage collection happening several times in a short period of time under a load and the CPU spike up during these periods of garbage collection. Once I see this behavior, I then slowly increase the max memory until that behavior stops, to find the sweet spot of max memory needed.

我会不断减少内存,直到我看到在负载下的短时间内发生多次主要垃圾收集,并且在这些垃圾收集期间 CPU 激增。一旦我看到这种行为,我就会慢慢增加最大内存,直到该行为停止,以找到所需的最大内存的最佳位置。

This is a very simple way to find reasonable values for min/max memory settings, and works very well for me in most common application scenarios. There are of course many other more exhaustive ways to profile your application to fine tune both the memory setting and garbage collection schemes/settings if you really want to fine tune your requirements.

这是一种为最小/最大内存设置找到合理值的非常简单的方法,并且在大多数常见应用场景中对我来说效果很好。当然,如果您真的想微调您的需求,还有许多其他更详尽的方法来分析您的应用程序以微调内存设置和垃圾收集方案/设置。

回答by Andy Wilkinson

The maximum amount of memory that you need depends almost entirely on your application and the load that it needs to be able to handle. Using a tool like JMeter to simulate some load is one way to see how much memory is needed.

您需要的最大内存量几乎完全取决于您的应用程序和它需要能够处理的负载。使用像 JMeter 这样的工具来模拟一些负载是查看需要多少内存的一种方法。

The minimum amount of memory also depends on your application, but you'll need at least 25MB or thereabouts. This blog postprovides lots of additional information.

最小内存量还取决于您的应用程序,但您至少需要 25MB 左右。这篇博文提供了许多附加信息。

回答by DoctorP

Usually I launcha jar without any flags and start monitoring by 'Java VisualVM'. Based on actual data I'm setting -Xmx. Like that.

通常我会启动一个没有任何标志的 jar 并通过“Java VisualVM”开始监控。根据实际数据,我正在设置 -Xmx。像那样。