Use JDK tools for Java application server fault exclusion

The main advantage of this article and sharp lesson we talk about Java Performance Tuning Guide - on improving Java various techniques of code performance.

1. Introduction

In the Java world, most of us are accustomed to in Java using all phases of application development GUI tools: write code to debug and analysis. We usually prefer to set up the server environment in a development environment, and try to use familiar tools to reproduce the problem locally. Unfortunately, for various reasons, it is generally not possible to reproduce some of the problems locally. For example, you may not have access to the server application processing real client data.

In this case, you need on the server box remote application troubleshooting. You should keep in mind that you can not use exposed JRE to properly troubleshoot application: It contains all the troubleshooting capabilities, but in fact can not access it. As a result, you need to use on the same box JDK or some third-party tools. This article will introduce JDK tools, as compared with many third-party tools any organization that requires a security audit, you may be allowed to use it in a production environment.

Typically, only the JDK release Extract the package to your box is enough - you do not need for troubleshooting purposes and properly installed it (in fact, in many cases you do not want installed correctly). For based on JMX functionality, you can actually install any Java 7/8 JDK , but some tools do not recognize the future of the JDK , so I suggest you install the latest Java 7/8 JDK or with the server JRE build exact match - it allows you to dump the stack for the current application security applications do not have access point (some applications in idle mode is a simple example of "no safe point" applications).


2. Troubleshooting program

2.1. Gets a running JVM list

To start work, you almost always need to get running the JVM , their process ID list and command-line parameters. Sometimes it may be sufficient: you may find that the same application simultaneously execute a second instance of the same work (and damage to the output file / reopen socket / perform some other silly operations).

Simply run jcmd without any parameters. It will show you running JVM list:

3824 org.jetbrains.idea.maven.server.RemoteMavenServer
2196
780 sun.tools.jcmd.JCmd

 

Now, you can run jcmd <PID> help to see what can be used to diagnose command given command the JVM . This is VisualVM example of an output:

 

>jcmd 3036 help
 
3036:
The following commands are available:
JFR.stop
JFR.start
JFR.dump
JFR.check
VM.native_memory
VM.check_commercial_features
VM.unlock_commercial_features
ManagementAgent.stop
ManagementAgent.start_local
ManagementAgent.start
GC.rotate_log
Thread.print
GC.class_stats
GC.class_histogram
GC.heap_dump
GC.run_finalization
GC.run
VM.uptime
VM.flags
VM.system_properties
VM.command_line
VM.version
help

 

Type jcmd <PID> <COMMAND_NAME> to run diagnostic commands or get an error message asking the command parameters:

 

>jcmd 3036 GC.heap_dump
 
3036:
java.lang.IllegalArgumentException: The argument 'filename' is mandatory.

 

You can use the following command to obtain more information about the diagnostic command parameters: jcmd <PID> Help <CommandName> . For example, this is GC.heap_dump output of the command:

 

>jcmd 3036 help GC.heap_dump
        
3036:
GC.heap_dump
Generate a HPROF format dump of the Java heap.
 
Impact: High: Depends on Java heap size and content. Request a full GC unless the '-all' option is specified.
 
Permission: java.lang.management.ManagementPermission(monitor)
 
Syntax : GC.heap_dump [options] <filename>
 
Arguments:
filename :  Name of the dump file (STRING, no default value)
 
Options: (options must be specified using the <key> or <key>=<value> syntax)
-all : [optional] Dump all objects, including unreachable objects (BOOLEAN, false)


2.2. Heap dump

jcmd为你提供了一个方便的界面,用于以HPROF格式进行堆转储。只需运行jcmd <PID> GC.heap_dump <FILENAME>。请注意,文件名是相对于正在运行的JVM当前目录而不是当前目录的,因此你可能需要指定完整路径。最好使用.hprof扩展名作为转储文件名。

线程转储完成后,你可以将文件复制到自己的盒子中,然后在VisualVM(它是JDK的一部分)中打开它,并使用其堆walker和查询语言功能,或将其加载到Java Mission ControlJOverflow插件中并对其进行分析各种内存问题。

Note 1 : Of course, there are many other tools can handle hprof files: NetBeans , the Eclipse Memory Analyzer , YourKit and so on. The .hprof After the file is downloaded to the box, use your favorite tool.

Note 2 : You can also use the jmap tools heap dump: jmap -dump : Live , File = <FILE_NAME> <PID> . The problem is that it is officially proved as unsupported. Many of us people think JDK unsupported content will always be there, but it proved no longer the case: JEP 240 , JEP 241


2.3. Analysis histogram class

If you are looking for memory leaks, usually only interested in the activities of the heap objects of certain types. For example, you may know can only have a certain type of objects (mainly working class some applications). There may also be in the old generation in one instance or more of the same class, so far, these examples are not yet eligible for garbage collection, but they should not be accessed from the application root directory.

To print class histograms, run one of the following two commands (Both commands print the active object number):

jcmd <PID> GC.class_histogram
jmap -histo:live <PID>

The following is an example of the output lines before:

 num     #instances         #bytes  class name
----------------------------------------------
   1:          5923        5976952  [I
   2:         50034        4127704  [C
   3:         49465        1187160  java.lang.String
   4:           188        1069496  [J
   5: 3985 1067240 [Ljava.util.HashMap $ Node;
   6:          8756         982872  java.lang.Class
   7:          2855         835792  [B
   8: 23570 754240 java.util.HashMap $ Node
   9: 671440 13964 [Ljava.lang.Object;
  10:          9642         308544  java.util.Hashtable$Entry
  11: 4453 213744 java.util.HashMap

 

Please note that, in bytes occupied size is the size of a lighter - it does not contain any child objects. Easily accessible from [] char (class name = [C] and String stats ) took note of the fact that - even though the number of instances is similar (although char [] -s is always better than String much, but char [] - size s significantly greater, if String size comprising a base char [] size, is not the case.

Now, you can grep / search for the name of the class you are interested in, and check the number of active instances. If you see an instance exceeded expectations, follow and analyze heap dumps (see above) in any of the heap walker.

2.4. A thread dump

Sometimes, your application may be reported as " not doing Anything / GOT Stuck ." There are many " Stuck " situation - deadlocks, resource contention or simply O(N10)request algorithm to handle millions of users, in all these cases, you should know what your application is executing threads and lock the what actions they hold.

There are two types of locks: keyword-based synchronization and Object.wait / notifyAll original lock method, and Java 5 introduced the java.util.concurrent locks. The main difference between them is that the former is bound to synchronize your input portion of the stack frame, and always available in the thread dump. On the other hand, the latter ( the java.util.concurrent ) stack frame is not restricted - You can use one of the lock, and then leave it in another method. As a result, some time, they did not print a thread dump, even now they are still an option. However, you need to use both locked in a thread dump in order to properly investigate threading issues.

There are 3 ways to print application thread dump. You can Linux run on the kill -3 <PID> . Or, you can run one of these commands on any platform:

jstack <PID>
jcmd <PID> Thread.print

2.5. Run Java Flight Recorder

So far, all the tools mentioned in this article applies only to a quick survey. For more in-depth analysis, I recommend using the built-in the Java Flight Recorder .

Run JFR is a three-step process:

  1. You need to create a set that contains the required JFR template file. To do this, run jmc and go to the "Window" -> "flight recorder Template Manager" menu. After the configuration file is ready, export it to a file, and then send it to the box you are using.

  2. JFR need JDK commercial license. Now, you need the required JVM unlock business function:

jcmd <PID> VM.unlock_commercial_features

  1. After that you can start JFR . This is the command-line example:

jcmd <PID> JFR.start name=test duration=60s settings=template.jfc filename=output.jfr

 

This command runs immediately JFR (not set delay property), and use template.jfc template file and write the results set output.jfr (use absolute paths in both files), collecting JVM information 60 seconds.

录制完成后,你可以将.jfr文件复制到笔记本电脑并在jmcGUI中对其进行分析。它包含几乎所有你需要对JVM进行故障排除的信息,除了完整堆转储,你可以单独创建并复制到你的机器中。

 

感谢阅读!更深入探讨欢迎留言或私信。


抽丝剥茧 细说架构那些事——【优锐课】


Guess you like

Origin blog.51cto.com/14631216/2461173