Skip to content

Commit

Permalink
#44 vjstar,修正JDK参数设定脚本,添加注释与小修改
Browse files Browse the repository at this point in the history
  • Loading branch information
calvin1978 committed Jun 21, 2018
1 parent d3d864c commit ae1555f
Showing 1 changed file with 53 additions and 22 deletions.
75 changes: 53 additions & 22 deletions vjstar/src/main/script/jvm-options.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@

#!/bin/bash

# change it here
## Only for JDK7/JDK8

# change the dir here
LOGDIR="./logs"

JAVA_VERSION=$(java -version 2>&1 | awk -F '"' '/version/ {print $2}')
Expand All @@ -14,37 +16,56 @@ ulimit -c unlimited

MEM_OPTS="-Xms4g -Xmx4g -XX:NewRatio=1"

MEM_OPTS="$MEM_OPTS -XX:+AlwaysPreTouch"


if [[ "$JAVA_VERSION" < "1.8" ]]; then
MEM_OPTS="$MEM_OPTS -XX:PermSize=256m -XX:MaxPermSize=512m"
MEM_OPTS="$MEM_OPTS -XX:PermSize=128m -XX:MaxPermSize=512m"
else
MEM_OPTS="$MEM_OPTS -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=512m"
MEM_OPTS="$MEM_OPTS -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=512m"
fi

# 启动时预申请内存
MEM_OPTS="$MEM_OPTS -XX:+AlwaysPreTouch"

# 如果线程数较多,函数的递归较少,可以调成256k节约内存
#MEM_OPTS="$MEM_OPTS -Xss256k"

#MEM_OPTS_="$MEM_OPTS -XX:ReservedCodeCacheSize=240M"
# 堆外内存的最大值默认约等于堆大小,可以显式将其设小,获得一个比较清晰的内存总量预估
#MEM_OPTS="$MEM_OPTS -XX:MaxDirectMemorySize=2g"

# 根据VJTop的观察,调整JIT 二进制代码区大小
#MEM_OPTS="$MEM_OPTS -XX:ReservedCodeCacheSize=240M"


## GC Options ##

GC_OPTS="-XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:MaxTenuringThreshold=4 -XX:+UseCMSInitiatingOccupancyOnly"
GC_OPTS="$GC_OPTS -XX:+ExplicitGCInvokesConcurrent -XX:+ParallelRefProcEnabled -XX:+CMSParallelInitialMarkEnabled"
GC_OPTS="$GC_OPTS -XX:+UnlockDiagnosticVMOptions -XX:ParGCCardsPerStrideChunk=1024"
GC_OPTS="-XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly"

# Syste.gc() 使用CMS算法
GC_OPTS="$GC_OPTS -XX:+ExplicitGCInvokesConcurrent"

# CMS中的下列阶段并发执行
GC_OPTS="$GC_OPTS -XX:+ParallelRefProcEnabled -XX:+CMSParallelInitialMarkEnabled"

# 根据应用的对象生命周期设定,减少实际老生代对象在新生代停留时间能加快YGC速度
GC_OPTS=" -XX:MaxTenuringThreshold=3"

# 如果堆较大,加大YGC时扫描OldGen关联的卡片,加快YGC速度,默认值256较低
GC_OPTS="$GC_OPTS -XX:+UnlockDiagnosticVMOptions -XX:ParGCCardsPerStrideChunk=1024"

# 如果机器上有其他较繁忙的进程在运行时,将GC线程数设置得比默认值更低,另建议CMS GC线程数简单改为YGC线程数一半
#GC_OPTS="$GC_OPTS -XX:ParallelGCThreads=16 -XX:ConcGCThreads=8"

# 如果CMS GC时间很长,并且明显受新生代存活对象数量影响时打开
#GC_OPTS="$GC_OPTS -XX:+CMSScavengeBeforeRemark"

# 如果永久代不会增长,关闭ClassUnloading降低CMS出现缓慢的几率
#if [[ "$JAVA_VERSION" > "1.8" ]]; then
# GC_OPTS="$GC_OPTS -XX:-CMSClassUnloadingEnabled"
#fi


## GC log Options ##

# change the name here
if [ -d /dev/shm/ ]; then
GC_LOG_FILE=/dev/shm/gc-myapp.log
else
Expand All @@ -53,50 +74,60 @@ fi


if [ -f ${GC_LOG_FILE} ]; then
# change the name here
GC_LOG_BACKUP = ${LOGDIR}/gc-myapp-$(date +'%Y%m%d_%H%M%S').log
echo "saving gc log ${GC_LOG_FILE} to ${GC_LOG_BACKUP}"
mv ${GC_LOG_FILE} ${GC_LOG_BACKUP}
fi

GCLOG_OPTS="-Xloggc:${GC_LOG_FILE} -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:+PrintGCApplicationStoppedTime -XX:+PrintPromotionFailure"
GCLOG_OPTS="-Xloggc:${GC_LOG_FILE} -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintPromotionFailure -XX:+PrintGCApplicationStoppedTime"


if [[ "$JAVA_VERSION" < "1.8" ]]; then
GCLOG_OPTS="$GCLOG_OPTS -XX:+PrintGCCause"
fi

# 打印安全点日志
#GCLOG_OPTS="$GCLOG_OPTS -XX:+PrintSafepointStatistics -XX:PrintSafepointStatisticsCount=1"


## Performance Options##
## Optimization Options##

PERFORMANCE_OPTS=" -XX:-UseBiasedLocking -XX:AutoBoxCacheMax=20000 -Djava.security.egd=file:/dev/./urandom"
OPTIMIZE_OPTS="-XX:-UseBiasedLocking -XX:AutoBoxCacheMax=20000 -Djava.security.egd=file:/dev/./urandom"

#PERFORMANCE_OPTS="$PERFORMANCE_OPTS -XX:-UseCounterDecay"
# 如果希望最终JIT所有函数,无论热度如何。
#OPTIMIZE_OPTS="$OPTIMIZE_OPTS -XX:-UseCounterDecay"

#PERFORMANCE_OPTS="$PERFORMANCE_OPTS -XX:+PerfDisableSharedMem"
# 关闭PerfData写入,避免GC时因为高IO导致写PerfData文件被锁定,但会导致jstats,jps不能使用。
#OPTIMIZE_OPTS="$OPTIMIZE_OPTS -XX:+PerfDisableSharedMem"

# 关闭多层编译,减少应用刚启动时的JIT导致的可能超时,以及避免部分函数C1编译后,最终没被C2编译。 但导致函数没有被初始C1编译。
#if [[ "$JAVA_VERSION" > "1.8" ]]; then
# PERFORMANCE_OPTS="$PERFORMANCE_OPTS -XX:-TieredCompilation"
# OPTIMIZE_OPTS="$OPTIMIZE_OPTS -XX:-TieredCompilation"
#fi


## Trouble shotting ##
## Trouble shotting Options##

SHOTTING_OPTS="-XX:+PrintCommandLineFlags -XX:-OmitStackTraceInFastThrow -XX:ErrorFile=${LOGDIR}/hs_err_%p.log"


#OOM 时进行HeapDump,但此时会产生较高的IO,有可能会影响他的容器
#SHOTTING_OPTS="$SHOTTING_OPTS -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${LOGDIR}/"


## JMX ##
## JMX Options##

JMX_OPTS="-Djava.rmi.server.hostname=127.0.0.1 -Dcom.sun.management.jmxremote.port=7001 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false"


## Other ##
## Other Options##

OTHER_OPTS="-Djava.net.preferIPv4Stack=true"
OTHER_OPTS="-Djava.net.preferIPv4Stack=true -Djava.awt.headless=true"


## All together ##

JAVA_OTPS="$MEM_OPTS $GC_OPTS $GCLOG_OPTS $PERFORMANCE_OPTS $SHOTTING_OPTS $JMX_OPTS $OTHER_OPTS"
JAVA_OPTS="$MEM_OPTS $GC_OPTS $GCLOG_OPTS $OPTIMIZE_OPTS $SHOTTING_OPTS $JMX_OPTS $OTHER_OPTS"

echo $JAVA_OTPS
echo JAVA_OPTS=$JAVA_OPTS

0 comments on commit ae1555f

Please sign in to comment.