JVM Architecture and Tuning

Source: Internet
Author: User
Tags jprofiler
This is a creation in Article, where the information may have evolved or changed.

JVM Architecture and optimization

Write in front

Let's take a look at one interesting question:

Java Engineer interview, always ask some JVM how to optimize the problem, these really useful in development, work seven years has never been used in the project, and I have been a number of excellent staff, looking to do the optimization of the Daniel solution?

Answer one:
JVM optimizations are certainly useful, and may just be that the owner has not experienced this requirement. For example, some GC mechanisms can cause the JVM to stop the world, which means that all worker threads will stop waiting for the GC to complete. For some programs that are sensitive to latency, the pause is 100 or even dozens of milliseconds, which is unacceptable. To solve this problem, the JVM's parameters need to be adjusted appropriately. For example, adjust the size of the heap, choose the appropriate garbage collector, control the speed of the promotion of the old age and so on.

Author: Xiai Zhiheng

Links: https://www.zhihu.com/question/40913700/answer/88862720

Source: Know

Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

Answer two:
For the landlord this situation is understandable, I used to do the industry in the traditional industry software is also ignorant of the JVM, feel a little use no, interview asked just loaded. Later stepping into the internet industry to understand, this is a must master, and often use, encounter tp99 intermittent improvement, this situation first will see whether it is FULLGC caused, there is memory overflow and other problems, do not understand how the JVM to solve such problems. Of course, traditional industry software, less users may not go to monitor performance, encountered memory overflow may also be restarted resolved, nothing, next time restart. However, for Java developers, the JVM is a commercial building, we open a shop inside, even the fire hydrant where do not know, which day on fire, emergency stairs do not know where, can only sit and cry? So it is recommended that the landlord understand the JVM, recommended to see "in-depth understanding of Java Virtual Machine"

Author: Zhou Yi

Links: https://www.zhihu.com/question/40913700/answer/138011891

Source: Know

Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

As can be seen from the above two paragraphs of the answer, familiar with the JVM architecture and tuning the significance of the performance problem, more to the programmer left an escape route.

JVM Architecture


JVM Architecture

First, attach a JVM architecture diagram.
Mainly consists of several parts:

    • Class loader (ClassLoader): Loads the required class into the JVM when the JVM is started or when the class is running. Whenever you run a
      Java program, you will first dynamically create the appropriate JVM instance ( starting three programs, there will be three JVM instances, which is very similar to the Golang runtime ), and then load all of the program's class files through the class loader into the JVM.
    • Execution Engine: Responsible for executing the bytecode directives contained in the class file. The execution engine is the arithmetic engine that the Java Virtual machine simulates to run, basically plays the role of translation.
      There are two ways to translate:
      • A sentence is translated in a word. That is the interpretation of execution. Translate the Java bytecode you want to execute in the program calculator to the machine instructions that the CPU can run.
      • Disposable translation. That is, compile execution. All bytecode translations are completed at once using the JIT (just in time).
    • Memory area (also called run-time data area): operates the allocated memory area when the JVM is running. As can be seen, the main is divided into five parts.
    • Local library interface, local method library: Operating system-owned, for handling JVM native code and JIT-compiled native code.

JVM Execution Engine

With respect to the execution engine of the JVM, it is not the real software simulation that implements the CPU, and ultimately all the operations on the computer are handled by the CPU executing machine instructions.

Is there a problem? What is the relationship between the JVM execution engine and the local code? Does the native code also require a JVM execution engine?

Guess the answer: it should be needed, the JVM encapsulates all the code and manages the results of the program execution.

The experience of the execution engine in which it is run is as follows:

It takes more time to compile the code with the JIT compiler than to use an interpreter to explain the execution. Therefore, if the code is executed only once, then it is better to interpret execution rather than compile it. Therefore, a JVM with a built-in JIT compiler checks how often the method executes, and if a method executes more frequently than a specific value, the method is compiled with cost code.

JVM Memory

As you can see from the JVM architecture, the JVM memory is divided into five main parts:

    • Method Area: A place for storing class structure information, including constant pools, static variables, constructors, and so on. Although the JVM specification describes the method area as a logical part of the heap, it has an individual name of non-heap (not a heap), so let's not confuse it. The method area also contains a run-time-constant pool.
    • Java heap: The place where Java instances or objects are stored. This block is the main area of the GC. From the stored content we can easily know that the method area and heap are shared by all Java threads.
    • Java stack: the Java stack is always associated with threads, and whenever a thread is created, the JVM creates a corresponding Java stack for that thread. In this Java stack will contain more than one stack frame, each run a method to create a stack frame for storing local variable table, Operation Stack, method return value and so on. Each method from the call until the completion of the process, corresponding to a stack frame in the Java stack into the stack to the process. So the Java stack is ready-made and private.
    • Program counter (PC Register): Used to save the memory address of the current thread execution. Because the JVM program is multi-threaded execution (thread rotation switching), in order to ensure that the thread switch back, you can return to the original state, you need a separate counter, record where the previous interruption, visible program counters are also thread-private.
    • The local method Stack (Native): is similar to the Java stack, except for the Native method used by the JVM.

The Java heap holds object instances.
The Java stack holds common variables and other types of information.
The advantage is that objects usually occupy a large amount of memory, but the number is small. There are many things stored in the stack, which takes up less memory. Objects are the main battlefield of garbage collection.
So to separate stacks and heaps.

JVM Tuning

The traditional software development process does not need to take into account the JVM tuning content, mainly for the following considerations:

    • Performance is not a lifeline.
    • After the system problems can be taken in other ways to deal with, such as downtime, downtime and then restore, not affected.

In the Internet area, these two problems have become more serious problems.
In order to retain users, response time and throughput becomes an issue that must be addressed. JVM performance bottlenecks are mainly in the garbage collection mechanism that comes with the JVM.

Memory allocation mechanism

Static Memory & Dynamic memory

The principle of memory allocation in Java and C + +, each application of memory to malloc for the system calls, and system calls occur in the kernel space, each time to interrupt the switch, which requires a certain amount of overhead,

The Java Virtual machine is a one-time allocation of a large space, and then each new time in the space for allocation and release, reducing the number of system calls, saving a certain amount of overhead, which is somewhat similar to the concept of memory pool, and second, with this space, how to allocate and recycle is related to the GC mechanism.

There are two types of Java general memory applications:

    • Static memory. The memory that can be determined at compile time is static memory, that is, memory is fixed, system is allocated once, such as int type variable, Java stack, program counter, local method stack are thread private, line generates is born, thread is out, and stack frame in stack will be revoked with the end of method. The memory is naturally recycled. We don't need a tube.
    • Dynamic memory. Dynamic memory allocation is the amount of storage space that is known to be allocated when the program executes, such as the memory space of a Java object. So the memory allocations and recoveries in these areas are deterministic, but the Java heap and the method area are different, and we only know what to create when the program is running, so this part of the memory allocation and recycling is dynamic. In general, the garbage collection we are talking about is also part of this.

In short, stack memory management is sequential allocation, and fixed long, there is no memory recovery problem, and heap is for Java object instances randomly allocated memory, indefinite length, so there is memory allocation and recovery problems;

New Generation & Laosheng generation

In Java, the heap is divided into two different zones:

    • New Generation (young). The life cycle is relatively short.
    • Old age. Longer life cycle.
    • Permanent generation. are seldom discussed.

The New Generation (young) is divided into three regions:

    • Eden
    • From Survivor
    • To Survivor.

GC Mechanism Brief

What is the GC algorithm used by the JVM?

Generational collection: The memory is divided into several regions, and objects of different lifecycles are placed in different regions. The GC has the following three types for use:

    • GC (or minor GC): Collects areas with short life cycles. Minor GC will move all live objects in Eden to the Survivor area, and if the survivor area is not fit, the remaining live objects will be moved to old generation.

    • Full GC (or major GC): based on the tag-purge algorithm, collects the entire heap in areas with short life cycles (young area) and long life cycle (old areas).

    • Major GC: Clean up the permanent generation.

GC efficiency is also high, we want to minimize the number of full GC.

There are three options available on the minor GC and full GC execution mechanisms:

    • Serial GC (SERIALGC)
    • Parallel Recycle GC (Parallelscavenge
    • Parallel GC (PARNEW)

You can select the appropriate GC according to your specific needs.

The GC's adjustment is a balance between throughput and response time.

JVM Tuning Brief and tools

How to tune?
What tools do you have?
The JVM tuning tools are available in the following ways:

    • JCONSOLE:JDK comes with a simple function, but can be used with a certain load on the system. The garbage collection algorithm has a very detailed tracking. For more information, see here
    • Jprofiler: Commercial software, you pay for it. Powerful. For more information, see here
    • VISUALVM:JDK comes with powerful features similar to Jprofiler. Recommended.

On the tuning this piece in the actual business encounter this kind of problem and then carry on the concrete analysis.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.