Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SpringBoot对于Java运行时环境内存不足_Java_Spring Boot_Memory Leaks - Fatal编程技术网

SpringBoot对于Java运行时环境内存不足

SpringBoot对于Java运行时环境内存不足,java,spring-boot,memory-leaks,Java,Spring Boot,Memory Leaks,我在5个线程(每个线程6000个)中执行了30000多个REST请求,在SpringBoot内的Tomcat上大摇大摆地运行,运行35分钟后出现下一个错误: # # There is insufficient memory for the Java Runtime Environment to continue. # Native memory allocation (mmap) failed to map 16384 bytes for committing reserved memory.

我在5个线程(每个线程6000个)中执行了30000多个REST请求,在SpringBoot内的Tomcat上大摇大摆地运行,运行35分钟后出现下一个错误:

#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 16384 bytes for committing reserved memory.
# Possible reasons:
#   The system is out of physical RAM or swap space
#   The process is running with CompressedOops enabled, and the Java Heap may be blocking the growth of the native heap
# Possible solutions:
#   Reduce memory load on the system
#   Increase physical memory or swap space
#   Check if swap backing store is full
#   Decrease Java heap size (-Xmx/-Xms)
#   Decrease number of Java threads
#   Decrease Java thread stack sizes (-Xss)
#   Set larger code cache with -XX:ReservedCodeCacheSize=
#   JVM is running with Zero Based Compressed Oops mode in which the Java heap is
#     placed in the first 32GB address space. The Java Heap base address is the
#     maximum limit for the native heap growth. Please use -XX:HeapBaseMinAddress
#     to set the Java Heap base and to place the Java Heap above 32GB virtual address.
# This output file may be truncated or incomplete.
#
#  Out of Memory Error (os_linux.cpp:2985), pid=24780, tid=58785
#
# JRE version: OpenJDK Runtime Environment (11.0.11+9) (build 11.0.11+9-Ubuntu-0ubuntu2.20.04)
# Java VM: OpenJDK 64-Bit Server VM (11.0.11+9-Ubuntu-0ubuntu2.20.04, mixed mode, sharing, tiered, compressed oops, g1 gc, linux-amd64)

Command Line: -Xverify:none -XX:TieredStopAtLevel=1 -Xms2g -Xmx10g com.CamelSpringBootApplication

Host: Common KVM processor, 16 cores, 28G, Ubuntu 20.04.2 LTS
Time: Mon May 31 15:40:22 2021 CEST elapsed time: 2122.252495 seconds (0d 0h 35m 22s)
SpringBoot应用程序在Ubuntu虚拟机上运行,该虚拟机应该有64GB的RAM,但正如您在下一张图中看到的,该值正在疯狂下降

我遗漏了一些信息,不知道原因。我在哪里犯了错误?到底是什么占用了这么多内存?我如何在运行时释放内存

只有在重新启动虚拟机后,内存才会恢复到64GB。 在我再次启动SpringBoot应用程序之后,RAM甚至在运行REST请求之前就开始下降。 我需要支持尽可能多的REST请求,保留RAM并阻止SpringBoot应用程序崩溃

更新:

这些是收集的堆转储的结果。我已将heapdump.bin加载到heaphero.io中。我将非常感谢基于此结果的任何提示


只是一个危险猜测,您可能已经超过了垃圾收集的速度 数完30000个左右数字的四分之一后,打电话给 gc(); 通知收集垃圾。
位于RAM中,需要一个小的垃圾管理检查和计数器。

我希望您已经分配了足够的堆大小,即使您的Linux机器有64 GB RAM,您也需要根据应用程序传入的流量从外部扩展其默认容量

您可以在pom.xml文件中增加spring boot应用程序的堆大小,如下所示-

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <jvmArguments>-Xmx4096m</jvmArguments>
    </configuration>
</plugin>

org.springframework.boot
springbootmaven插件
-Xmx4096m

您可以查看应用程序崩溃时生成的堆转储(或在高内存消耗期间手动创建一个堆转储)。您可以使用这个来查看什么需要大量内存。使用哪些工具可以做到这一点?是否有任何可以直接在VM内的终端中运行的文件?请参阅如何获取堆转储。然后,您可以使用eclipse内存分析器工具之类的工具来分析它。我会在使用后尝试清理字符串。我没有找到这种内存行为的确切原因,但很可能主要原因是我的代码,应该对其进行编辑和优化。内存泄漏的另一个可能原因是,Spring Boot中的Tomcat需要在短时间内处理大量请求(如果不是自动完成,则应该正确关闭),并且应该为该任务正确配置。我在代码中添加了大量System.gc()调用,在它们之前设置变量(主要是ArrayList、Maps和JSONObject)设置为null,但看起来Java忽略了这些调用,内存仍在下降。我将-Xmx扩展到16GB,但没有帮助。