Java基础-JVM调优策略简介

               Java基础-JVM调优策略简介

                                  作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.JVM结构分析

1>.JVM结构图

Java基础-JVM调优策略简介

2>.JVM运行时数据区功能说明

  JVM管理的内存段可分为两大类:线程共享内存和线程私有内存。

  2.1:线程共享内存

    方法区:存储jvm加载class,常量,静态变量,即时编译器编译后的代码等。

    java堆:存储Java的所有对象实例,数组等;

  2.2:线程私有内存

    程序计数寄存器:每个线程有自己的计数存寄器,存储当前线程执行字节码的地址;

    jvm栈:jvm会为每个运行线程分配一个栈区,线程调用方法时和方法返回时和方法返回时会进行入栈和出栈操作;

    本地方法栈区:与jvm stack类似,只不过此区域是为调用本地方法服务。

  由于每个线程都有自己的私有内存,我们去一一调试他们的内存的话,很显然是不合适的。因此,我们重点关注的对象是线程共享内存的调优。

 

二.JVM进程的内存结构

Java基础-JVM调优策略简介

新生代:新创建的进程,通常指刚刚创建的对象
老年代:如果对象内存较大通常会放在老年代中,尽管它刚刚创建
持久代:不会被回收 伊甸区Eden:一个对象被创建后,通常被放在伊甸区中,当gc在清理eden时,会将整个eden清空,如果清空时发现eden区创建的对象仍然被引用就会将这些引用对象存入辛存区。 辛存区:
辛存区主要存储的是Eden清理时未被清理的对象,而辛存区又分为辛存一区(from)和辛存二区(to),当gc清理from时,如果对象没有被引用就会被清理,如果有引用就将其清理到辛存二区,你可以简单的理解是将from中仍然被引用的对象存入到to区,同理,当gc在清理辛存二区(to)时,如果依然存在被引用的对象,那么该对象就会被存入到老年 老年区:
主要存放新生代传来的对象,而老年代也是会有gc来清理的哟,只不过老年代的垃圾回收器(gc),即gc可能和新生代的不一致。

三.JAVA调优介绍

 1>.Categories of java hotspot VM options

  1.1:JavaStaveVM重新定义的标准选项在Java应用程序启动器页面上描述,用于Windows、Solaris和Linux;

  1.2:"-X"是非标准选项,其有以下两个显著特点:

    第一:不保证在所有VM实现上得到支持‘;

    第二:在JDK的后续版本中,如有更改,恕不另行通知;

  1.3:"-XX"选项不稳定,如有更改,恕不另行通知。

  1.4:jinfo -flags 指令

[root@yinzhengjie ~]# jps
Jps
Bootstrap
[root@yinzhengjie ~]#
[root@yinzhengjie ~]# jinfo -flags
Attaching to process ID , please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.131-b11
Non-default VM flags: -XX:CICompilerCount= -XX:InitialHeapSize= -XX:MaxHeapSize= -XX:MaxNewSize= -XX:MinHeapDeltaBytes= -XX:NewSize= -XX:OldSize= -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseFastUnorderedTimeStamps -XX:+UseParallelGC
Command line: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize= -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK= -Dignore.endorsed.dirs= -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp
[root@yinzhengjie ~]#

查看Java线程([root@yinzhengjie ~]# jinfo -flags 2635)

2>.Java堆大小内存选项

Java基础-JVM调优策略简介

堆内存调试的常用参数总结:
-xms:堆内存空间的初始大小
--XX:NewSize:新生代的初始空间大小
-Xmx:堆内存的最大值
--XX:MaxNewSize:新生代空间的最大值 --XX:PermSize:持久带的初始值
--XX:MaxPermSize:持久带的最大值
--XX:SurvivorRatio:在新生代内部Eden/survivor的值

 三.JVM调参案例展示

1>.查看tomcat的调优配置

[root@yinzhengjie ~]# head - /usr/local/tomcat/bin/catalina.sh
#!/bin/sh # Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License. # -----------------------------------------------------------------------------
# Control Script for the CATALINA Server
#
# Environment Variable Prerequisites
#
# Do not set the variables in this script. Instead put them into a script
# setenv.sh in CATALINA_BASE/bin to keep your customizations separate.
#
# CATALINA_HOME May point at your Catalina "build" directory.
#
# CATALINA_BASE (Optional) Base directory for resolving dynamic portions
# of a Catalina installation. If not present, resolves to
# the same directory that CATALINA_HOME points to.
#
# CATALINA_OUT (Optional) Full path to a file where stdout and stderr
# will be redirected.
# Default is $CATALINA_BASE/logs/catalina.out
#
# CATALINA_OPTS (Optional) Java runtime options used when the "start",
# "run" or "debug" command is executed.
# Include here and not in JAVA_OPTS all options, that should
# only be used by Tomcat itself, not by the stop process,
# the version command etc.
# Examples are heap size, GC logging, JMX ports etc.
#
# CATALINA_TMPDIR (Optional) Directory path location of temporary directory
# the JVM should use (java.io.tmpdir). Defaults to
# $CATALINA_BASE/temp.
#
# JAVA_HOME Must point at your Java Development Kit installation.
# Required to run the with the "debug" argument.
#
# JRE_HOME Must point at your Java Runtime installation.
# Defaults to JAVA_HOME if empty. If JRE_HOME and JAVA_HOME
# are both set, JRE_HOME is used.
#
# JAVA_OPTS (Optional) Java runtime options used when any command
# is executed.
# Include here and not in CATALINA_OPTS all options, that
# should be used by Tomcat and also by the stop process,
# the version command etc.
# Most options should go into CATALINA_OPTS.
#
# JAVA_ENDORSED_DIRS (Optional) Lists of of colon separated directories
# containing some jars in order to allow replacement of APIs
# created outside of the JCP (i.e. DOM and SAX from W3C).
# It can also be used to update the XML parser implementation.
# This is only supported for Java <= .
# Defaults to $CATALINA_HOME/endorsed.
#
# JPDA_TRANSPORT (Optional) JPDA transport used when the "jpda start"
# command is executed. The default is "dt_socket".
#
# JPDA_ADDRESS (Optional) Java runtime options used when the "jpda start"
# command is executed. The default is localhost:.
#
# JPDA_SUSPEND (Optional) Java runtime options used when the "jpda start"
# command is executed. Specifies whether JVM should suspend
# execution immediately after startup. Default is "n".
#
# JPDA_OPTS (Optional) Java runtime options used when the "jpda start"
# command is executed. If used, JPDA_TRANSPORT, JPDA_ADDRESS,
# and JPDA_SUSPEND are ignored. Thus, all required jpda
# options MUST be specified. The default is:
#
# -agentlib:jdwp=transport=$JPDA_TRANSPORT,
# address=$JPDA_ADDRESS,server=y,suspend=$JPDA_SUSPEND
#
# JSSE_OPTS (Optional) Java runtime options used to control the TLS
# implementation when JSSE is used. Default is:
# "-Djdk.tls.ephemeralDHKeySize=2048"
#
# CATALINA_PID (Optional) Path of the file which should contains the pid
# of the catalina startup java process, when start (fork) is
# used
#
# LOGGING_CONFIG (Optional) Override Tomcat's logging config file
# Example (all one line)
# LOGGING_CONFIG="-Djava.util.logging.config.file=$CATALINA_BASE/conf/logging.properties"
#
# LOGGING_MANAGER (Optional) Override Tomcat's logging manager
# Example (all one line)
# LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"
#
# UMASK (Optional) Override Tomcat's default UMASK of 0027
#
# USE_NOHUP (Optional) If set to the string true the start command will
# use nohup so that the Tomcat process will ignore any hangup
# signals. Default is "false" unless running on HP-UX in which
# case the default is "true"
# -----------------------------------------------------------------------------
[root@yinzhengjie ~]#

[root@yinzhengjie ~]# head -108 /usr/local/tomcat/bin/catalina.sh

2>.配置tomcat的启动脚本

[root@yinzhengjie ~]# more /etc/init.d/tomcat
#!/bin/sh
# Tomcat init script for Linux.
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie
#EMAIL:y1053419035@qq.com
#
# chkconfig:
# description: The Apache Tomcat servlet/JSP container. JAVA_OPTS='-Xms64m -Xmx128m'
JAVA_HOME=/usr/java/jdk1.
CATALINA_HOME=/usr/local/apache-tomcat-9.0. export JAVA_OPTS JAVA_HOME CATALINA_HOME case $ in
start)
exec $CATALINA_HOME/bin/catalina.sh start
;;
stop)
exec $CATALINA_HOME/bin/catalina.sh stop
;;
restart)
$CATALINA_HOME/bin/catalina.sh stop
sleep
exec $CATALINA_HOME/bin/catalina.sh start
;;
*)
exec $CATALINA_HOME/bin/catalina.sh *
;;
esac
[root@yinzhengjie ~]#
[root@yinzhengjie ~]# chmod +x /etc/init.d/tomcat
[root@yinzhengjie ~]#
[root@yinzhengjie ~]# ll /etc/init.d/tomcat
-rwxr-xr-x root root Sep : /etc/init.d/tomcat
[root@yinzhengjie ~]#

3>.测试启动脚本

[root@yinzhengjie ~]# jps
Jps
[root@yinzhengjie ~]#
[root@yinzhengjie ~]# /etc/init.d/tomcat start
Using CATALINA_BASE: /usr/local/apache-tomcat-9.0.
Using CATALINA_HOME: /usr/local/apache-tomcat-9.0.
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-9.0./temp
Using JRE_HOME: /usr/java/jdk1.
Using CLASSPATH: /usr/local/apache-tomcat-9.0./bin/bootstrap.jar:/usr/local/apache-tomcat-9.0./bin/tomcat-juli.jar
Tomcat started.
[root@yinzhengjie ~]#
[root@yinzhengjie ~]# jps
Bootstrap
Jps
[root@yinzhengjie ~]#
[root@yinzhengjie ~]# /etc/init.d/tomcat stop    #温馨提示:和start之间执行频率不宜过快!          
Using CATALINA_BASE: /usr/local/apache-tomcat-9.0.
Using CATALINA_HOME: /usr/local/apache-tomcat-9.0.
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-9.0./temp
Using JRE_HOME: /usr/java/jdk1.
Using CLASSPATH: /usr/local/apache-tomcat-9.0./bin/bootstrap.jar:/usr/local/apache-tomcat-9.0./bin/tomcat-juli.jar
[root@yinzhengjie ~]# jps
Jps
[root@yinzhengjie ~]#

 

四.观测垃圾回收器的回收机制

1>.垃圾回收器的适用场景

  1.1>.并行垃圾收集器(Parallel GC)

    优势在于有着很高的吞吐能力(Best Throughput)

  1.2>.Cpncurrent GC

    优势在于有着更好的暂停能力(Best Pause)

  1.3>.增量垃圾收集器(Incremental GC,即Train模型)

    优势在于有着更好的暂停方式(Bestter Pause)

  1.4>.标记回收算法(Mark-compact)

    优势在于有着更好的吞吐能力(Better throughput)

2>.通过命令行jstat工具观察Java的回收机制

[root@yinzhengjie ~]# jps
Jps
[root@yinzhengjie ~]#
[root@yinzhengjie ~]# /etc/init.d/tomcat start
Using CATALINA_BASE: /usr/local/apache-tomcat-9.0.
Using CATALINA_HOME: /usr/local/apache-tomcat-9.0.
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-9.0./temp
Using JRE_HOME: /usr/java/jdk1.
Using CLASSPATH: /usr/local/apache-tomcat-9.0./bin/bootstrap.jar:/usr/local/apache-tomcat-9.0./bin/tomcat-juli.jar
Tomcat started.
[root@yinzhengjie ~]#
[root@yinzhengjie ~]# jps
Bootstrap
Jps
[root@yinzhengjie ~]#
[root@yinzhengjie ~]# jstat -gc 1s
S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT
2560.0 2560.0 2528.1 0.0 32768.0 14705.1 44032.0 5294.3 15360.0 14687.6 1792.0 1625.2 0.016 0.000 0.016
2560.0 2560.0 2528.1 0.0 32768.0 14705.1 44032.0 5294.3 15360.0 14687.6 1792.0 1625.2 0.016 0.000 0.016
2560.0 2560.0 2528.1 0.0 32768.0 14705.1 44032.0 5294.3 15360.0 14687.6 1792.0 1625.2 0.016 0.000 0.016
2560.0 2560.0 2528.1 0.0 32768.0 14705.2 44032.0 5294.3 15360.0 14687.6 1792.0 1625.2 0.016 0.000 0.016
2560.0 2560.0 2528.1 0.0 32768.0 14705.2 44032.0 5294.3 15360.0 14687.6 1792.0 1625.2 0.016 0.000 0.016
2560.0 2560.0 2528.1 0.0 32768.0 14705.2 44032.0 5294.3 15360.0 14687.6 1792.0 1625.2 0.016 0.000 0.016
2560.0 2560.0 2528.1 0.0 32768.0 14705.3 44032.0 5294.3 15360.0 14687.6 1792.0 1625.2 0.016 0.000 0.016
2560.0 2560.0 2528.1 0.0 32768.0 14705.3 44032.0 5294.3 15360.0 14687.6 1792.0 1625.2 0.016 0.000 0.016
2560.0 2560.0 2528.1 0.0 32768.0 14705.3 44032.0 5294.3 15360.0 14687.6 1792.0 1625.2 0.016 0.000 0.016
2560.0 2560.0 2528.1 0.0 32768.0 14705.4 44032.0 5294.3 15360.0 14687.6 1792.0 1625.2 0.016 0.000 0.016
[root@yinzhengjie ~]#

3>.通过jconsole和jvisualvm,jmap等工具查看Java垃圾回收机制

  详情请参考:https://www.cnblogs.com/yinzhengjie/p/9279283.html。

上一篇:“warning C4996: 'fopen': This function or variable may be unsafe”和“LINK : fatal error LNK1104”的解决办法


下一篇:jvm监控工具jconsole进行远程监控配置