JVM Series (i) JVM startup process and infrastructure

Source: Internet
Author: User
Tags exception handling garbage collection

"Great haste makes great waste"



JVM startup process


The JVM is the environment in which the Java program runs, as well as an application process of an operating system, so it has its own lifecycle and its own code and data space.

The JVM works and features mainly refers to the operating system loaded into the JVM, through the JDK Java.exe to complete, through the following 4 steps to complete the JVM environment.


1. Create JVM mount Environment and configuration

JVM loading environment, the JVM provides the way the dynamic connection file of the operating system


2. Loading JVM.dll

The path to the JVM has been found by the first step, and Java is LOADJAVAVM to mount the JVM.dll file. Loading work is simply invoking Windows API functions:

LoadLibrary load JVM.dll dynamic Connection library. Then the derived functions in the JVM.dll are JNI_CREATEJAVAVM and jni_ The Getdefaultjavavminitargs is attached to the CREATEJAVAVM and Getdefaultjavavminitargs function pointer variables of the invocationfunctions variable. JVM.dll's loading work was declared complete.


3. Initializing JVM.dll and hanging bounds to jnienv (JNI call interface) instance

This allows the JVM's functions to be invoked in Java. The invocation of INVOCATIONFUNCTIONS->CREATEJAVAVM, the Jni_createjavavm method in the JVM, obtains an instance of the JNIENV structure.


4. Invoke the JNIEnv instance to mount and process the class class.



The basic structure of the JVM


The JVM system is essentially a two-JVM internal architecture divided into three subsystems and two major components: the class loader (ClassLoader) subsystem, the executive engine subsystem, and the GC subsystem, which are memory-run data regions and local interfaces.


· Program counters (PC registers)

The PC (program Couneter) registers are private to each thread, and the Java virtual Opportunity creates a PC register for each thread, and at any moment a Java thread is always executing a method called the current method, if the current method is not a local method. The PC register always executes the instruction currently being executed and, if it is a local method, the PC register value is underfined. Each command PC is automatically added, so the PC stores the next instruction address to be executed. The JVM uses a PC to track the location of the instruction execution, and the PC is actually a memory address that points to the method region.

· Method Area (permanent generation)

The method area stores information for each class, such as a constant pool of types, fields, method information, method byte codes. All threads share the same method area, so the dynamically linked processes that access the method area data must be thread safe. If two threads attempt to access a field or method of a class that has not yet been loaded, it must be loaded only once, and two threads must wait until it is loaded to continue.


(In JDK1.7, a string constant pool placed on a permanent generation has been moved to the heap.) JDK1.8 undo the permanent generation and introduce the meta space. The meta space is directly in memory, not in the Java Virtual machine, so the dimensions depend on the memory size. Of course, you can also customize the size of the dimension. )

· The method area does not require contiguous memory, optionally a fixed size or extensible. And you can also choose not to implement garbage collection. Garbage collection behavior is relatively rare in this region, but not that data enters the method area as "permanent" as the name of the permanent generation. This area of memory recovery target is mainly for the collection of constant pools and the type of uninstall, generally speaking, the region's recovery "performance" is more difficult to satisfy, especially the type of uninstall, the conditions are very harsh, but this part of the recovery is indeed necessary. The OutOfMemoryError exception is thrown when the method area is unable to meet the memory allocation requirements.

· Heap (HEAP)

The application system objects are stored in the Java heap, and the heap is used to allocate class instances and arrays at run time. Arrays and objects cannot be stored on the stack. Because the stack frame is designed to be created, it cannot be resized. The stack frame stores only references to objects or arrays in the heap. Unlike the original type and reference type in a local variable array (in each stack frame), the object is always stored on the heap so that it is not removed at the end of the method. objects can only be removed by the garbage collector. All threads share the Java heap.

Brief Introduction to garbage collection

In order to support the generation of garbage collection mechanism, heap memory can be divided into the Cenozoic and the old age two regions (the default Cenozoic and old age space size is 1:2). The Cenozoic can be divided into the Eden area, the from Survivor area and the to Survivor Area (the ratio of three to 8:1:1). Almost all the new objects were created in the Eden area. In the process of garbage collection (GC), active objects in Eden are transferred to the Survivor area, when it reaches a certain age (the number of minor GC, every time after a cenozoic recovery, if the object survives then its age increases by 1, the object reaches a certain age), it will be transferred to the old age.

Replication algorithm

· Java stacks (Java stack)

A Java stack is a thread-private area of memory in which a stack frame is stored. , each time a method call creates a frame, and presses the stack, and the call completes the stack. The following is a thread-public private schematic of memory:

If the method MethodOne method calls Methodtwo, then MethodOne creates a stack frame first into the stack, then methodtwo into the stack as the top of the stack (assuming no other method executes), Methodtwo executes the first stack, Then MethodOne execute the stack.


Generally consists of three parts: local variable table, operation data stack and frame data area

Local Variables table: There are 8 basic data types (boolean,byte,char,short,int,float,long,double), object references, and returnaddress types that can be stored. where long and double, because they are 64 bits, occupy two of the space of local variables.

Each block is a stack, as shown in the figure is two stacks

In the Java Virtual Machine specification, two exceptions are specified for this area: if the thread requests a stack depth greater than the depth allowed by the virtual machine (such as the recursive call Y-time), the Stackoverflowerror exception is thrown If the virtual machine stack can be dynamically expanded (most Java virtual machines are currently dynamically extensible, except that fixed-length virtual machine stacks are also allowed in the Java Virtual Machine specification), and OutOfMemoryError exceptions are thrown when the extension cannot be applied to enough memory.


Operand stack: The main preservation of the intermediate results of the calculation process, as well as the temporary storage space for the variables in the calculation process.
The following figure is the process of adding an operand stack of two numbers:

Frame Data area: In addition to the local variables table and the operation data stack, the stack needs some data to support the parsing of the constant pool, where the frame data area holds the pointer to the constant pool, the convenience program accesses the constant pool, and the virtual machine must have an exception handling table when the function returns or an exception occurs. It is convenient to send an exception to find the exception code, so the exception handling table is also part of the frame data area.


Allocation on stack

Small objects (generally dozens of bytes), in the absence of escape, can be directly allocated on the stack

Directly distributed on the stack, can automatically recycle, reduce the GC pressure

Large object or Escape object cannot be allocated on stack

· Local methods Stack (Native method Stack)

The local method stack is also a thread-private memory area, which is similar to the Java stack, except that the region is primarily about saving data related to native methods. The native method is a method written in a non-Java language.

As with virtual machine stacks, the local method stack area also throws Stackoverflowerror and OutOfMemoryError exceptions.





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.