Introduction to the Java language and JVM virtual machines

Source: Internet
Author: User
Tags garbage collection

One, the Java language

1.1 Support OOP for object-oriented programming

Emphasis on support, because Java can also be process-oriented programming.

The three main features of OOP are encapsulation, inheritance, polymorphism.

Encapsulation is mainly for member variables, the idea of OOP requires that member variables are private, should not be accessible externally, a class that conforms to the OOP idea should be accessible only by public methods;

Inheritance, the main understanding of the inheritance system, private, protected, public in the inheritance of the use of the scene. Understand that Java is a single-inheritance multi-implementation (and the difference from C + +);

Polymorphism means that an instance of a class is determined by the runtime, not by the declaration. Parent Class A = new subclass (); yes. This function can be oriented to abstract programming, interface-oriented programming, the object does not have to be consistent with the declared class, as long as its subclass, grandson class, etc. can be;

1.2 JDK version improvements to the Java language

1996 release of Jdk1.0,java language with basic OOP syntax;

1997 release of jdk1.1, introduction of internal classes;

2004 release jdk1.5, introducing syntactic sugars, automatic disassembly boxes, generics, dynamic annotations, enumerations, variable-length parameters, traversal loops (foreach);

2014 release JDK8, introducing lambda expressions;

Second, JVM virtual machine (this article refers to the official default hotspot virtual machine)

2.1 Development

Developed by a small company Longview Technologies, was acquired by Sun Company in 1997, and formally became the official default virtual machine after jdk1.3;

Hotspot named from its hotspot code detection technology, can effectively detect hot spot code, and the use of the JIT compiler to further optimize the hot code and compile into machine code, improve operational efficiency;

2.2 JVM Virtual machine memory Area composition

program counter, thread private, point to byte code instruction;

Java Virtual machine stack, thread-private, mainly describes the Java method, with the program counter step-by-step execution method (understand why the stack);

Local method Stack, thread-private, similar to the Java Virtual machine stack, the difference is that it is used to execute non-Java methods;

Java heap, thread sharing, this is the largest piece of virtual machine memory area, mainly our new object will be allocated here, here is divided into the Cenozoic (Eden, Survivor1, Survivor2) and the old age;

Method area, thread sharing, in a hotspot called permanent generation (Permanent Generation), Storing loaded class information, constants, static variables, and so on, static code block, static variable, static method will be stored here there is a copy. Why is called the permanent generation, mainly to this part of the object instance recycling efficiency is not high, this part of the object instance survival rate is high;

Running a constant-rate pool is part of the method area;

Direct memory, not part of the virtual machine memory, refers to the memory that is requested outside of the virtual machine memory.

2.3 Garbage Collection algorithm

How to judge the object can be recycled? There is a reference counting algorithm and an accessibility analysis algorithm. The reference counting algorithm is very simple, give each object a reference counter, whenever there is a place to reference it, then give it counter +1, when this reference expires counter-1, this is very efficient, but there is a flaw is mutually referenced object, cannot be recycled, causing memory leaks. Hotspot uses the accessibility analysis algorithm to determine whether an object can be recycled from the GC roots object, the GC Roots object includes the object referenced by the virtual machine stack, the object referenced by the method area class static property, the object referenced by the method area constant, and the object referenced in the local method stack;

IBM Research pointed out that 98% of the objects are to die, so the new generation of recovery frequency is higher, each can recover a large amount of memory, the old age after more than two times the recovery is still alive, indicating that the efficiency of recovery is not high, recovery frequency can be lower. In addition, large objects are not allocated in the Cenozoic, but directly into the old age.

① tag-Clears the algorithm. The idea of the algorithm is to first label the objects that need to be recycled, and then to purge the collection uniformly. Implementation is simple, but the efficiency of labeling and purging is not high, but also generates a lot of discontinuous memory space, affecting the subsequent allocation of memory for new objects, especially large objects.

② copy algorithm. For the mark-clear algorithm problem, the idea of the replication algorithm is to divide the memory area evenly into two blocks, such as 10M of memory equal to two blocks of 5M, each time can only use a piece, the first tag from the object still alive object, and then uniformly copied to the second block of memory, and in the memory space order, The first block of memory is all recycled. The second recovery begins with the second block and repeats itself. This is efficient and the memory space can be allocated continuously. But the problem is that 10M of memory can only be used in half, resulting in a waste of memory.

Hotspot in the actual implementation of the replication algorithm, the memory space is divided into Eden and two survivor, and the default Eden and Survivor Scale is 8:1:1, the new object is allocated in Eden, the first time after the recovery of the surviving objects are copied to survivor 1, Eden clears all, the second new object is still allocated in Eden, and the surviving objects in Eden and Survivor 1 are copied to survivor 2 in the second collection, and Eden and Survivor 1 are all cleared.

Here's a question: What if the surviving object exceeds 10% of the memory? This is the time to make a distribution guarantee (Handle Promotion) from the old age.

③ tag-collation algorithm. And the new generation, GC recovery in the old age is not too high, the use of replication algorithm survivor space is probably not enough, if the survivor large and wasted memory space, then put forward the marking-sorting algorithm to deal with the actual situation of the old age. Tag-Grooming algorithm when memory is reclaimed, all surviving objects are marked, but not purged, but the surviving objects are moved toward one end of the memory, then the memory ends are recyclable objects that are cleared when the recoverable objects are "extruded" into the memory boundary.

Since the Java heap in the hotspot is divided into the new generation, the old age, their chances of survival is not the same, so according to the new generation and the old age to take different algorithms, which is called the generation of collection algorithm, in the new generation of the replication algorithm, in the old age using the mark-clear or marker-collation algorithm.

2.4 Garbage collector

The above analysis of the new generation, the old generation should adopt what algorithm, hotspot in the actual application scenario, the implementation of different garbage collector;

①serial collector, copy algorithm, single thread, client mode default Cenozoic collector. There will be stop the world problem;

②parnew collector, copy algorithm, multi-threading, new generation collector;

③parallel scavenge collector, copy algorithm, multi-threading, Cenozoic Collector, differs from parnew in that it is designed for throughput;

④serial old collector, tag-collation algorithm, single-threaded, client-mode default age collector;

⑤cms Collector, Tag-purge algorithm, multi-threading, old age collector, to reduce the pause time for the target, only in the initial marking, re-tagging when the need to stop the world, to take concurrent tagging and concurrent cleanup to reduce the pause time;

⑥G1 Collector, Tag-collation algorithm + copy algorithm, multi-threading, old age collector;

Third, compile and run

3.1 javac Compiler compilation

The first compile, compile the. java file into an intermediate language, and output the. class file, during which the main syntax analysis and lexical analysis (compiling principle), annotation processing, semantic analysis (parsing syntax sugar, etc.), the generation of byte code class file;

3.2 Interpreter

Class loading into the interpreter run, the process of class loading is: loading, validation, preparation, parsing, initialization;

3.3 JIT Instant compiler

The interpreter monitors the hotspot code to provide monitoring data for further compilation of the JIT compiler, triggering the JIT compiler to compile the hotspot code into machine code;

I have limited knowledge, the article inevitably has flaws, please do not hesitate to correct the errata.

Introduction to the Java language and JVM virtual machines

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.