简体   繁体   English

Spring Boot应用程序 - 使用“mvn spring-boot:run”和“java -jar”启动时间的差异

[英]Spring Boot application - difference in startup time with “mvn spring-boot:run” and “java -jar”

I noticed a big difference in the startup time of my Spring Boot application when starting with Maven and as jar. 我注意到在使用Maven和jar开始时,我的Spring Boot应用程序的启动时间有很大差异。 For example: 例如:

  • mvn spring-boot:run - 5 seconds mvn spring-boot:run - 5秒
  • java -jar myapp.jar - 25 seconds java -jar myapp.jar - 25秒

Starting the jar file takes like 5 times longer. 启动jar文件需要5倍的时间。 Why is it so? 为什么会这样? What is happening in the background when starting the application with Maven and as jar? 使用Maven和jar启动应用程序时后台发生了什么? Is something loaded differently? 是不是加载了什么? And is it possible to start the jar file for the time used by Maven? 是否有可能在Maven使用的时间内启动jar文件?

This difference could be due to the fact that spring-boot:run is actually running the Spring Boot application inside Maven JVM (Unless you explicitly set the fork argument), When it does run inside Maven JVM it is actually running as a new Thread not a Process. 这种差异可能是因为spring-boot:run实际上是在Maven JVM中运行Spring Boot应用程序(除非你明确设置了fork参数),当它在Maven JVM中运行时它实际上是作为一个新线程运行而不是一个过程。 Creating a Thread is much faster than a Process. 创建线程比进程快得多。

But java -jar command will create a OS Process, Process creation has steps attached to it like requesting a process id, allocating memory, etc. On top of that the myapp.jar will need to extracted plus the JVM will not have any optimization as it will be reading the .class extracted from the jar file for the first time. java -jar命令将创建一个OS进程,进程创建有附加的步骤,如请求进程ID,分配内存等。最重要的是myapp.jar将需要提取加上JVM将不会有任何优化它将首次读取从jar文件中提取的.class Basically a Cold Start which takes time. 基本上冷启动需要时间。

You can see the source code for Maven Spring Boot Plugin here 您可以在此处查看Maven Spring Boot插件的源代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM