This is an automated email from the ASF dual-hosted git repository.

jianbin pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/incubator-seata.git


The following commit(s) were added to refs/heads/2.x by this push:
     new a72d2651c7 feature: Automatically calculate the values for JVM 
parameters (#7872)
a72d2651c7 is described below

commit a72d2651c728e8dca5d6c05c90fc32ee8998f13f
Author: legendpei <[email protected]>
AuthorDate: Thu Dec 18 17:14:46 2025 +0800

    feature: Automatically calculate the values for JVM parameters (#7872)
---
 changes/en-us/2.x.md              |  2 +-
 changes/zh-cn/2.x.md              |  1 +
 distribution/bin/seata-server.bat |  6 +++-
 distribution/bin/seata-setup.sh   | 62 ++++++++++++++++++++++++++++++++++-----
 4 files changed, 61 insertions(+), 10 deletions(-)

diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md
index 70e60109d2..dc936922ae 100644
--- a/changes/en-us/2.x.md
+++ b/changes/en-us/2.x.md
@@ -32,7 +32,7 @@ Add changes here for all PR submitted to the 2.x branch.
 - [[#7851](https://github.com/apache/incubator-seata/pull/7851)] add support 
for managing transaction groups
 - [[#7857](https://github.com/apache/incubator-seata/pull/7857)] support 
displaying cluster information in the console
 - [[#7826](https://github.com/apache/incubator-seata/pull/7826)] Support 
HTTP/2 response handling for the Watch API in Server Raft mode
-
+- [[#7872](https://github.com/apache/incubator-seata/pull/7872)] Automatically 
calculate the values for JVM parameters
 
 
 ### bugfix:
diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md
index 376f6b7425..54eaeabd84 100644
--- a/changes/zh-cn/2.x.md
+++ b/changes/zh-cn/2.x.md
@@ -32,6 +32,7 @@
 - [[#7851](https://github.com/apache/incubator-seata/pull/7851)] 控制台支持事务分组管理能力
 - [[#7857](https://github.com/apache/incubator-seata/pull/7857)] 控制台支持集群信息展示
 - [[#7826](https://github.com/apache/incubator-seata/pull/7826)] 在 Server Raft 
模式下,为 Watch API 提供对 HTTP/2 响应处理的支持
+- [[#7872](https://github.com/apache/incubator-seata/pull/7872)] 根据当前内存值自动计算 
JVM 参数
 
 
 ### bugfix:
diff --git a/distribution/bin/seata-server.bat 
b/distribution/bin/seata-server.bat
index f0fb66983b..81114fab02 100644
--- a/distribution/bin/seata-server.bat
+++ b/distribution/bin/seata-server.bat
@@ -73,6 +73,10 @@ if "%JAVACMD%"=="" set JAVACMD=java
 
 if "%REPO%"=="" set REPO=%BASEDIR%\lib
 
+if not defined JVM_XMX set JVM_XMX=2048m
+
+if not defined JVM_XMS set JVM_XMS=2048m
+
 set CLASSPATH="%BASEDIR%"\conf;"%REPO%"\*
 
 set ENDORSED_DIR=
@@ -109,7 +113,7 @@ if "%JMX_ENABLE%"=="true" (
   echo "JMX disabled"
 )
 
-%JAVACMD% %JAVA_OPTS% %SKYWALKING_OPTS% %JMX_OPTS% -server 
-Dloader.path="%BASEDIR%"/lib -Xmx2048m -Xms2048m -Xss512k -XX:SurvivorRatio=10 
-XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=256m -XX:MaxDirectMemorySize=1024m 
-XX:-OmitStackTraceInFastThrow -XX:-UseAdaptiveSizePolicy 
-XX:+HeapDumpOnOutOfMemoryError 
-XX:HeapDumpPath="%BASEDIR%"/logs/java_heapdump.hprof -XX:+DisableExplicitGC 
-Xloggc:"%BASEDIR%"/logs/seata_gc.log -verbose:gc 
-Dio.netty.leakDetectionLevel=advanced -classpath %CLASSP [...]
+%JAVACMD% %JAVA_OPTS% %SKYWALKING_OPTS% %JMX_OPTS% -server 
-Dloader.path="%BASEDIR%"/lib -Xmx%JVM_XMX% -Xms%JVM_XMS% -Xss512k 
-XX:SurvivorRatio=10 -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=256m 
-XX:MaxDirectMemorySize=1024m -XX:-OmitStackTraceInFastThrow 
-XX:-UseAdaptiveSizePolicy -XX:+HeapDumpOnOutOfMemoryError 
-XX:HeapDumpPath="%BASEDIR%"/logs/java_heapdump.hprof -XX:+DisableExplicitGC 
-Xloggc:"%BASEDIR%"/logs/seata_gc.log -verbose:gc 
-Dio.netty.leakDetectionLevel=advanced -classpath [...]
 if %ERRORLEVEL% NEQ 0 goto error
 goto end
 
diff --git a/distribution/bin/seata-setup.sh b/distribution/bin/seata-setup.sh
index a391f172d8..91f311512c 100644
--- a/distribution/bin/seata-setup.sh
+++ b/distribution/bin/seata-setup.sh
@@ -124,19 +124,65 @@ if [ "$SKYWALKING_ENABLE" = "true" ]; then
 else
   echo "apm-skywalking not enabled"
 fi
-JVM_XMX=$JVM_XMX
-JVM_XMS=$JVM_XMS
-JVM_XSS=$JVM_XSS
-JVM_MetaspaceSize=$JVM_MetaspaceSize
-JVM_MaxMetaspaceSize=$JVM_MaxMetaspaceSize
-JVM_MaxDirectMemorySize=$JVM_MaxDirectMemorySize
+
+# auto JVM Memory Calculation
+AVAILABLE_MEM_BYTES=""
+
+# cgroup v1
+if [ -f /sys/fs/cgroup/memory/memory.limit_in_bytes ]; then
+  CGROUP_LIMIT=$(cat /sys/fs/cgroup/memory/memory.limit_in_bytes 2>/dev/null)
+  if [ "$CGROUP_LIMIT" != "9223372036854771712" ] && [ "$CGROUP_LIMIT" -gt 
104857600 ]; then
+    AVAILABLE_MEM_BYTES=$CGROUP_LIMIT
+  fi
+# cgroup v2
+elif [ -f /sys/fs/cgroup/memory.max ]; then
+  CGROUP_LIMIT=$(cat /sys/fs/cgroup/memory.max 2>/dev/null)
+  if [ "$CGROUP_LIMIT" != "max" ] && [ "$CGROUP_LIMIT" -gt 104857600 ]; then
+    AVAILABLE_MEM_BYTES=$CGROUP_LIMIT
+  fi
+fi
+
+# for VMs / bare-metal
+if [ -z "$AVAILABLE_MEM_BYTES" ]; then
+  if command -v free >/dev/null 2>&1; then
+    AVAILABLE_MEM_BYTES=$(free -b | awk '/Mem:/ {print $2}')
+  elif [ -f /proc/meminfo ]; then
+    AVAILABLE_MEM_BYTES=$(awk '/MemTotal/ {print $2 * 1024}' /proc/meminfo)
+  fi
+fi
+
+# auto set JVM_XMX/JVM_XMS if not provided by user
+if [ -n "$AVAILABLE_MEM_BYTES" ] && [ -z "$JVM_XMX" ]; then
+  TOTAL_MEM_MB=$(( AVAILABLE_MEM_BYTES / 1024 / 1024 ))
+  if [ "$TOTAL_MEM_MB" -le 4096 ]; then
+    HEAP_PCT=70
+  else
+    HEAP_PCT=75
+  fi
+  HEAP_MB=$(( TOTAL_MEM_MB * HEAP_PCT / 100 ))
+  # minimum heap
+  [ "$HEAP_MB" -lt 256 ] && HEAP_MB=256
+
+  JVM_XMX="${HEAP_MB}m"
+  JVM_XMS="${HEAP_MB}m"
+  echo "[INFO] Auto set JVM heap to ${HEAP_MB}m (total memory: 
${TOTAL_MEM_MB}MB, heap%: ${HEAP_PCT}%)"
+fi
+
+# final fallback
+JVM_XMX=${JVM_XMX:-"2048m"}
+JVM_XMS=${JVM_XMS:-"2048m"}
+JVM_XSS=${JVM_XSS:-"640k"}
+JVM_MetaspaceSize=${JVM_MetaspaceSize:-"128m"}
+JVM_MaxMetaspaceSize=${JVM_MaxMetaspaceSize:-"256m"}
+JVM_MaxDirectMemorySize=${JVM_MaxDirectMemorySize:-"1024m"}
+
 LOADER_PATH=$LOADER_PATH
 LOG_HOME=$LOG_HOME
 if [ -z "$LOG_HOME" ]; then
     LOG_HOME="$HOME/logs/seata"
     mkdir -p $LOG_HOME
 fi
-JAVA_OPT="${JAVA_OPT} -Dlog.home=${LOG_HOME} -server 
-Dloader.path=${LOADER_PATH:="$BASEDIR/lib"} -Xmx${JVM_XMX:="2048m"} 
-Xms${JVM_XMS:="2048m"} -Xss${JVM_XSS:="640k"} -XX:SurvivorRatio=10 
-XX:MetaspaceSize=${JVM_MetaspaceSize:="128m"} 
-XX:MaxMetaspaceSize=${JVM_MaxMetaspaceSize:="256m"} 
-XX:MaxDirectMemorySize=${JVM_MaxDirectMemorySize:=1024m} 
-XX:-OmitStackTraceInFastThrow -XX:-UseAdaptiveSizePolicy"
+JAVA_OPT="${JAVA_OPT} -Dlog.home=${LOG_HOME} -server 
-Dloader.path=${LOADER_PATH:="$BASEDIR/lib"} -Xmx${JVM_XMX} -Xms${JVM_XMS} 
-Xss${JVM_XSS} -XX:SurvivorRatio=10 -XX:MetaspaceSize=${JVM_MetaspaceSize} 
-XX:MaxMetaspaceSize=${JVM_MaxMetaspaceSize} 
-XX:MaxDirectMemorySize=${JVM_MaxDirectMemorySize} 
-XX:-OmitStackTraceInFastThrow -XX:-UseAdaptiveSizePolicy"
 JAVA_OPT="${JAVA_OPT} -XX:+HeapDumpOnOutOfMemoryError 
-XX:HeapDumpPath=${LOG_HOME}/java_heapdump.hprof -XX:+DisableExplicitGC"
 
 JAVA_MAJOR_VERSION=$($JAVACMD -version 2>&1 | sed '1!d' | sed -e 's/"//g' | 
awk '{print $3}' | awk -F '.' '{print $1}')
@@ -170,4 +216,4 @@ JAVA_OPT="${JAVA_OPT} ${JMX_OPTS}"
 
 if [ ! -x "$BASEDIR"/logs ]; then
   mkdir "$BASEDIR"/logs
-fi
+fi
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to