Deep understanding of Java Virtual machines: JVM advanced features and best practices learn note ⅰ

Source: Internet
Author: User

The first part walks into Java
Part two automatic memory management mechanism
The 2nd Chapter garbage collector and memory allocation policy
1.Java virtual machine When executing a Java program, the memory that it locks into is divided into several different data regions.
These zones have their own uses, as well as the time to create and destroy, and some areas exist as the virtual machine process starts,
Some regions are created and destroyed since the start and end of user threads.
According to the Java Virtual Machine specification, there are several runtime data regions:
Program counter (Counter Register)
Method area
Virtual machine stack (VM stack)
Local methods Stack (Native method Stacks)
Heaps (heap)
2.PCR Program counter
In the operating system, the program Counter is used to store the address of the unit where the next instruction is located.
In a virtual machine, it can be seen as the line number indicator of the bytecode executed by the current thread. The bytecode interpreter works by changing the value of this counter to select the next byte-code instruction that needs to be executed, and the basic functions such as branching, looping, and jumping need to rely on the counter to complete.

In order to be able to recover to the correct execution location during the multi-thread switching process, each thread needs to have a separate program counter, the counters between the threads do not affect each other, independent storage, become "thread-private" memory. Also, the program counter is the only area in the Java Virtual Machine specification that does not specify any outofmemory conditions.
3.java VM stack Java virtual machine Stacks
As with program counters, Java Virtual machine stacks are thread-private, with the same lifecycle and threads. The
Virtual machine stack describes the memory model that is executed by the Java method: Each method is executed with a stack frame that stores information such as local variable tables, operations stacks, dynamic links, method exits, and so on.
//Each method is called until the completion of the process, corresponding to a stack frame in the virtual machine stack from the stack into the outbound process. The
virtual machine stack throws Stackoverflowerror and OutOfMemoryError exceptions.
5.java heap
The Java heap is a piece of memory that is shared by all threads and created when the virtual machine is started. The only purpose of this area is to hold object instances. The
Java heap is the primary area of garbage collector management and is often referred to as the GC heap (garbage collected heap). The
throws an OutOfMemoryError exception if there is no memory in the heap to complete the instance assignment, and the extension cannot continue.

6. Method Area
The method area, like the Java heap, is an area of memory shared by each thread that stores data such as class information, constants, static variables, and immediately compiled code, which have been loaded by the virtual machine.

Run a constant pool, which is part of the method area.
7. Direct Diret Memory
Direct memory is not an area of memory defined in the Java Virtual Machine specification, but frequent use can also cause outofmemoryerror anomalies to occur.
A partial operation of NIO, which allocates out-of-heap memory directly using the native library, then operates through a Directbytebuffer object stored in the Java heap as a reference to this memory, avoiding copying data back and forth in the Java heap and native heap, Performance can be significantly improved in some scenarios.

8. Mainstream object access, using handles and direct pointers.
The advantage of using handle access is that a stable handle address is stored in reference, and only the instance data pointers in the handle are changed when the object is moved, and the reference itself does not need to be modified.
Faster access with pointers saves time overhead for pointer positioning.

3rd Chapter Virtual machine performance monitoring and fault handling
What does GC and memory allocation mean?
When you need to troubleshoot various memory overflows, memory leaks, and when garbage collection becomes a bottleneck for the system to achieve higher concurrency, you need to implement the necessary monitoring and tuning of these "automated" technologies.
1. Reference counting algorithm
Add a reference counter to the object, and whenever there is a place to refer to it, the counter value is incremented by 1, and when the reference fails, the counter value minus 1 ' is no longer possible to use the object with zero counter at any time.
However, there is a large shortage of reference counting methods, and it is difficult to solve the mutual circular reference problem between objects.
The following code describes a scenario for the reference counting method:

public class REFERENCECOUNTGC {public    Object instance=null;    private static final int _1mb=1024*1024;    Private byte[] Bigsize=new BYTE[2*_1MB];    public static void Testgc () {        referencecountgc obja=new referencecountgc ();        REFERENCECOUNTGC objb=new REFERENCECOUNTGC ();        OBJA.INSTANCE=OBJB;        Objb.instance=obja;        Obja=null;        Objb=null;}    }

If you simply judge the state of an object by reference counting, both objects are not recycled by GC because they are circular references to each other.

2. Root Search algorithm
A series of objects named "GC Roots" are used as starting points from which to search down, and when you reach objects that are not on the GC Roots reference chain, even if they are related to each other, they are still considered recyclable objects.
Using the theory of graph theory, can be expressed as GC roots to the object unreachable .
3. Better understanding of "references"
In Java, the first definition is traditional: If the value stored in the data of the reference type represents the starting address of another piece of memory, it is said that this memory represents a reference.
Such a definition is too absolute in garbage collection, then the concept of reference is expanded, the reference is divided into strong references, soft references, if referenced, virtual reference, four kinds of reference strength gradually weakened.

Deep understanding of Java Virtual machines: JVM advanced features and best practices learn note ⅰ

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.