Java Virtual Machine JVM memory partition, and code enforcement mechanisms

Original link: https://blog.csdn.net/zhangren07/article/details/6270842

1. JVM architecture

 

1 JVM Architecture FIG.

 

 

   The method area: the type of information stored JVM loaded. Comprising: a basic information type, constant pool, field information, the information method, class variables, ClassLoader reference point, a reference Class class method table and the like.

(Corresponding to -PermSize JVM memory configuration, etc.)

   java heap: class instances and arrays created programs, including class exception objects and objects stored in the heap. In addition to the stack object instance data storage device, but also a pointer to the object stored in the method area type information.

(All JVM threads shared heap space, JVM memory configuration corresponding to -Xms -Xmx and the like)

   java stack: When the JVM creates a new thread, the thread will have a counter (PC Register) and stack. Each method invocation stack frame is generated, the stack frame contains the local variables and operand stack.

(JVM stack of the thread to be exclusive, JVM memory configuration corresponding to -Xss)

  Thread counter: each thread has its own program counter, which points to the next instruction. When a thread calls the native method, it is undefined.

   Native method stacks: When the JVM thread calls a native method stacks will jump into the native method. Java stack method may jump back again after the native method returns.

(JVM supports native method call, so the JVM memory occupied by the OS may exceed the JVM heap memory size, and even produce local memory leaks)

 

2. Java code is compiled loading mechanism

 

  Java source code compilation mechanism

Analysis  input to the symbol table: string java file is parsed syntax tree and generate a symbol table.

 annotation process: The annotations generated code, and then lexical and syntax analysis.

 semantic analysis and generate class files: checking whether a variable declared prior to use, whether to capture abnormal, the type of inspection semantic information. Generating class files include not executable bytecode class files further comprises a version number, interface information, attribute classes, constants, metadata, and the exception handler lists superclass and implementation class.

The above three steps is a "lexical analysis - parsing> -> Semantic Analysis" sequential build process.

  JVM class loading mechanism

 Load: The class files (bytecodes binary) is loaded into the JVM.

 link: check class file format, loading classes associated interfaces and classes, access to properties, methods, and methods for verification and so on.

 initialize: performs static initialization code, constructors, static attribute initialization, class initialization is not finished loading of a process must be performed, but no later than before the implementation of the first major use of the object. Initialization trigger condition, comprising: 1) a call to new; 2) reflecting a class method calls; 3) calls the initialization subclass; 4) in the JVM startup procedure specified initialization class.

  Sun JDK Class Loader inheritance


As shown above, the JVM class loader uses a tree structure, except Bootstrap class loader, class loader remaining before loading a class, which will go to see whether the Parent loader loaded, has been up to root. From the class loader object is to avoid the root to the leaves on a path of a different class is the class loader to load a plurality of times repeated. User-defined class loader, need to inherit the abstract class ClassLoader.
(Explain in detail see: HTTP: //www.ibm.com/developerworks/cn/java/j-lo-classloader/index.html)

3. enforcement mechanism the Java class
  byte code interpreted
 Definition: The stage will compile source code source is compiled (a form of intermediate code) the JVM byte code, interprets and executes them at runtime the JVM, this manner becomes a bytecode interpreter implementation.
 procedure: bytecode instructions are generic format that can identify all of the JVM, the format is similar to assembly language format, such as astore, aload, pop, return the like stored number, read the number of the stack, function return, etc. instruction. JVM interprets these commands, and execute them, the JVM thread stack and manage its program counter, etc. during execution.
 open the learning point: the stack cache portion of the shared stack frame
  compiler implementation
 Objective: To improve the performance of code execution (interpreted less efficient), Sun JDK bytecode compiler provides support for the machine code. Compiled at runtime, often referred to as JIT compiler.
 Hotspot VM: Sun JDK in the implementation process of the high frequency of execution code is compiled, that you do not continue to use the code frequently by way of explanation, so Sun JDK again become Hotspot VM.
 compilation mode: -client and -server. By default, a pattern JVM is enabled according to the default configuration of the machine, but which can also be specified by way JIT mode to the JVM transmission parameters. There are more than two physical processors and more than 2G physical memory of the machine will enable server mode, client mode or enabled by default.
 performance test environment: According to the application server configuration, JBoss will default set -server mode.
 JDK Tools: different modes, JDK Tools function point may be different, more under Server mode.
  Compiler implementation performance optimization
 Client mode: also known as C1 models, mainly inline methods when compiled to native code, to optimize virtualization and redundancy deleted.
 Server mode: mode is also called C2, C1 optimized on the basis, will be replaced scalar (point.x variables will need to reference a local variable x in code to reduce access time to the reference), allocated on the stack ( when the object is not referenced in vitro methods, may be directly allocated on the stack, the object method is performed automatically recovered after completed) and deleted synchronization (synchronized write mode but not actually remove sync synchronous access) three forms of optimization.
After  be optimized JVM, if not compiled machine code optimized conditions are met, it will perform the way back to explain. Such as: the original code is not synchronized synchronous access later visited; the original is only one class that implements the interface is now used in a number of implementation classes and so on.
 extended learning points: OSR (On Stack Replace) compiler mechanism, JDK 7 multi-layer editing mechanisms.
  Interpreted vs. compiled bytecode execution
 interpreted more than save memory compiler implementation, the start interpretive execution speed faster startup
 compiler implementation of higher performance
 interpreted or compiled execution counter: -CompileThreshold (method has been called number), - OnStackReplacePercentage (whether compiled triggering threshold OSR)
4. reflected performs
  the reflection process
 dynamically loaded class type not known in advance, and performs a specified method, Java mainly reflected a common concept supported through the class class and java.lang.reflect package.
 reflection such that the frame can be very flexible implementation of the code calling the object's
  performance concerns
achieve  enhance the flexibility of the reflective code, but the entire process is more complicated than a direct call to compiled into bytecode, so performance than direct execution slower.
 Sun JDK executed as reflected in the performance of JDK version upgrade is rising, not performance after 6 JDK and direct implementation gaps.
 getMethod relative consumption performance, and better performance method.invoke (direct calls only a little lower than)
  the performance test Reflection
 reflected dynamically generated class object, so that the load will increase in memory usage Perm region in which the JVM is running.
 Dead Class Object in the JVM GC will be recovered out of the normal recovery, specific trigger mechanism is what?
 
 

Guess you like

Origin blog.csdn.net/tiantian1980/article/details/102721892