JVM monitoring and tuning

    The purpose of learning the Java GC mechanism is to analyze the cause and solve it when there is a problem with the JVM. JVM monitoring and tuning mainly focuses on three points: how to configure, how to monitor, and how to optimize.

1. Parameter configuration

    In the parameters of the Java virtual machine, there are three representation methods. With the "ps -ef | grep java" command, you can get all the startup parameters and configuration parameters of the current Java process:

  • Standard parameters (-), all jvm implementations must implement the functions of these parameters and are backward compatible;
  • Non-standard parameters (-X), the default JVM implements the functions of these parameters, but does not guarantee that all JVM implementations are satisfied, and does not guarantee backward compatibility;
  • Non-Stable parameters (-XX), such parameters are different for each JVM implementation, may be canceled at any time in the future, and need to be used carefully (however, these parameters are often very useful);

2. Standard parameters

    The parameters added after running Java commands, such as java -version, java -jar, etc., enter the command Java -help or java -? to get a list of all Java standard parameters of the current machine.

  • -client: Set Java to use client mode, which is generally used on PC machines. It starts quickly, but the performance and memory management efficiency are not high; it is mostly used for desktop applications;
  • -server: Using server mode, although the startup speed is slow (about 10% slower than client mode), but the performance and memory management efficiency are very high, suitable for servers, servers used for generation environment, development environment or test environment;

    If -server or -client is not specified, the JVM will automatically detect whether the current host is a server when it starts. If it is, it will start in server mode. The 64-bit JVM only has server mode, so the -client parameter cannot be used; by default, different Startup mode, the way to perform GC is different:

boot mode Cenozoic GC method Old generation and persistent generation GC methods
client serial serial
server parallel Concurrency

    If no -server or -client mode is specified, the judgment method is as follows:

  • -classpath/-cp: The directory path where the JVM loads and searches for files, multiple paths are split with ;. Note that if -classpath is used, the JVM will no longer search the CLASSPATH path defined in the environment variable.

    The order of the JVM search path is:

  1. First search for the jar or zip package that comes with the JVM (Bootstrat, the search path can be obtained with System.getProperty("sun.boot.class.path");
  2. Search for the jar package under JRE_HOME/lib/ext (Extension, the search path can be obtained with System.getProperty("java.ext.dirs"));
  3. Search user-defined directories, the order is: current directory (.), CLASSPATH, -cp; (the search path is obtained with System.getProperty("java.class.path"))
  • -DpropertyName=value: Define the global property value of the system, such as the configuration file address, etc. If the value has spaces, it can be defined in the form of -Dname="space string", and these can be obtained with System.getProperty("propertyName") The defined property value can also be defined in the code in the form of System.setProperty(""propertyName","value").
  • -verbose: This is one of the most commonly used commands to query GC problems. The specific parameters are as follows: ①-verbose: class outputs the relevant information of the jvm loaded class. When the jvm reports that the class cannot be found or the class conflicts, it can be diagnosed. ②-verbose: gc outputs the relevant information of each GC. ③-verbose: jni outputs information about native method calls, which is generally used to diagnose jni call error messages.

2. Non-standard parameters

    Non-standard parameters are parameters extended on the basis of standard parameters. Enter the "java -X" command to obtain a list of all non-standard parameters supported by the current JVM. In different types of JVM, the parameters used are different. Please refer to the following figure to have a visual understanding of the size of the memory area:

  • -Xmn:新生代内存大小的最大值,包括E区和两个S区的总和。-Xmn只能使用在JDK1.4或之后的版本中,之前的1.3和1.4版本中,可使用-XX:NewSize设置年轻代大小,用-XX:MaxNewSize设置年轻代最大值;如果同时设置了-Xmn和-XX:NewSize,-XX:MaxNewSize,则谁设置在后面,谁就生效;如果同时设置了-XX:newSize  -XX:MaxNewSize与-XX:NewRatio则实际生效的值是:min(MaxNewSize,Max(newSize,Heap/(NewRatio+1))),在开发、测试环境,可以-XX:NewSize和-XX:MaxNewSize来设置新生代大小,但在线上生产环境,使用-Xmn一个即可,或者-XX:NewSize和-XX:MaxNewSize设置为同一个值,这样能够防止在每次GC之后都要调整堆的大小(即:抖动,抖动会严重影响新能)。
  • -Xms:初始堆的大小,也是堆大小的最小值,默认值是总共的物理内存/64(且小于1G),默认情况下,当堆中可用内存小于40%(这个值可以用-XX:MinHeapFreeRatio调整)时,堆内存会开始增加,一直增加到-Xmx的大小;
  • -Xmx:堆的最大值,默认值是总共的物理内存/64(且小于1G),如果Xms和Xmx都不设置,则两者大小会相同,默认情况下,当堆中可用内存大于70%时,堆内存会开始减少,一直减少到-Xms的大小;这个堆的大小=年轻代大小+年老代大小,堆的大小不包括持久代的大小,如果增大了年轻代,年老代会相应减少,官方默认的设置年老代大小/年轻代大小=2/1左右(使用-XX:NewRatio可以设置)。建议在开发测试环境可以用Xms和Xmx分别设置大小值最大值,但是在生产环境上,Xms和Xmx设置的值必须一样,原因与年轻代一样--防止抖动。
  • -Xss:这个参数用于设置每个线程的栈内存,默认1M,一般来说是不需要修改的。除非代码不多,可以设置的小点,另外一个相似的参数是-XX:ThreadStackSize,这两个参数在1.6以前,都是谁设置在后面,谁就生效;1.6版本以后,-Xss设置在后面,则以-Xss为准,-XXThreadStackSize设置在后面,则主线程以-Xss为准,其他线程以-XX:ThreadStackSize为准。
  • -Xrs:减少JVM对操作系统信号的使用,当此参数设置之后,JVM将不接收控制台的控制handler,以防止与在后台以服务形式运行的JVM冲突。
  • -Xprof:跟踪正运行的程序,并将跟踪数据在标准输出输出;适合于开发环境调试;
  • -Xnoclassgc:关闭针对class的gc功能;因为其阻止内存回收,所以可能会导致OutOfMemoryError错误,慎用。
  • -Xincgc:开启增量gc(默认为关闭);这有助于减少长时间GC时应用程序出现的停顿;但由于可能和应用程序并发执行,所以会降低CPU对应的处理能力。
  • -Xloggc:file:与-verbose:gc功能类似,只是将每次GC事件的相关记录到一个文件中,文件的位置最好在本地,以避免网络的潜在问题。若与verbose命令同时出现在命令行中,则以-Xloggc为准。

三、非Stable参数(非静态参数)

    以-XX表示的非Stable参数,虽然在官方文档中是不正确的,不健壮的,各个公司的实现也各不相同,但往往非常实用,所以这部分参数对于GC非常重要。JVM(Hotspot)中主要的参数可以大致分为3类:

  • 性能参数(Performance Options):用于JVM的性能调优和内存分配控制,如初始化内存大小的设置;
  • 行为参数(Behavioral Options):用于改变JVM的基础行为,如GC的方法和算法的选择;
  • 调试参数(Debugging Options):用于监控、打印、输出等JVM参数,用于显示JVM更详细的信息;

    对于非Stable参数,使用方法有4种:

  • -XX:+<option>启用选项
  • -XX:-<option>不启用选项
  • -XX:<option>=<number>给选项设置一个数字类型值,可跟单位,例如32k,1024m,2g
  • -XX:<option>=<String>给选项设置一个字符串值,例如-XX:HeapDumpPath=./dump.core

   首先介绍性能参数,性能参数往往用来定义内存分配的大小和比例,相比于行为参数和调试参数。一个比较明显的区别是性能参数后面往往跟的有数值,常用如下:


    常用的行为参数,主要用来选择使用什么样的垃圾收集器组合,以控制运行中的GC策略。


     常用的调试参数,用于监控和打印GC的信息:


     这些参数将为我们进行GC的监控与调试提供很大帮助,是我们进行GC相关操作的重要工具。 

 

 

转自:http://www.cnblogs.com/zhguang/p/Java-JVM-GC.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326947311&siteId=291194637