


Operations and maintenance asked me to optimize the SpringBoot startup speed, and this is what I did!
Spring Boot is undoubtedly the largest framework for Java back-end development. Based on Spring Boot, it has a complete tool chain and various starters. For daily business development, it can be said that the wheels are complete.
However, with the popularity of microservices and cloud native era, Spring Boot applications have exposed some problems, the most prominent of which are:
- Slow startup
- The application takes up too much memory
- Cloud native applications have relatively high requirements for startup speed. When horizontal expansion is required, these new instances must be started within a short enough time to process new requests as quickly as possible.
- #Cloud-native applications require as few resources as possible during runtime. Reducing the resources occupied by a single instance as much as possible means that more access requests can be supported at the same cost.
- #Cloud native applications require smaller packaging volumes. Cloud native applications are packaged in the form of container images. The larger the size of the application image, the larger the storage space required, and the longer it takes to push and pull the image.
In fact, we all know that most of the startup time is due to Spring's need to load various beans, resulting in a decrease in startup speed
1. Delayed initialization of Bean
Generally, there are many time-consuming tasks in SpringBoot, such as establishing a database connection, creating an initial thread pool, etc. We can delay the initialization of these operations to optimize the startup speed. Purpose. The
spring.main.lazy-initialization property was introduced after Spring Boot version 2.2. If configured to true, all beans will be lazily initialized.
spring:main:lazy-initialization: true
After the delayed initialization is turned on locally, the startup can be 1~2 seconds faster.
environment |
Configuration |
(ten times average) startup speed |
springboot2 jdk1.8 |
##≈10.3s | |
Lazy initialization Bean | ≈8.63s |
二、创建扫描索引
Spring5 之后提供了spring-context-indexer功能,可以通过在编译时创建一个静态候选列表来提高大型应用程序的启动性能。
先看官方的解释:
在项目中使用了@Indexed之后,编译打包的时候会在项目中自动生成META-INT/spring.components文件。
当Spring应用上下文执行ComponentScan扫描时,META-INT/spring.components将会被CandidateComponentsIndexLoader 读取并加载,转换为CandidateComponentsIndex对象,这样的话@ComponentScan不在扫描指定的package,而是读取CandidateComponentsIndex对象,从而达到提升性能的目的.
我们只需要将依赖引入,然后在启动类上使用@Indexed注解即可。这样在程序编译打包之后会生成
META-INT/spring.components文件,当执行@ComponentScan扫描类时,会读取索引文件,提高扫描速度。
<dependency> <groupid>org.springframework</groupid> <artifactid>spring-context-indexer</artifactid> <optional>true</optional></dependency>
@Indexed@SpringBootApplicationpublic class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}
环境 |
配置 |
(十次平均值)启动速度 |
springboot2+jdk1.8 |
≈10.3s |
|
+延迟初始化Bean |
≈8.63s |
|
+创建扫描索引 |
≈7.7s |
Other tips:
1. Reduce the scope of @ComponentScan @SpringBootApplication when scanning classes
2. Turn off Spring Boot’s JMX monitoring , set spring.jmx.enabled=false
3. Set JVM parameters -noverify, do not verify classes
4. Delay loading of beans that are not loaded at startup 5. Use Spring Boot's global lazy loading 1
5. Try not to use annotations for AOPQ aspects, which will cause all methods to be scanned at startup 7. Turn off some endpoint monitoring functions
6. Exclude redundant projects Rely on jar
7. When scanning interfaces with swagger, specify to only scan classes under a certain path. 10. Scanning of Feign client interface reduces the scope of package scanning.
At this point, the startup speed should be considered optimized. It's quite extreme, but large memory usage is still a problem
3. Upgrade jdk17
Of course jdk has also made great efforts in this regard:
Memory The main reason for the high occupation is that the memory will not be returned to the operating system after it is occupied. This is gradually improving:
- G1 JDK12 and later has been supported
- ZGC JDK13 and later has supported
based on the characteristics of the Java language and Spring Boot Some implementation methods determine that even if the unused memory of G1/ZGC is turned on and returned to the operating system in time, the memory usage of Spring Boot is still much larger than that of compiled languages such as Golang.
Therefore, if Java wants to solve the problems in the cloud native era, the current solutions are basically based on GraalVM, whether it is Quarkus or Micronaut.
So, does Spring Boot have a similar solution? :spring-graalvm-native
4. Upgrade SpringBoot3spring-graalvm-native is a very important feature of springBoo6/SpringBoot3. It supports Using GraalVM to compile SpringBoot applications into local executable image files can significantly improve startup speed, peak performance, and reduce memory usage.
The above is the detailed content of Operations and maintenance asked me to optimize the SpringBoot startup speed, and this is what I did!. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











For mechanical hard drives or SATA solid-state drives, you will feel the increase in software running speed. If it is an NVME hard drive, you may not feel it. 1. Import the registry into the desktop and create a new text document, copy and paste the following content, save it as 1.reg, then right-click to merge and restart the computer. WindowsRegistryEditorVersion5.00[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SessionManager\MemoryManagement]"DisablePagingExecutive"=d

Recently, Xiaomi released a powerful high-end smartphone Xiaomi 14Pro, which not only has a stylish design, but also has internal and external black technology. The phone has top performance and excellent multitasking capabilities, allowing users to enjoy a fast and smooth mobile phone experience. However, performance will also be affected by memory. Many users want to know how to check the memory usage of Xiaomi 14Pro, so let’s take a look. How to check memory usage on Xiaomi Mi 14Pro? Introduction to how to check the memory usage of Xiaomi 14Pro. Open the [Application Management] button in [Settings] of Xiaomi 14Pro phone. To view the list of all installed apps, browse the list and find the app you want to view, click on it to enter the app details page. In the application details page

When novice users buy a computer, they will be curious about the difference between 8g and 16g computer memory? Should I choose 8g or 16g? In response to this problem, today the editor will explain it to you in detail. Is there a big difference between 8g and 16g of computer memory? 1. For ordinary families or ordinary work, 8G running memory can meet the requirements, so there is not much difference between 8g and 16g during use. 2. When used by game enthusiasts, currently large-scale games basically start at 6g, and 8g is the minimum standard. Currently, when the screen is 2k, higher resolution will not bring higher frame rate performance, so there is no big difference between 8g and 16g. 3. For audio and video editing users, there will be obvious differences between 8g and 16g.

According to news from this website on September 3, Korean media etnews reported yesterday (local time) that Samsung Electronics and SK Hynix’s “HBM-like” stacked structure mobile memory products will be commercialized after 2026. Sources said that the two Korean memory giants regard stacked mobile memory as an important source of future revenue and plan to expand "HBM-like memory" to smartphones, tablets and laptops to provide power for end-side AI. According to previous reports on this site, Samsung Electronics’ product is called LPWide I/O memory, and SK Hynix calls this technology VFO. The two companies have used roughly the same technical route, which is to combine fan-out packaging and vertical channels. Samsung Electronics’ LPWide I/O memory has a bit width of 512

According to the report, Samsung Electronics executive Dae Woo Kim said that at the 2024 Korean Microelectronics and Packaging Society Annual Meeting, Samsung Electronics will complete the verification of the 16-layer hybrid bonding HBM memory technology. It is reported that this technology has passed technical verification. The report also stated that this technical verification will lay the foundation for the development of the memory market in the next few years. DaeWooKim said that Samsung Electronics has successfully manufactured a 16-layer stacked HBM3 memory based on hybrid bonding technology. The memory sample works normally. In the future, the 16-layer stacked hybrid bonding technology will be used for mass production of HBM4 memory. ▲Image source TheElec, same as below. Compared with the existing bonding process, hybrid bonding does not need to add bumps between DRAM memory layers, but directly connects the upper and lower layers copper to copper.

This site reported on March 21 that Micron held a conference call after releasing its quarterly financial report. At the conference, Micron CEO Sanjay Mehrotra said that compared to traditional memory, HBM consumes significantly more wafers. Micron said that when producing the same capacity at the same node, the current most advanced HBM3E memory consumes three times more wafers than standard DDR5, and it is expected that as performance improves and packaging complexity intensifies, in the future HBM4 This ratio will further increase. Referring to previous reports on this site, this high ratio is partly due to HBM’s low yield rate. HBM memory is stacked with multi-layer DRAM memory TSV connections. A problem with one layer means that the entire

If you have installed new RAM but it is not showing up on your Windows computer, this article will help you resolve the issue. Usually, we improve the performance of the system by upgrading RAM. However, system performance also depends on other hardware such as CPU, SSD, etc. Upgrading RAM can also improve your gaming experience. Some users have noticed that installed memory is not showing up in Windows 11/10. If this happens to you, you can use the advice provided here. Installed RAM not showing up on Windows 11 If the installed RAM is not showing up on your Windows 11/10 PC, the following suggestions will help you. Is the installed memory compatible with your computer's motherboard? in BIO

According to news from this website on May 6, Lexar launched the Ares Wings of War series DDR57600CL36 overclocking memory. The 16GBx2 set will be available for pre-sale at 0:00 on May 7 with a deposit of 50 yuan, and the price is 1,299 yuan. Lexar Wings of War memory uses Hynix A-die memory chips, supports Intel XMP3.0, and provides the following two overclocking presets: 7600MT/s: CL36-46-46-961.4V8000MT/s: CL38-48-49 -1001.45V In terms of heat dissipation, this memory set is equipped with a 1.8mm thick all-aluminum heat dissipation vest and is equipped with PMIC's exclusive thermal conductive silicone grease pad. The memory uses 8 high-brightness LED beads and supports 13 RGB lighting modes.
