"Read the book, leave the Trail." Deep understanding of Java virtual Machine __java

Source: Internet
Author: User
Preface

Recently found that sometimes after reading a book, time is easy to forget, reading does not summarize the effect of a large discount, it is intended to write this series of articles, one is to tidy up the main points in the book, to help their digestion and understanding; the second is to encourage themselves to read more thinking. The article will not explain the contents of the book is very detailed, but summed up, suitable for the reader has read the book. 2nd Chapter: Java memory area and memory overflow exception

(1) JVM Runtime data area



Program counter
The line number indicator of the byte code executed by the current thread Java Virtual machine stack
Thread private base data type, reference type local method stack
Similar to Java Virtual machine stacks the former is for executing Java method (bytecode) service, the latter is the native method services Java heap
All object instances and array allocations

Method area loaded with class information, constants, static variables, Just-in-time compiled code, and so on the regular volume pool of data

Special: Direct Memory NiO class, can directly allocate the outer heap memory Directbytebuffer, by the total memory limit of the machine

(2) OutOfMemoryError exception Java heap overflow
Infinite new object virtual machine stack, local method stack Overflow
Infinite recursive function, infinite create thread method area, run frequent pool overflow
Infinite generating string causes a constant pool overflow to cause a large number of classes to overflow the method area (using reflection, etc.) for direct memory overflow
Padding Directbytebuffer the 3rd chapter: garbage Collector and memory allocation policy

(1) The algorithm of accessibility analysis for judging object survival reference counting algorithm
Implementation: GC Root: The object referenced in the virtual machine stack, the object referenced by a class static property in the method area, and a constant reference in the method area. Virtual machines can directly learn where to store object references, such as using OOPMAP data structures in hotspot virtual machines. Strong reference: Never recycle soft references: Recall weak references before memory overflow: Next GC is recycled virtual reference

(2) Garbage collection algorithm mark-Clear algorithm
Simple, inefficient, resulting in a fragmented replication algorithm
High efficiency, waste spatial labeling-finishing algorithm
No fragmentation collection algorithm
New generation, old age

(3) Garbage collector



Serial collector
Single-threaded, simple and efficient, stop all worker threads when collecting (stop the world) apply to virtual machine parnew collector in client mode
Serial's multi-threaded version, Stop the world, only serial and parnew can work with CMS using virtual machines in Server Mode preferred generation collector Parallel scavenge collector
Based on optimized throughput, both of these are tasks that fit in the background and do not require too much interaction based on the optimal pause time serial old collector
Is the Parallel old collector for virtual machines in client mode
With parallel scavenge, "throughput first" collector CMS collector
The CMS (Concurrent Mark Sweep) collector is a collector's advantage for getting the shortest recovery pause time: Concurrent collection, low pause disadvantage: Consuming CPU, unable to handle floating garbage, unable to concurrency G1 collector based on tag clearly triggering memory collation
Features: Parallel concurrency, generational collection, spatial integration, predictable pauses

(4) Memory allocation priority in the new generation of Eden region to allocate large objects directly into the old age of the long-term survival of the object will enter the old age (object age counter) dynamic object ages determine the 6th chapter: Class file Structure

(1) System-independent implementation of programming language and operating system is based on the independence of the virtual machine and bytecode storage format, Java Virtual machine is not included in any language, including Java binding, it only with the "class file" This specific binary file format association
Programming language-> compiler-> byte code (. class file)-> virtual machine

(2) class file structure any one class file corresponds to the definition information of the unique classes or interfaces, but the class or interface is not necessarily defined in the class file, or it can be generated directly by the ClassLoader class file containing code and metadata for supporting code (metadata)
The code part is the bytecode metadata section that includes such things as class names, member name, method signature, Chang, method size, evaluation stack occupancy, etc. many information class file formats include magic number, Chang, Access flag, class index, parent class index, interface index, field set, method set, etc.

7th Chapter: Virtual Machine class loading mechanism

(1) noun virtual machine class loading mechanism (class loading)
The virtual machine loads the description class data from the class file into memory, verifies and transforms the data, resolves and initializes the Java type that can be used directly by the virtual machine to load
One-step ClassLoader in class loading process



(2) The timing of class loading the virtual machine specification does not enforce the timing of the load, but it strictly stipulates that there are only 5 situations in which the class must be initialized immediately
Encountered new,getstatic,putstatic,invokestatic these 4 byte code instructions, common even with the new keyword, read or set the static field of a class (except the final modified) Initializes a class when a class is reflected using the Java.lang.reflect package, but when the virtual machine starts when its parent class is uninitialized, the user needs to specify a main class to perform when using JDK1.7 Dynamic language support .... (less)

(3) class loading process loading
3 things to do with a virtual machine
1. Gets the binary byte throttling 2 that defines this class through the fully qualified name of a class. Converts the static storage structure represented by this byte stream to the RUN-TIME data structure of the method area 3. Generates a Java.lang.Class object representing this class in memory, as the access gate to various data for this class of the method area After the load phase is complete, the binary byte streams outside the virtual machine are stored in the method area in the format required by the virtual machine, and the data storage format in the method area is validated by the virtual machine.
Objective: To ensure that the information contained in the byte stream of a class file meets the requirements of the current virtual machine and does not compromise the security of the virtual machine itself:
File format verification: Only through this phase, the byte stream will enter the memory of the method area for storage, so the next 3 verification phases are all based on the method area of the storage structure, no longer directly manipulate the byte stream metadata validation bytecode verification symbol reference verification preparation
Formally allocate memory for class variables and set the phase of the class variable's initial value, the memory used by these variables will be assigned in the method area.
1: The memory allocation includes only the class variable (the variable that is modified by static), not the instance variable, and the instance variable is allocated 2 in the Java heap when the object is instantiated: The value of the initial value is 0 of the type, and the actual assignment is parsed at the initialization stage.
Replaces a symbolic reference within a constant pool with a procedure that is directly referenced.
Initialized to actually execute the Java program code (that is, bytecode) defined in the class

(4) Class loader

Definition: In the class load phase, "Get the definition of binary byte throttling by a class by its fully qualified name" This action is implemented outside the Java Virtual machine, and the code that implements this action is called the "ClassLoader"

To determine whether a class is equal 1: Whether 2 is loaded by the same loader: is the class itself equal

3 System-supplied Java Program class loader 1: Boot class loader: Responsible for loading class libraries under \lib into virtual machine Memory 2: Extended class Loader: Load \lib\ext Class Library 3: Application class Loader: Class library loading on user path

Parental delegation Model: In addition to the top-level boot class loader, the rest of the ClassLoader has its own parent loader working process: When a class loader receives a class load request, the request is first delegated to the parent ClassLoader to complete, and only the parent class loader feedback cannot be completed, and the child loader attempts to load Benefits: The Java class has a prioritized hierarchy with its classloader, as detailed in the 8th chapter, "Determining whether classes are equal" : Virtual machine byte code execution engine

The execution engine for all Java virtual machines is consistent: The byte code file is entered, the process is the equivalent of bytecode parsing, output is the execution result

Many Java Virtual machine execution engines have interpreted execution (through interpreter execution) and compilation execution (resulting in native code execution via an instant compiler) when executing Java code Chapter 10th: Early (compile-time) optimization

(1) Overview front-end compilers: Javac
The process of converting. java files into. class files JIT compiler: HotSpot vm C1,C2 Compiler
Instant compilers, the process of converting bytecode into machine code

(2) The front-end compiler compilation process is roughly divided into 3 processes 1: Parsing and populating the symbol table process
Lexical analysis, syntax analysis, etc. 2: annotation processing process of the Insert annotation Processor 3: Parsing and bytecode generation process
Syntactic sugar parsing, byte code generation

(3) syntactic sugar paradigm and type erasure
C # 's fan-type system is generated during run time, have their own virtual method table and type data, that is, type expansion, is the real generic Java language paradigm is based on type erasure, that is, the conversion of the original type in the compilation process, and the response to local insertion of the cast, pseudo-fan 11th: late (run-time) optimization

Just-in-time compilers: In order to improve the efficiency of hot code execution, at runtime, the virtual machine will compile the code into the local platform-related machine code

(1) Pros and cons interpreter
Quick Start, fast execution, save memory compiler
Time lapse, play a role, enhance execution efficiency

(2) methods to determine how hot code is invoked multiple times
Hot-spot detection based on sampling: periodically check the top of each thread, statistics which method appears high frequency based on counter hot Spot Detection: Method call counter is repeatedly executed loop body
Counter-based hotspot detection: back-edge counter

(3) compiler optimization technology common subexpression elimination
If an expression e has already been computed and has not changed from the previous to the present, then the data boundary check can be eliminated by using the previous calculation result
The boundary check of the data is not required to be checked at run time without leaking the method inline
Reduced function call Escape analysis
An object is defined in a method and is referenced by an external method, called escape, a method escapes, a thread escapes if it is proved that an object does not escape to a method or thread, you can:
On-Stack allocation synchronization eliminates scalar substitution: splitting objects into 12th chapter: Java memory model and thread

Java Virtual Machine specification attempts to define a Java memory model to mask memory access differences for various hardware and operating systems

Primary goal: Define access rules for the variables in the program-the low-level details of storing variables in a virtual machine in memory and taking them out of memory

(1) The Java memory model Java memory model stipulates that all variables are stored in main memory (similar to object instances in the Java heap). Each thread also has its own working memory (similar to a partial domain of the virtual machine stack), and the thread's working memory holds copies of the main memory copies of the variables used by the thread. All operations of a thread on a variable must be done in working memory, not directly in the main memory. There is no direct access to the variables in each other's working memory between threads, and the transfer of variable values between threads needs to be done through main memory.

(Supplementary map)

(2) Java memory model characteristics of atomicity
To ensure the visibility of atomic operations
When a thread modifies the value of a shared variable, other threads can immediately learn about the modified order
To sum up: observe in this thread, all operations are orderly; on another thread, all operations are unordered.

(3) Java thread Implementation kernel thread implementation user thread implementation user line Chengga lightweight process hybrid implementation

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.