1.查看jdk1.8 默认jvm参数
java -XX:+PrintCommandLineFlags -version
内存大小相关的选项
-Xms 设置初始堆的大小,也是最小堆的大小,它等价于:-XX:InitialHeapSize
-Xmx 设置最大堆的大小,它等价于-XX:MaxHeapSize。
比如,下面这条命令就是设置堆的初始值为128m,最大值为2g。
java -Xms128m -Xmx2g MyApp
如果堆的初始值和最大值不一样的话,JVM会根据程序的运行情况,自动调整堆的大小,这可能会影响到一些效率。针对服务端程序,一般是把堆的最小值和最大值设置为一样来避免堆扩展和收缩对性能的影响。
他们对应的JVM参数如下
新生代(别名) | 老年代 | JVM 参数 |
Serial (DefNew) | Serial Old(PSOldGen) | -XX:+UseSerialGC |
Parallel Scavenge (PSYoungGen) | Serial Old(PSOldGen) | -XX:+UseParallelGC |
Parallel Scavenge (PSYoungGen) | Parallel Old (ParOldGen) | -XX:+UseParallelOldGC |
ParNew (ParNew) | Serial Old(PSOldGen) | -XX:-UseParNewGC |
ParNew (ParNew) | CMS+Serial Old(PSOldGen) | -XX:+UseConcMarkSweepGC |
G1 | G1 | -XX:+UseG1GC |
2.start.sh
#!/bin/bash # START Java #set -o errexit set -o nounset #The vars can be used #-------------------------- # $def_app_id # $def_app_name # $def_app_domain # $def_app_deploy_path # $def_path_app_log # $def_path_app_data # $def_group_id # $def_instance_id # $def_instance_name # $def_instance_path # $def_host_ip #-------------------------- # echo ------------------------------------------- echo start server echo ------------------------------------------- # 设置项目代码路径 export CODE_HOME="/export/App" #日志路径 export LOG_PATH="/export/Logs/application" mkdir -p $LOG_PATH mkdir -p $CODE_HOME # 设置依赖路径 export CLASSPATH="$CODE_HOME/classes:$CODE_HOME/lib/*" # java可执行文件位置 export _EXECJAVA="$JAVA_HOME/bin/java" # JVM启动参数 export JAVA_ERR_OPTS="-XX:ErrorFile=$LOG_PATH/hs_err_pid_%p.log -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$LOG_PATH " export JAVA_COMPILE="-XX:CompileCommand=exclude,com/mysql/cj/NativeSession::execSQL " export JAVA_OPTS="-server -Xms4096m -Xmx4096m -Xss256k -XX:MaxDirectMemorySize=256m $JAVA_ERR_OPTS $JAVA_COMPILE $JAVA_OPTS " # 启动类 export MAIN_CLASS="com.xq.Application" echo Using JAVA_HOME: "$JAVA_HOME" echo Using CLASSPATH: "$CLASSPATH" echo Using JAVA_OPTS: "$JAVA_OPTS" echo Using MAIN_CLASS: "$MAIN_CLASS" echo ------------------------------------------- export instance_pattern="com.xq.Application" function check_instance { # 注意此处instance_pattern不要只写应用名,会把系统启动脚本也杀掉的, >/dev/null pgrep -lf "$instance_pattern" } function start_instance { local -i retry=0 if check_instance; then echo "ERROR: BoardSchedulerApplication instance process has already been started" >&2 exit 1 fi setsid $_EXECJAVA $JAVA_OPTS -classpath $CLASSPATH $MAIN_CLASS >& /dev/null & sleep 1 while true; do if check_instance; then echo "Application Instance started successfully" break elif (( retry == 20 ));then echo "ERROR: starting up Application instance has timed out" >&2 exit 1 else echo -n "." sleep 0.5 retry=$(( $retry + 1 )) fi done } start_instance
3.stop.sh
#!/bin/bash # STOP Java set -o nounset #The vars can be used #-------------------------- # $def_app_id # $def_app_name # $def_app_domain # $def_app_deploy_path # $def_path_app_log # $def_path_app_data # $def_group_id # $def_instance_id # $def_instance_name # $def_instance_path # $def_host_ip #-------------------------- # export instance_pattern="Application" function check_instance { # 注意此处instance_pattern不要只写应用名,会把系统启动脚本也杀掉的 pgrep -lf "$instance_pattern" >/dev/null } function stop_instance { local -i retry=0 if ! check_instance; then echo "WARNING: Application Application instance process not found, nothing to stop" >&2 exit 0 fi # 注意此处instance_pattern不要只写应用名,会把系统启动脚本也杀掉的 pkill -f "$instance_pattern" while (( retry < 20 )); do if ! check_instance; then echo "Application Application Instance stopped successfully" return else echo -n "." sleep 0.5 retry=$(( $retry + 1 )) fi done echo "ERROR: Fashion instance process still alive, sending SIGKILL ..." >&2 # 注意此处instance_pattern不要只写应用名,会把系统启动脚本也杀掉的 pkill -9 -f "$instance_pattern" exit $? } stop_instance