Su warm small talk JVM: Chapter VM performance monitoring and troubleshooting tool summary, Chapter V tuning case studies and practical

This series is used to record "in-depth understanding of the Java Virtual Machine" book study notes. Easy to see for yourself, but also facilitate access.

Haste makes waste To reach the haste makes waste!

Chapter IV Virtual Machine Performance Monitoring and Troubleshooting Tools Summary

A, JDK command line tools

1, jps: VM process status tool

You can list the virtual machines running processes and displays the virtual machine executes the main class (main class where) the name of these processes as well as local virtual machine unique ID (Local Virtual Machine Identifier LVMID)

2, jstat: Virtual Machine Statistics Monitoring Tool

jstat (JVM Statistics Monitoring Tool) is a command-line tool for monitoring virtual machines running a variety of state information, it can display local or remote virtual machine in the process of class loading, memory, garbage collection, JIT compiler and other operating data, in the absence of GUI graphical interface, available only on text-only console server environment, when you run it on a virtual machine performance problems positioning the tool of choice.

3, jinfo: java configuration tool

Role jinfo (Configuration Info for java) is a real-time view and adjust the parameters of the virtual machine

4, jmap: java memory mapping tool

jmap (Merory Map for java) command is used to generate heap dump snapshot (commonly referred to as heapdump or dump file)

5, jhat: a snapshot of the virtual machine heap dump analysis tool

After the Sun JDK provided jhat (JVM Heap Analysis Tool) command jmap with the use of the generated heap dump analysis jmap jhat built a miniature snapshot of HTTP / HTML server, generate analysis results dump files can be viewed in a browser.

6, jstack: java stack trace tool

jstack (Stack Trace for java) command is used to generate a snapshot of the virtual machine thread the current time (generally referred to threaddump or Javacore file), the thread stack is a collection of snapshots of the current virtual machine approach each executing thread, the thread generated snapshots the main purpose is to locate the thread appears long pause, such as inter-thread deadlocks, infinite loop, external resource request due to long waits etc. are a common cause of thread long pause, pause time thread to see through jstack each thread's call stack, you can know that is not responding threads in the end things done in the background, waiting for something or resources.

In JDK1.5 java.lang.Thread class adds a getAllStackTreces () method, used to obtain virtual machine StackTraceElement objects for all threads, you can use this method to complete most of the functionality jstack through a few simple lines of code, administrators may wish to call this method to be a page in the actual project, you can always use a browser to view the thread stack.

<%@ page import="java.util.Map"%>

<html>
<head>
<title>服务器线程信息</title>
</head>
<body>
<pre>
<%
    for (Map.Entry<Thread, StackTraceElement[]> stackTrace : Thread.getAllStackTraces().entrySet()) {
        Thread thread = (Thread) stackTrace.getKey();
        StackTraceElement[] stack = (StackTraceElement[]) stackTrace.getValue();
        if (thread.equals(Thread.currentThread())) {
            continue;
        }
        out.print("\n线程:" + thread.getName() + "\n");
        for (StackTraceElement element : stack) {
            out.print("\t"+element+"\n");
        }
    }
%>
</pre>
</body>
</html>

7, HSIDS: JIT generated code compilation

HSIDS is a Sun official recommended HotSpot virtual machine JIT compiled code disassembly plug

Two, JDK visualization tools

1, JConsole: java monitoring and management console

JConsole (java monitoring and management console) is a visual monitoring based on JMX management tool that is part of management functions for managing JMX MBean, MBean codes may be used, the management console or the middleware server software in compliance with JMX specification access.

2, VisualVM: in-one troubleshooting tool

VisualVM (All-in-One java Troubleshooting Tool) is by far the JDK with the release of the most powerful features of the operation monitoring and troubleshooting procedures, and can be expected virtual machine troubleshooting tools in the next period of time is the main official development official on the software VisualVM wrote the "All-in-One" description of the word, suggesting that in addition to the operational monitoring, troubleshooting, but also provides many other features, such as performance analysis, performance analysis of VisualVM JProfiler YourKit function even compared to other professional fees and Profiling tools will not be much less, and VisualVM there is a big advantage: therefore affect not need to be monitored special Agent program running on the actual performance of its applications so small that it can be used directly in the production environment, this advantage is JProfiler YourKit and other tools can not be compared of.

Chapter V tuning case studies and practical

In the "in-depth understanding of java virtual machine" The main contents of the book is about the author of this section to tuning in the course of their work some experience in actual combat. For our readers, the focus of the analysis is to learn specific ideas to solve the problem.

First, optimizing time and compile-time class load

Compile time is the time-consuming virtual machine JTI hotspot compiler code.

Virtual machine built two run-time compiler, java method is called if the number of times a certain program, it will be judged to the JIT compiler for hot code, time compilation to native code, run faster, this is the Hotspot virtual machine name origin.

Program on two, high-performance hardware deployment strategy

1. Basic Information About

(1) Environment: a PV about 150,000 / day online document type website recently replaced the hardware system, the new system hardware 4 CPU, 16G of physical memory, OS is 64-bit CentOS5.4, Resin as a Web server.

(2) Description: The entire server is temporarily not deploy other applications, all the hardware resources can be provided to the traffic is not too much use of the site. Administrator To maximize utilization of hardware resources of the selected 64-bit JDK1.5, and by -Xms -Xms parameters java heap fixed 12GB.

(3) Problem: After using for some time found that the use is not satisfactory, the site often irregular phenomenon for a long time without a response.

(4) investigation: After Monitoring Server Health found that the site is not responding due to GC pauses caused a virtual machine running on Server mode, the default throughput priority collector, recycling heap of 12GB, a Full GC pause time up to 15 second. And because of the design of the program, when access to documents should document extracted from disk into memory, resulting in many large objects generated by the document sequence of memory appeared, many of these large objects into the old era, not cleaned in the Minor GC out. In this case, even if there is 12GB of heap memory soon be depleted, resulting in ten minutes pause every ten seconds appear.

(5) Analysis: The first issue does not extend to discuss the program code, the program deployed on the main issues apparently caused when too much memory heap for recycling long pause. Before using 32-bit system hardware upgrades 1.5GB heap, visit the Web site user experience only slow, but very significant pause does not occur, so before considering upgrading hardware to improve the effectiveness of the program, if rescaled to java heap memory, then investment in hardware is wasted.

2, the embodiment of high-performance hardware deployment

There are two main ways:

(1) by using the large memory 64 JDK

(2) using a plurality of 32-bit virtual machine to establish a logical cluster using hardware resources

3, used by the 64-bit large memory JDK

For user interaction is strong, sensitive to the dwell time of the system, you can allocate more than the java virtual machine heap premise is confident of Full GC control frequency applications low enough, at least to a low not affect users, such as 10 a few hours or even the day before appears once Full GC, so it can even automatically restart the server to be maintained at a stable level memory space available Full GC departure by way of execution timing tasks late at night.

Full GC frequency control key is to see whether the application of an absolute majority of objects in line with the principle of "Chaosheng evening off", ie, the survival time of most objects should not be too long, especially not produce a batch of long survival time of large objects, so as to ensure stable's old space.

Most sites in the application form, the life cycle of the main target should be requested level or page-level, session-level and global level relative reduction of long-life objects. As long as the code written by reasonable and they should be able to achieve in a large heap of normal use without Full GC, so, when using large heap memory, speed of response of the site was more assured. In addition, if readers plan to use the 64-bit JDK to manage large memory, also we need to consider the following issues faced by:

(1) caused by long pause garbage collection

(2) stage, 64-bit JDK performance test results are generally lower than 32 JDK

(3) the need to ensure adequate stability program, since this application if the produce is almost impossible to produce a heap overflow heap dump snapshot (because you want to generate hundreds of GB or even larger dump file), even if it produces a snapshot is almost impossible to resolve.

(4) The same procedure as consumed in the memory 64 JDK JDK generally large than 32, which is due to expansion and a pointer to its data type and other factors caused by padding.

4, using a number of 32-bit virtual machine cluster to create a logical use of hardware resources

The specific approach is to start on a physical machine in the plurality of application server processes, the different dispensing port to each server, and then to build a front end load balancer to assign manner reverse proxy access requests. Readers do not need to be too concerned about forwarding performance equalizer consumed, even with 64-bit JDK, many applications have more than one server, so many applications pre-stage equalizer always present.

High availability requirements For the purpose of establishing a logical cluster on a single physical machine is only possible use of hardware resources, does not need to care about state retention, thermal transfer and the like, do not need to ensure that each virtual machine has absolutely accurate balancing process load, so no session replication using affinity-style cluster is a very good choice. We just need to ensure that the cluster has an affinity, that is, according to certain rules equalizer algorithm will always be a fixed user requests a fixed allocation to a cluster node can be processed, so that the program development phase basically do not do anything for a clustered environment special consideration.

5, using a logical cluster deployment mode, you may experience some of the following questions

(1) Try to avoid competitive global resource nodes, the most typical is the disk competition, each node if simultaneous access to a disk file, then (problem is particularly prone to concurrent read and write operations), it can easily lead to an IO exception.

(2) difficult to use the most efficient pool certain resources, such as connection pooling, usually establish their own independent connection pool in each node, this can lead to some nodes pond full while others still have more spare nodes. Although you can use a centralized JNDI, but there is a certain degree of complexity and may bring additional performance cost.

(3) each node is still inevitably be a 32-bit memory limitations, only in 32-bit Windows platforms 2GB of memory per process, taking into account the memory overhead than the heap that is generally open to a maximum of 1.5GB. In some Linux, Unix system, you can upgrade to 3GB and even closer to 4GB of RAM, but 32 are still subject to (2 ^ 32) to limit the maximum memory of 4GB.

(4) using a large number of local cache (e.g., using a large number of so-called hashmap K / V buffer) of the application, resulting in a large waste of memory logical cluster, since each node has a logical cache, then the local cache may be considered into a centralized cache.

6. Summary

Introduction to these two deployments, and then return to this case, adjust the final deployment plan for the establishment of five 32-bit JDK logic clusters, each process is calculated by 2GB of RAM, 10GB of memory occupied. In addition the establishment of an Apache service as a front-end equalizer proxy access portal. Considering the low response to the request for the user, so CMS collector to garbage collection. After the deployment is complete, the service then there have long pause phenomenon, speed upgrades have more than the previous hardware upgrade.

Three, eclipse tuning speed

Eclipse tuning speed

IV Summary

Tuning is indeed a "coolie" live, but the quality of personnel requirements are relatively high, like the above mentioned experience, because of lack of experience with such a long lead time tuning. Of course, the sun after the storm, survive, and really improved a lot, write a lot of code, but also enhance the bigger picture. So tune this thing, is a good interview question, the best indicator of personal ability.

 

Acknowledgments: Special thanks to technical support provided by the author Zhou Zhiming!

Published 110 original articles · won praise 9 · views 6955

Guess you like

Origin blog.csdn.net/guorui_java/article/details/104026992