The first chapter memory structure study notes

To understand why the virtual machine?

JVM Java language is not just only support, but also support other languages (Scala, Kotlin, Groovy, etc.).
Square block chain ether 2.0 (Bitcoin block chain is 1.0) provides the EVM virtual machine, JVM and its implementation Similarly, stack-based, generate a script compiled into bytecode to execute. General knowledge (theoretical than practical).

Virtual machine history - interpreted and compiled execution (bytecode for execution):

Interpreted edge is translated into machine code execution side, and then executed after the time compilation (compiled executables) it is first of all a method bytecode into machine code compiled all. Hotspot uses to explain the implementation, at a certain timing after hot Code (repeatedly, cycles, etc.) and then translated into machine code. Hot Code Detection Technology (find the code to compile the most value by executing counter, if the code is used very frequently, these codes will be compiled to native code).

JRockit is the method adopted in the implementation class directly compiled to machine code (Java program starts may be slow). J9 and Hotspot closer, mainly used in (on IBM WebSphere and IBM AIX platforms) IBM products, Huawei some projects with J9. ,

Google: Google Android Dalivk VM: register architecture to use, do dex (Dalvik Executable) through conversion from class.

The future of Java technology

  • Modular: OSGI (dynamic, modular), micro application level service is the development direction of the Internet.
  • Mixed-Language: Multiple languages ​​can be run in the JVM, google the Kotlin became the official language of Android, Scala (Kafka).
  • Multi-core parallel: CPU transition from high frequency multi-core, multi-core era. JDK1.7 introduced Fork / Join, JDK1.8 proposed lambda expression (programming function inherently suitable for parallel operation).
  • Rich syntax: JDK5 proposed autoboxing, generic (concurrent programming mentioned), dynamic annotations and grammar. JDK7 binary native support. try-catch-finally to try-with-resource.
  • 64: Although the same program 64 memory consumption, but large memory support to a little more than 32, so the virtual machine will be full transition to 64-bit, 32-bit JVM heap size limit of 4G.
  • More garbage collector (now mainstream CMS, G1): JDK11 -ZGC (pause time of less than 10 milliseconds, and does not increase with the increase of the heap, TB levels of recycling pile)): colored pointer, loaded barrier. JDK12 uninstall support concurrent classes, to further reduce the pause time JDK13 (scheduled for September 2019) will be increased to the maximum heap size from 4TB 16TB.

A, Java SE Architecture

Here Insert Picture Description
JavaSE: Java Platform, Standard Edition, provides the basis for Java EE and Java ME.
JDK: Java Development Kit, JDK is a superset of the JRE, JRE all types of content, as well as compilers and debuggers and other tools needed to develop the program.
JRE: Java SE runtime environment that provides libraries, Java Virtual Machine, and other components to run programs written in the Java programming language. The main library, including: deployment release, the user interface tools, inheritance libraries, other libraries foundation, the basis of language and tool libraries.
JVM: java virtual machine, responsible for JavaSE platform hardware and operating system independence, compile and run the code (bytecode) and platform security.

1, JVM overall introduction:

Here Insert Picture Description
JVM = class loader (classloader) + execution engine (execution engine) + runtime data area (runtime data area)

1.1 What is jvm?

(1) jvm is a specification for A computing device, which is out of an imaginary machine that is implemented by a computer simulation of the actual various functions.
(2) jvm comprising a bytecode instruction set, a set of registers, a stack, a heap garbage collection method and a storage domain.
(3) JVM mask information associated with a particular operating system platform, so that only the Java program to generate object code (bytecode) running in the Java virtual machine can run without modification on multiple platforms.
JVM byte code, when executed, in fact, eventually interpreted bytecode into machine instructions executed on a particular platform.

1.2 runtime data area

Java virtual machine memory division during the execution of the Java program in which it will manage for a number of different data areas. These regions have their own purposes, and the time of creation and destruction, some areas along with the virtual machine process exists, some regions are dependent on start and end users to create and destroy threads while.
Thread private: a program counter, the virtual machine stack, native method stacks; threads share: heap, the method area.

This is an abstraction, the internal implementation-dependent registers, cache, main memory (JVM specifically to analyze the source code C ++ language, no need to look at). = + Instruction operation of the computer data, instructions for performing a method, data and objects used to store data.

Virtual machine stack: the implementation of java method, native method stacks - native method execution, the program counter (counter program execution).
Data in Java: variables, constants, objects, arrays related.
Here Insert Picture Description

Second, the thread-private area

1, the program counter

Thread private, its life cycle and the same thread. It can be seen as the line number indicator bytecode executed by the current thread. In the conceptual model of a virtual machine's (only a conceptual model, a variety of virtual machine might go through some achieve more efficient way), when the bytecode interpreter at work is to be selected by changing the value of a counter to be executed bytecode instructions, such as: branching, looping, branching, exception handling, recovery threads (multithreaded switch) and other basic functions.

If the thread is executing a Java method, the counter records the address of the virtual machine bytecode instruction being executed; if the method is being performed Natvie, this counter value is null (undefined). Size of the data stored in the program counter space occupied with the execution of the program will not be changed, so that this region does not appear OutOfMemoryError case. Independent memory among threads of each other.

2, the stack virtual machine (JVM execution routine subsequent detailed views)

2.1 The concept of the stack

The method of calling features consistent data structure and characteristics of the method in java. (Why use JVM stack - demo code StackFilo)
anomaly: StackOverflowError: thread stack depth is greater than the requested virtual machine allowable depth
is impossible to request enough memory to the JVM dynamic expansion: OutOfMemoryError

2.2 virtual machine stack (Java Virtual Machine Stacks)

Like the program counter, Java virtual machine thread stack is private, its life cycle and the same thread. Is a virtual machine stack Java memory model described method of execution: when each method will be executed at the same time create a stack frame (Stack Frame) for storing local variable table, operation information stack, dynamic linking, method exports. Each method is called until the completion of the execution procedure, a stack frame corresponds to a virtual machine from the stack to the stack of a process stack.

Has often been divided into the Java memory heap (Heap) and stack memory (Stack), that the task was a bit rough, divide Java memory area is actually complicated than this. Popular this division can only suggest that most programmers are most concerned about, the relationship with the object of memory allocated memory area is most closely these two. Which referred to "stack" will be devoted in the back, while referring to "stack" is the story of the virtual machine stack, or a local variable table portion of the virtual machine stack.

The local variable table stored compile various known basic data types (boolean, byte, char, short, int, float, long, double), object reference (reference type, it is not equivalent to the object itself, depending on the virtual machine implement, it may be a reference to the start address pointer to the object may also be a handle to the object representing the object associated with this or other location) and returnAddress type (address pointing to a byte code instruction). Wherein the long and double-length 64-bit data occupies two local variable space (Slot), the remaining data occupies only one type. Desired local variable table created during compilation memory space allocated, when entering a method, this method requires much space for local variables allocated in the frame is completely determined during operation of the method does not change the size of the local variable table.

In the Java Virtual Machine Specification, this area provides two anomalies: If the stack is greater than the depth of the thread requested virtual machine allowable depth, StackOverflowError will throw an exception; if the virtual machine can dynamically expand the stack (most of the current Java virtual machines can be dynamically expanding, but the Java virtual machine specification also allows virtual machine stack fixed length), when the expansion can not apply enough memory will throw OutOfMemoryError exception.

Note: The default stack size is 1M, available parameters -Xss resized, e.g. -Xss256k

When the compiler code, stack frame requires much local variable table, deep stack operands have been completely determined, and written into the Code attribute table in a method, a stack frame so how much memory allocation, It will not be affected by the program runtime variable data, but only depending on the particular virtual machine implementation.

Command can decompile class file as a text file:
For example: javap -v JavaStack.class> JavaStack.txt
Here Insert Picture Description
portion bytecode information corresponding to:
Here Insert Picture Description
more bytecodes Reference: http://bbs.xiangxueketang.cn/forum .php? mod = viewthread & tid = 46 & extra = page

2.3 stack frame

Each method creates a stack frame at the same time execution, the stack frame may be divided into: the local variable table, the operand stack, dynamic linking, return address. When the method returns the calling procedure or the throws an uncaught exception stack frame is popped.

  • Local variable table: the name suggests is a table of local variables for storing our local variables. First, it is a 32-bit length, the main storage of our Java eight basic data types, generally 32 can be stored at which 64
    Long double type and the bit length of data that can take two local variable space (Slot), the rest of the data type only occupy 1
    Ge. If it is localized some objects, such as our Object object, we only need to store a reference to it can address (basic data types, object references, returnAddress type).

  • Operation Data Stack: Our method of performing storage operand, the operand stack is used in the process of execution of bytecode instructions in a manner similar native CPU registers. Most JVM bytecode to spend time on the operation of the operand stack: push, pop, copied, switching operation of generating consumption variables. Thus, variable command exchange between the local variables and operand stack array operation frequently performed by bytecodes. For example, a simple variable initialization statement will produce two byte code interacting with the operation of stack.

int i; is compiled into bytecode following:

0:    iconst_0    // Push 0 to top of the operand stack
1:    istore_1    // Pop value from top of operand stack and store as local variable 1
  • Dynamic Link: Java language features polymorphism (class loader needs to determine the specific method of operation, the subsequent detailed explanation)
    for each stack frame has a reference to the runtime constant pool. The reference points to the constant pool of the current stack frame where the method of operation. Support dynamic linking (dynamic linking) by this reference. C / C ++ code is generally compiled into an object file, and a plurality of object files are linked to produce an executable file or dll together. In the link stage, symbols for each object file has been replaced with a reference to the final executable file relative offset memory address. In Java, the link stage is done dynamically at runtime.
    When the Java class files compiled, all references to variables and methods are as symbolic reference constant pool is stored in this class. Reference symbol is a logical reference, not actually points to a physical memory address. JVM resolves symbolic references may be selected timing is when the class file is loaded and verified after the passage of this analytical approach is called starvation mode. Another is the symbol reference is resolved at the time of first use, this is called an inert parsing mode. In any case, JVM must be resolved at the completion of the first use of symbolic references and throw parsing errors may occur. Binding domain is an object, the method, the process class is replaced symbolic references referenced directly. Binding only happens once, once bonded, the symbolic references will be completely replaced. If the symbol is a reference to the class has not yet been resolved, it will load the class. Each direct reference is stored as the offset amount storage structure (the position of the runtime variables or methods associated) with respect.
  • Return Address: normal return (address of the caller as the return counter)

Trilogy:

  • Operand and local variable table recover upper layer stack method.
  • Return value (if any) onto the stack frame of the caller of the operand stack.
  • Adjust PC calling an instruction counter to point to the following instruction method.

An abnormality in the (determined <Non stack frame> The exception handler table).

2, native method stacks

Native method stacks (Native Method Stacks) and the role played by the virtual machine stack is very similar, but the difference is the virtual machine execution stack for the Java Virtual Machine method (ie bytecode) service, and is for the native method stacks virtual machine to use a method of Native service. Virtual Machine Specification language Method native method stacks are used, the use of the data structure is not mandatory, so the specific virtual machine free to implement it, native method stacks native method invocation JNI to the bottom of the C / C ++ (c / c ++ can trigger assembly language, then drive hardware). Even some virtual machines (such as Sun HotSpot VM) directly put into one native method stacks and stacks virtual machine. Like the virtual machine stack, the stack area native method can also throw StackOverflowError and an OutOfMemoryError.

Third, the thread shared area

1, Java heap: the old era, the Cenozoic

For most applications, Java heap (Java Heap) is the largest piece of memory in the Java virtual machine management. Java heap is shared by all threads in a memory area is created when the virtual machine starts. The sole purpose of this memory area is stored object instance, almost all object instances are here to allocate memory. This is described in the Java Virtual Machine Specification is: all object instances and arrays to be allocated on the heap, but with the allocation JIT compiler development and escape the mature analysis techniques, stack, scalar replacement ② optimization techniques will result in some subtle changes, all objects are allocated on the heap gradually becomes less "absolutely" the.

Java heap is the main area managed by the garbage collector, and therefore often also called "GC heap" (Garbage Collected Heap). If you look from the perspective of memory recovery, due to the current collector basically adopted generational collection algorithm, so the Java heap can also be broken down into: the old and the new generation's; and then there is little detailed Eden space, From Survivor space, To Survivor space. If you look from the perspective of memory allocation, threads share the Java heap may be divided into multiple threads private allocate a buffer (Thread Local Allocation Buffer, TLAB). Anyway division, has nothing to do with the storage of content, regardless of which area, are still stored object instances, further divided the aim is to better recovery of memory, allocating memory or faster.

Under the Java Virtual Machine specification, Java heap may be in discontinuous physical memory space, as long as logically contiguous to, just like our disk space. When implemented, may be implemented as a fixed size, it can be expanded, but the current mainstream is an extensible virtual machine implemented (by -Xms -Xmx and control). If there is no complete examples in the heap memory allocation, and the stack can no longer expand, it will throw an OutOfMemoryError.

可用以下参数调整:
-Xms:堆的最小值;
-Xmx:堆的最大值;
-Xmn:新生代的大小;
-XX:NewSize;新生代最小值;
-XX:MaxNewSize:新生代最大值;
例如- Xmx256m

2, method area (JDK1.7 before - Generation of permanent, JDK1.8- metaSpace)

District method (Method Area) and the Java heap, as each thread is a shared memory area, which is used to store class information has been loaded in the virtual machine, constants, static variables, the time compiler to compile the code and other data. Although Java Virtual Machine Specification as described in the method area is a logical part of the heap, but it has an alias called Non-Heap (non-stack), the Java heap object should be distinguished.
For habit development and deployment for developers to program in the HotSpot virtual machine, a lot of people willing to method area called "permanent Generation" (Permanent Generation), essentially the two are not equivalent, simply because HotSpot Virtual Machine design team chose the GC generational collection extends to the method area, or use the method to achieve permanent generations area only. For other virtual machines (such as BEA JRockit, IBM J9, etc.) is the concept of permanent generation does not exist. Even the HotSpot virtual machine itself, according to official information released by the road map, but now they have to give up permanently and on behalf of "move" to Native Memory to implement the method area of planning.
Java Virtual Machine Specification limit of this region is very loose, in addition to and do not require continuous as the Java heap memory can be selected and fixed size or may be expanded, but also can choose not to implement garbage collection. In contrast, garbage collection behavior in this area is relatively small appearance, but not the data into the area just as a permanent method of generation of the same name "permanent" exists. Garbage collection target this area is mainly for recycling constant pool and unloading of the type of general recovery in this region, "achievement" relatively unsatisfactory, especially the type of unloading, conditions can be quite harsh, but this part of the region recycling is really necessary. BUG Sun in the list of companies, there had been a number of serious BUG is due to the low version of the HotSpot virtual machine zones is not fully recovered and this causes a memory leak. Under the Java Virtual Machine specification, when the method of memory allocation area can not meet demand, it will throw an OutOfMemoryError.

方法区可用以下参数调整:
jdk1.7及以前:-XX:PermSize;-XX:MaxPermSize;
jdk1.8以后:-XX:MetaspaceSize; -XX:MaxMetaspaceSize;
jdk1.8以后大小就只受本机总内存的限制,如:-XX:MaxMetaspaceSize=3M

2.1 class information

The fully qualified class name, return type, modifier (public, private ...), the variable name, method name, code, complete name of this type effectively direct parent (unless this is the interface type or java.lang.Object an ordered list of interfaces directly, in both cases no parent), and the like.

2.2 runtime constant pool

Runtime constant pool (Runtime Constant Pool) is part of the zone method. Class file versions in addition to the classes, fields, methods, and interface description information and the like, there is a constant pool information (Constant Pool Table), and for storing various literal symbols compile generated reference, this part the contents stored in the methods of classes loaded running time constant region of the pool.

Java virtual machine formats for each part of the Class file (naturally including the constant pool) has strict rules, each byte is used to store data which must meet the requirements of the specification, this will be recognized by the virtual machine, loading and execution. But for the runtime constant pool, Java Virtual Machine Specification do not require any details of the different providers of virtual machines can be realized in accordance with their own needs to achieve this memory area. However, in general, in addition to saving Class symbol described in the document cited, it will also translate out of the direct reference is also stored in the runtime constant pool.

Another important feature of the runtime constant pool file relative to Class constant pool is equipped dynamic, Java language does not require a certain constants can only be generated at compile time, which is not preset the contents of Class file constant pool to enter the method run-time constant pool area during the operation may be the new constants into the pool, take advantage of this feature is the developer intern is more than the String class () method. Since the runtime constant pool area is part of the method, the natural method is limited by the memory area, when the constant pool can no longer apply for an exception to be thrown OutOfMemoryError memory.

The constant pool may store various types of data: numeric, string, class references type field reference pattern, a reference method.

示例代码如下:
Object foo = new Object();
写成字节码将是下面这样:
0:    new #2             // Class java/lang/Object
1:    dup				// 将操作数栈定的数据复制一份,并压入栈
2:    invokespecial #3     // pop出栈引用值,调用其构造函数,完成对象的初始化

Later followed new opcode operand # 2. This operand is an index of the constant pool, which point represents the constant pool of the second entity. The second is a reference to a class of entities, entity, another entity, in turn, references the constant pool UTF8 encoded string containing the name of the class (// Class java / lang / Object). Then, the symbol reference is used to find the java.lang.Object class. Create a new operation code and initialize the class instance variables. Refer to the new instance of the class were added to the operand stack. dup opcode create additional copies of a top element operand references. The last instance initialization method in the second row with invokespecial to call. The opcode also contains a pointer to the constant pool reference. The initialization method of operand stack references the top of the stack as a parameter to this method. Finally, only one reference to the new object, the object creation and initialization has been completed.

  • Symbolic references: a java class (assumed to People category) when the file is compiled into a class, if the People class references Tool class, but the class at compile time People do not know the actual memory address referenced class, it can only use symbolic references instead. And when the class loader loads People category, this time can be acquired based Tool actual memory address of the virtual machine, and therefore it is possible to replace the symbol org.simple.Tool Tool class actual memory address and the reference address directly. I.e., at compile time, instead of symbol references cited by the class, then obtain the actual address of the class referenced by the virtual machine when loading.
    Describe a set of symbols to be referenced by the target, literal symbols may be in any form, can be positioned unambiguously as long as the goal to use. Symbol reference memory layout to achieve the virtual machine is independent, objective references do not necessarily have been loaded into memory.

  • Literal
    text strings: String a = "abc", this abc is literal.
    Eight basic types int a = 1; this is a literal.
    Declared as a constant final.

3, JVM versions memory area changes:

Here Insert Picture Description

Fourth, the direct memory

Direct memory (Direct Memory) is not part of the data area of ​​the virtual machine is running, nor is it Java virtual machine memory area defined in the specification, but this memory is also frequently used, and may also lead to OutOfMemoryError exception, so we put to explain here together.

In JDK 1.4 newly added to the NIO (New
the Input / the Output) type, based on the introduction of a channel (Channel) and a buffer (Buffer) in the I / O mode, it can use Native
libraries directly outside the heap memory allocated, then in the heap by a Java DirectByteBuffer storing
objects of this memory as a reference to operate. This can significantly improve performance in some scenarios as to avoid the heap in Java and Native
heap copy data back and forth. Clearly, the direct memory allocation native Java heap size is not limited, however, since it is a memory, the machine must still be subject to the total memory (including RAM
and SWAP
area or paging file) size and processor address space limits. When the server administrator configures the virtual machine parameters, generally according to the actual memory -Xmx setting parameter information such as, but often ignores direct memory, such that each memory region is greater than the sum of the physical memory limitations (including the stage and Caozuojitong physical limitations ), resulting in an OutOfMemoryError occurs when the dynamic expansion.

Here Insert Picture Description
By -XX: to set (the default maximum heap memory with the same, there will be an exception OOM) MaxDirectMemorySize.

Test JavaStack: setting JVM parameters -Xmx100m is, abnormal operation, because if not set -XX: MaxDirectMemorySize, the default value of the same parameter -Xmx, 128M direct memory allocation limits exceeded.

Thread stand point of view: the virtual machine stack, native method stacks, the life cycle of the program counter and the three regions of the same thread. Threads share the region: it is more complicated than that, perfect follow-up.

Five-depth Analysis of the heap and stack

  • Function: a reference variable to the process stored method of stack frames call, and storing method invocation process the basic data types of variables (int, short, long, byte, float, double, boolean, char, etc.) and an object, its memory allocated on the stack, variables out of scope will be automatically released. The heap memory is used to store objects in Java, both member variables, local variables, or class variables, they point to the objects are stored in the heap memory.
  • Exclusive or shared thread: stack memory attributed to a single thread, each thread will have a stack memory which stores variables can only be seen that the stack memory can be understood as a thread private memory in their respective thread. Heap memory object is visible to all threads, the heap objects can be accessed by all threads.
  • Space: the stack memory is much smaller than heap memory. If you use recursion, then your stack is full and will soon produce StackOverFlowError.
    Stack overflow: Parameter: -Xss256k

If the stack is greater than the depth of the thread requested virtual machine allowable depth, java.lang.StackOverflowError will throw an exception. If the JVM stack can be dynamically extended (mostly JVM is possible), when the expansion is not enough memory to apply OutOfMemoryError is thrown. And no heap memory space available to store the resulting object, JVM will throw java.lang.OutOfMemoryError.

  • VM stack Inspiration:
    execution method because you want to package into a stack frame, so naturally slower cycle than to achieve the same function, so the tree traversal algorithms: recursive and non-recursive (cycle to achieve) have existed significance. Recursive code is simple, non-recursive code complexity but faster.
    OutOfMemoryError: continue to build thread. (No general presentation, the presentation out of the machine also died)

Guess you like

Origin blog.csdn.net/m0_37661458/article/details/90705520