Java Virtual Machines Those things (1)

Source: Internet
Author: User

For Java programmers, knowledge of virtual machines and multithreading is essential. Here's a chat. Some of the fundamentals and concepts of Java virtual machines are derived from the "deep understanding of Java Virtual Machines" book.

Why do we need a virtual machine first? Because object creation and destruction is a very frequent operation, by the programmer to maintain, on the one hand the cost is a bit high, increase development costs; On the other hand, if the operation is improper, a memory leak, you have to debug the code to find out why. So the emergence of this mechanism of virtual machines can be said to be the gospel of programmers, liberating the productivity. But there are pros and cons, the introduction of virtual machines makes Java in performance with C + + ratio or there is a certain gap, such as online games or databases such a high performance requirements of the application, will choose to develop with C + +. At the same time, although we have a virtual machine, but also to its operating mechanism and performance tuning have some knowledge, or in case of a Java memory leak, will not know.

Java Memory Area

The five big data regions in the graph can be divided into two categories: 1. Data regions shared by all threads 2. Thread-Isolated data area

First Class: Method area, heap

Type II: Virtual machine stack, local method stack, program counter

(Image from blog http://blog.sina.com.cn/s/blog_ed30769e0102v233.html)

Program counter (Counter Register)

You can think of it as the line number indicator of the byte code that the current thread executes. We select the next byte-code instruction to execute by changing the value of this counter. Each thread has a separate program counter. If the thread is executing a Java method, this counter records the address of the virtual machine bytecode instruction being executed, and if the native method is being executed, this counter value is empty. This memory area is the only area in the Java Virtual Machine specification that does not stipulate any outofmemoryerror conditions .

Java VM Stack (Java Virtual machine Stacks)

It describes the memory model that the Java method executes: Each method is executed to create a stack frame to store information such as local variable tables, operand stacks, dynamic links, method exits , and so on.

The local variable table holds the various basic data types, object references, and ReturnAddress types (addresses that point to a bytecode directive) that are known at compile time. The memory space required for the local variable table is allocated during compilation .

This area specifies two types of anomalies:

1. If the thread requests a stack depth greater than the virtual machine allows, a Stackoverflowerror exception is thrown.

2. When memory is dynamically extended, an OutOfMemoryError exception is thrown if sufficient memory cannot be requested.

Local methods Stack (Native method Stacks)

This method area is similar to the Java Virtual machine stack, except that it is the native method service that is used for the virtual machine. Some virtual machines, such as sun hotspot virtual machines, merge them directly.

As with the virtual machine stack, it throws Stackoverflowerror and OutOfMemoryError exceptions as well.

Java heap (Java heaps)

A chunk of memory that is shared by all threads, created when the virtual machine is started. The only purpose of this memory area is to hold object instances.

The heap memory size can be controlled by the settings of-xmx and-XMS.

If the heap cannot be extended, a OutOfMemoryError exception is thrown.

Method area

It is also an area of memory shared by each thread that stores data such as class information, constants, static variables, and code compiled by the instant compiler that have been loaded by the virtual machine.

Java virtual machines have a very loose limit on this area. Garbage collection behavior is less frequent in this area; the memory recovery target for this area is primarily for the recovery of constant pools and the unloading of types .

The OutOfMemoryError exception is thrown when the method area does not meet the memory allocation requirements.

Run a constant pool (runtime Constant)

It is part of the method area; Used to hold various literal and symbolic references generated during compilation, which will be stored in the run-time pool of the method area after the class is loaded.

It is also possible to put new constants into the pool during run time, for example, using the intern () method of the String class.

An OutOfMemoryError exception is thrown when the memory cannot be requested.

Direct Memory

Direct memory is not part of the data area when the virtual machine is running, nor is it a memory area in the VM specification, but this portion of memory is used frequently and may cause outofmemoryerror anomalies to occur.

By adding NiO to JDK1.4, a channel-to-buffer-based I/O approach is introduced, which can allocate out-of-heap memory directly using the native library, and then use the Directbytebuffer object stored in the heap as a reference for this memory.

Object access

Object obj = new Object ();

Virtual machine Stack: An object reference is stored in the local variable table

Heap: Storage of all instance data values of type Object

Method Area: Store class information for an object

Different virtual machines are accessed differently, and there are two main ways to access them: using handles and direct pointers.

Handle access Mode

The heap divides a piece of memory as a handle pool, and the reference stores the handle address of the object, and the handle contains the specific address information of the object instance data and the type data.

Benefit: A stable handle address is stored in the reference, and only the instance data pointer in the handle is changed when the object is moved, and the reference itself does not need to be modified.

Direct pointer access (the way Sun hotspot uses)

The layout of the heap object must consider how to place information about the access type data that is directly stored in the Reference object address.

Benefit: Faster, saves time overhead for pointer positioning.

OutOfMemory exception

Heap overflow: Through-xx:+heapdumponoutofmemoryerror, you can dump the current memory heap dump snapshot in case of a memory overflow for analysis afterwards.

Virtual machine stack and local method stack overflow: Set stack capacity through-XSS. The general stack depth can reach 1000-2000. Stacks are too large to run out of memory when multi-threading, because a single thread consumes more memory.

Run a constant pool overflow: common PermGen issue. JAVA8 has made adjustments to the method area, so the problem will not come up again.

Method Area overflow: Often occurs in applications that dynamically generate a large number of classes.

Native Direct Memory overflow: can be specified by-xx:maxdirectmemorysize and, if not specified, the default is the same as the maximum value of the heap.

Object Recycling

How to determine if an object is dead and can be recycled?

Reference counting method (Python)

A very intuitive way to add one when a reference is added, minus one when a reference is reduced.

But one of its main drawbacks is that it is difficult to solve the problem of circular dependencies between objects.

Root search algorithm (JAVA,C#,LISP)

Basic idea: Through a series of GC roots objects as the starting point, starting from these nodes to search down, the path that the search traversed is called the reference chain. When an object is connected to a GC roots without any reference chain, it proves that the object is unreachable as a collection object.

GC roots objects include the following:

1. Referenced objects in the virtual machine stack (local variable table in the stack frame)

2. Objects referenced by class static properties in the method area

3. Constants in the method area reference objects

4. Objects referenced by JNI in the local method stack

After JDK1.2, Java expanded the concept of references into four types:

1. Strong references (strong reference): Generic references, references, objects are not recycled.

2. Soft reference (soft reference): When the system is about to overflow, the objects they refer to are included in the collection scope for recycling. If the memory is not enough, then throw an exception.

3. Weak reference (weak reference): The referenced object only lives until the next garbage collection occurs.

4. Virtual reference (Phantom Reference): Whether an object has a virtual reference, does not have an impact on its lifetime, and cannot obtain an object instance from a virtual reference. The goal of setting this reference is to be able to receive a system notification when the object is recycled.

Declare an object to die, at least two times to mark the process

If an object is not associated with a reference chain after the root search, it will be first flagged and filtered once to see if it is necessary for this object to execute the Finalize () method.

If this method is not overridden or the method has been called by the virtual machine, it is considered "no need to execute."

If the Finalize () method needs to be executed, it is placed in a f-quenue queue and executed by a low-priority finalizer thread. This method is executed, but does not guarantee the end of the run (for operational efficiency considerations).

A second small-scale token for the f-queue will be made later, so long as it is associated with any object on the reference chain in the Finalize method, it is removed from the collection that is about to be recycled at this token.

Recycling Method Area

In the heap, especially in the Cenozoic, a garbage collection can reclaim 70%~95% space, and the method area is much less efficient.

Garbage collection of the method area main content: obsolete constants and useless classes .

Classes need to meet the following 3 criteria to be considered "useless classes" and can be recycled:

1. All instances of this class have been recycled

2. The classloader that loaded the class have been recycled

3. The corresponding Java.lang.Class object of this class is not referenced anywhere, and the method of accessing the class can be accessed anywhere by reflection

Whether the class is recycled and controlled by the parameter-XNOCLASSGC

Garbage collection algorithm tag-purge algorithm

The algorithm is divided into two phases: Mark and clear all the objects that need to be recycled, and collect all the tagged objects uniformly after the tag is complete.

Disadvantages:

1. Efficiency issues, marking and cleaning processes are not efficient

2. Spatial problems can result in large amounts of discontinuous memory fragmentation. If you find that you cannot allocate memory to large objects, you have to trigger a garbage collection again

Replication Algorithms

Divide the memory you can use into two pieces, one at a time.

The experiment found that the new generation of object 98% was born to die, so the memory can be allocated to a larger Eden Space and two smaller survivor space. Each time you use Eden and one of the survivor spaces, when you reclaim memory, copy the surviving objects to another survivor space, and then clean up the other two pieces of space.

The default Eden and survivor size ratios for Sun hotspot virtual machines are 8:1. When the survivor space is not enough, it is necessary to rely on the old age for distribution guarantees, if the guarantee is directly allocated into the old age.

Tagging-sorting algorithms

The replication algorithm performs more replication operations when the object has a higher survival rate and becomes less efficient. Therefore, the old age can not directly choose this algorithm.

Tag-grooming algorithm after tagging, let all surviving objects move to one end, and then directly clean out memory outside the end boundary.

Garbage collector serial Collector

The oldest collector, before JDK1.3.1, is the only option for the new generation of virtual machines. Single-threaded collector.

Advantages: Simple and efficient

Disadvantage: All other worker threads (stop the world) must be paused at the time of collection.

Usage Scenario: The virtual machine runs the default Cenozoic collector in client mode .

Parnew Collector

It is a multithreaded version of the serial collector. The number of collection threads opened by default is the same as the number of CPUs. JDK1.4 introduced.

In addition to the serial collector, only it can work with the CMS collector.

Usage Scenario: A new generation collector is preferred for virtual machines running in server mode .

Parallel Scavenge Collector

Goal: Achieve a controllable throughput . The so-called throughput is the ratio of the CPU's time spent running user code to the total CPU time consumed. Suitable for tasks that operate in the background and do not require too much interaction.

-xx:maxgcpausemillis Control maximum garbage collection pause time

The-xx:gctimeratio is used to set the throughput size by default 99

-xx:+useradaptivesizepolicy, turn on the GC Adaptive Tuning Strategy .

Usage Scenario: New Generation Collector

Serial Old Collector

It is the old version of the serial collector, using the tag grooming algorithm.

Usage scenario: The old age in client mode.

Parallel Old Collector

It is the old age version of the parallel scavenge collector. JDK1.6 introduced.

Attention to the throughput rate of the occasion, you can consider the use of parallel scavenge plus parallel old collector.

CMS collector

It is a collector that targets the shortest recovery pause time and executes concurrently. Tag Cleanup algorithm: initial tag, concurrency token, re-tagging, concurrent purge . Where the initial tag and re-tagging still need to stop the world.

Disadvantages:

1. Very sensitive to CPU resources. The default number of recycled threads is (number of CPUs +3)/4.

2. Unable to process floating garbage, a "concurrent mode failure" failure may occur resulting in another full GC. If the reserved memory fails to meet the program's needs during the run collection, a "concurrent mode failure" failure occurs, and the serial old collector is temporarily used for garbage collection in the older age, so that the pause time is longer.

3. The collection ends with a lot of fragmentation, and it is easy to trigger the full GC because of the inability to allocate large objects. You can enable the parameter-xx:+usecmscompactatfullcollection to enjoy the full GC after the defragmentation. There is also-xx:cmsfullgcsbeforecompaction this parameter to set the number of times the full GC has been executed without compression, once with compression.

G1 Collector

The goal is to replace the CMS collector. Collection can be generational, without fragmentation. With the concept of zoning (region), priority is given to recovering the most rubbish areas. Avoid full heap scanning by remenbered set.

The collection can be divided into four steps: initial tag, concurrency tag, final tag, filter collection.

Memory allocation and reclamation policy objects take precedence over Eden allocation

When there is not enough space in the Eden area, a minor GC is triggered. The virtual machine provides-xx:+printgcdetails this parameter to print the memory reclaim log and the allocation of the current memory regions when the process exits.

Large objects go straight into the old age

The purpose of this is to avoid a large number of memory copies in the Eden area. How many objects can be set through the-xx:pretenuresizethreshold parameter into the old age.

Long-lived objects will enter the old age.

After 15 minor GC, reached 15 years old, will be promoted to the old age. About this age, can be set by-xx:maxtenuringthreshold.

Dynamic Object Age Determination

In order to adapt to the memory situation of different programs, if the sum of all objects of the same age in Survivor space is greater than half of the survivor space, objects older than or equal to that age can enter the old age directly.

Space Allocation guarantee

When the minor GC occurs, the average size of each promotion to the old age is greater than the remaining space size of the old age before the virtual opportunity is detected.

If it is greater, then a full GC is performed instead.

If it is less than, first check whether the Handlepromotionfailure setting (JDK1.6 is on by default) allows the warranty to fail. If allowed, do minor GC, or full GC if not allowed.

If the last warranty fails, you will have to re-launch the full GC.

Java Virtual Machines Those things (1)

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.