如何减少Spring Boot的内存使用量? [英] how to reduce spring boot memory usage?

查看:1057
本文介绍了如何减少Spring Boot的内存使用量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Boot开发客户端应用程序. 当运行spring boot应用程序(使用完全可执行的jar)时,x64服务器的内存使用约为190M,x86服务器的内存使用约为110M.

I'm using spring boot to develope client application. and when run spring boot application(using fully executable jar), the memory usage is about 190M in x64 server, and 110M in x86 server.

我的jvm选项是(-Xmx64M -Xms64M -XX:MaxPermSize = 64M -server), 为什么在x64服务器中,内存使用量如此之大? 如何将内存使用量减少到150M以下?

my jvm option is (-Xmx64M -Xms64M -XX:MaxPermSize=64M -server), why in x64 server, memory usage is so big? how to reduce memory usage below 150M?

谢谢.

推荐答案

在这里玩的太晚了,但是在Docker上的容器化Spring Boot应用程序遇到了同样的问题.在具有单个控制器和嵌入式Tomcat的最简单的Spring Boot应用程序上,您将获得的最小内存总量约为72M.投入Spring Data REST,Spring Security和一些JPA实体,您的最低要求为200M-300M.使用以下JVM选项,您可以将一个简单的Spring Boot应用程序总数减少到约7200万.

Little late to the game here, but I suffered the same issue with a containerised Spring Boot application on Docker. The bare minimum you'll get away with is around 72M total memory on the simplest of Spring Boot applications with a single controller and embedded Tomcat. Throw in Spring Data REST, Spring Security and a few JPA entities and you'll be looking at 200M-300M minimum. You can get a simple Spring Boot app down to around 72M total by using the following JVM options.

使用 -XX:+UseSerialGC这将与分配堆内存的线程而不是专用GC线程内联地执行垃圾回收

With -XX:+UseSerialGC This will perform garbage collection inline with the thread allocating the heap memory instead of a dedicated GC thread(s)

使用 -Xss512k这会将每个线程的堆栈内存限制为512KB,而不是默认的1MB

With -Xss512k This will limit each threads stack memory to 512KB instead of the default 1MB

使用 -XX:MaxRAM=72m这将把JVM对堆和非堆托管内存的计算限制在此值的限制内.

With -XX:MaxRAM=72m This will restrict the JVM's calculations for the heap and non heap managed memory to be within the limits of this value.

除了上述JVM选项之外,您还可以在application.properties文件中使用以下属性:

In addition to the above JVM options you can also use the following property inside your application.properties file:

server.tomcat.max-threads=5这会将HTTP请求处理程序线程的数量限制为1(默认值为50)

server.tomcat.max-threads=5 This will limit the the number of HTTP request handler threads to 1 (default is 50)

这是docker stats在具有上述限制和docker -m 72m参数的情况下运行非常简单的Spring Boot应用程序的示例.如果我将值减小到任何比此低的值,我将无法启动该应用.

Here is an example of docker stats running a very simple Spring Boot application with the above limits and with the docker -m 72m argument. If I decrease the values any lower than this I cannot get the app to start.

83ccc9b2156d: Mem Usage: 70.36MiB / 72MiB | Mem Percentage: 97.72%

在这里,您可以看到退出时所有本机和Java堆内存的明细.

And here you can see a breakdown of all the native and java heap memory on exit.

Native Memory Tracking:

Total: reserved=1398681KB, committed=112996KB
-                 Java Heap (reserved=36864KB, committed=36260KB)
                            (mmap: reserved=36864KB, committed=36260KB) 

-                     Class (reserved=1086709KB, committed=43381KB)
                            (classes #7548)
                            (  instance classes #7049, array classes #499)
                            (malloc=1269KB #19354) 
                            (mmap: reserved=1085440KB, committed=42112KB) 
                            (  Metadata:   )
                            (    reserved=36864KB, committed=36864KB)
                            (    used=36161KB)
                            (    free=703KB)
                            (    waste=0KB =0.00%)
                            (  Class space:)
                            (    reserved=1048576KB, committed=5248KB)
                            (    used=4801KB)
                            (    free=447KB)
                            (    waste=0KB =0.00%)

-                    Thread (reserved=9319KB, committed=938KB)
                            (thread #14)
                            (stack: reserved=9253KB, committed=872KB)
                            (malloc=50KB #74) 
                            (arena=16KB #26)

-                      Code (reserved=248678KB, committed=15310KB)
                            (malloc=990KB #4592) 
                            (mmap: reserved=247688KB, committed=14320KB) 

-                        GC (reserved=400KB, committed=396KB)
                            (malloc=272KB #874) 
                            (mmap: reserved=128KB, committed=124KB) 

-                  Compiler (reserved=276KB, committed=276KB)
                            (malloc=17KB #409) 
                            (arena=260KB #6)

-                  Internal (reserved=660KB, committed=660KB)
                            (malloc=620KB #1880) 
                            (mmap: reserved=40KB, committed=40KB) 

-                    Symbol (reserved=11174KB, committed=11174KB)
                            (malloc=8417KB #88784) 
                            (arena=2757KB #1)

-    Native Memory Tracking (reserved=1858KB, committed=1858KB)
                            (malloc=6KB #80) 
                            (tracking overhead=1852KB)

-               Arena Chunk (reserved=2583KB, committed=2583KB)
                            (malloc=2583KB) 

-                   Logging (reserved=4KB, committed=4KB)
                            (malloc=4KB #179) 

-                 Arguments (reserved=17KB, committed=17KB)
                            (malloc=17KB #470) 

-                    Module (reserved=137KB, committed=137KB)
                            (malloc=137KB #1616)

也不要期望从中获得任何不错的性能,因为我想GC会在此设置下频繁运行,因为它没有太多可用的备用内存

Don't expect to get any decent performance out of this either, as I would imagine the GC would be running frequently with this setup as it doesn't have a lot of spare memory to play with

这篇关于如何减少Spring Boot的内存使用量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆