这个项目启动大概在30分钟,目标是通过调整jdk参数来提高程序的一个启动时间
@echo off
title [APP_NAME] Server
::Optional values: prod|dev.
::prod:Production Environment; dev:Development Environment, Remote debugging is enabled. See the SERVER_MODE parameter
set SERVER_MODE=dev
::Optional values: true|false. true:logs are printed on the console; false:Logs are not printed at the console.
if {%SERVER_MODE%}=={prod} (
set ENABLE_CONSOLE_LOGGING=false
) else (
set ENABLE_CONSOLE_LOGGING=true
)
::The default value of APP_SERVER_PATH is SERVER. You can change it to another directory name, you should keep the following variable value matching with the actual directory name. Jstack is compatible with the old version, please ignore it.
set APP_SERVER_PATH=server
set APP_SERVER_HOME=%~dp0%APP_SERVER_PATH%
if not exist "%APP_SERVER_HOME%" (
set APP_SERVER_PATH=jstack
set APP_SERVER_HOME=%~dp0jstack
)
::The following JAVA_HOME points to the APP JDK by default; If system environment variables are used, comment out the following line; For JDKs in other locations, change the following JAVA_HOME value.
set JAVA_HOME=%APP_SERVER_HOME%\runtime\java\%PROCESSOR_ARCHITECTURE%-win
::JVM_MEMORY_OPTS is used to specify the memory parameters that the Java virtual machine can use. By default, the appropriate memory is automatically calculated based on the physical machine environment. If you need to specify a fixed value, uncomment the downward comment and change the corresponding value
REM set JVM_MEMORY_OPTS=-Xmx3350M
::Change the following line to adjust the JVM debugging parameters
if not defined DEBUG_PORT (
set DEBUG_PORT=5006
)
set JVM_DEBUG_OPTS=-Dspring.profiles.active=dev,lsvo -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006
if not exist "%JAVA_HOME%" (
echo Invalid JAVA_HOME Path: "%JAVA_HOME%"
pause
exit
)
REM echo JAVA_HOME: %JAVA_HOME%
REM echo APP_SERVER_HOME: %APP_SERVER_HOME%
for /f "delims=" %%a in ('wmic os get TotalVisibleMemorySize /value^|find "="') do set %%a
set /a MemorySize=%TotalVisibleMemorySize% /1024
set TotalMemorySize=%MemorySize%M
@setlocal enabledelayedexpansion
for /r "%APP_SERVER_HOME%\runtime\" %%k in (app-bootstrap*.jar) do (
@set APP_BOOTSTRAP="%%k"
)
::APP_Runtime
@set APP_MODULE_PATHS="%APP_SERVER_HOME%\runtime\3rd","%APP_SERVER_HOME%\runtime\libs"
:: Total Memory Size
set /a totalMem=16777216+1
for /f "skip=1" %%i in ('wmic os get TotalVisibleMemorySize') do (
if %%i geq 0 set totalMem=%%i
)
set /a memorySize=%totalMem%/1024
::if total size less than 16G,then XmxSize=totalSize*6/10
set /a totalMem=%memorySize%/10*6
set XmxSize=%totalMem%M
if not defined JVM_MEM_OPTS (
if %memorySize% leq 16384 set JVM_MEM_OPTS=-Xmx%XmxSize%
)
if {%SERVER_MODE%}=={prod} (
set JVM_DEBUG_OPTS=-Dspring.profiles.active=prod,lsvo
)
Set REBEL_HOME=[REDACTED_PATH]
for /f "tokens=*" %%a in ('powershell -Command "(Get-CimInstance -ClassName Win32_Processor).NumberOfCores"') do set CPU_CORES=%%a
:: 计算并行线程数(核心数-1)
set /a PARALLEL_THREADS=23
:: 设置JVM参数
set JVM_GC_OPTS=-XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -XX:MaxGCPauseMillis=3000 -XX:G1NewSizePercent=40 -XX:G1MaxNewSizePercent=60 -XX:+DisableExplicitGC -XX:ParallelGCThreads=%PARALLEL_THREADS% -XX:ConcGCThreads=%PARALLEL_THREADS%
::Dparallel.startup=true 并行启动部署信息:
"%JAVA_HOME%\bin\java" %APP_OPS% %JVM_GC_OPTS% -server -Ddebug=true -Dloader.path=%APP_MODULE_PATHS% -Dparallel.startup=true -Dserver.runtime.path.name=%APP_SERVER_PATH% %JVM_MEM_OPTS% %JVM_DEBUG_OPTS% -jar %APP_BOOTSTRAP% --spring.config.location="%APP_SERVER_HOME%/runtime/"
pause
::-Dparallel.startup=true
wmic os get TotalVisibleMemorySize
获取系统实际可用内存这些优化措施共同作用,显著提升了应用的启动速度和运行性能。通过动态内存管理、优化的垃圾收集策略和并行处理,使应用能够更高效地利用系统资源。
在Windows环境下,可以通过设置进程优先级来优化应用性能:
开发/测试环境:
start /HIGH
命令启动应用生产环境:
特殊情况: