JVM memory-related knowledge --Jvm test sites

Knowledge Jvm summary total of six general direction: memory model, class loading mechanism, GC garbage collection is more focused on content. Performance Tuning section emphasis on practical application, focused and practical ability. Compiler optimization and execution mode portion of emphasis on the theoretical basis, mainly to acquire knowledge points.

Each section are as follows: 1> memory model portion : program counter, the method area, heap, stack, native method stacks of action, which data is stored;

2> class loader part : parents delegated loading mechanism and the common class loader which loads each class type;

3> GC part : Generational thinking and recovery basis, as well as ideas of different garbage collection algorithm, the appropriate scene;

4> Tuning section : the role of common jvm parameter optimization, tuning the parameters according to the information on common jvm can analyze what kind of problem analysis tools, and methods of use;

5> Execution Mode section : interpretation, compilation, advantages and disadvantages of mixed-mode, hierarchical understanding of compiler technology java7 provided. JIT compiler needs to know the real-time technology and OSR is replaced on the stack, know the scene C1, C2 compiler for which C2 for server mode, optimized more radical. In the new technology can find out graal compiler java10 implementation provided by the java.

6> Part compiler optimization : javac compilation process of the compiler front end, AST abstract syntax tree, the compiler optimization and optimization of operation. Common compiler optimization techniques, common sub-expression elimination, with the method, escape analysis, the stack distribution, elimination synchronization. I understand this friendly in order to write code compiler.

Jvm content is relatively concentrated, but the higher the requirements for knowledge to grasp the depth of the interview before the recommendations focus on strengthening.

Jvm memory-related test sites

1. Detailed -jvm memory model

jvm memory model mainly refers runtime data area, comprising five parts.

Stack also called methods stack, is private to a thread, the thread will be created in the implementation of each method at the same time a stack frame to store local variables table, operation information stack, dynamic linking, method exports. Execution stack when calling the method, the implementation of the stack when the method returns.

Native method stacks and stacks similar, is also used to store information when the thread execution method, except that the execution method using java stack, and execute native method uses the native method stacks.

The program counter holds the current position of the byte code execution thread, each thread has a separate work counter. The program counter service for the implementation of java method, native method execution, the program counter is empty.

Stack, native method stacks, program counter these three parts are thread exclusive.

Heap is the largest piece of jvm memory management, heap shared by all threads, the purpose is to store the object instance, almost all object instances are assigned here. When the heap memory space is not available, it throws OOM exception. Depending on the period of survival of the subject, the JVM generational memory for heap management, management objects to be recovered by the garbage collector.

Each thread is the method area shared memory area, also known as non-heap area. Class information is used to store the virtual machine has been loaded, constants, static variables, the time compiler to compile the code and other data,

One kind of the permanent jdk1.7 generations of 1.8 metaspace and method area are achieved.

When answering interview questions related to this knowledge, to know the result of two points: one is the function of each part, which is the other threads share, which exclusively.

2. Detailed -jmm visibility of memory

jmm is java memory model, the memory model jvm just mentioned are two different things, the main objective is to jmm access rules defined variables in a program, as shown, all the shared variables are stored in shared main memory. Each thread has its own working memory, working memory is stored in the main memory copy of variables, variables such as thread to read and write operations must be carried out in their working memory, but can not directly read and write the main variables in memory.

When a multi-threaded data exchange, such as after a thread to a shared variable assignment to read this value by the thread b, a variable is modified changes to your own work area in memory, b is not visible only from a the work area is written back to main memory, b and then read from main memory to their work area to carry out further operations. Since there is an instruction reordering, the write - read order is likely to be disrupted.

Therefore jmm need to provide atomicity, visibility, ordering guarantee.

3. Detailed -jmm guarantee

How to introduce the next major jmm guarantee atomicity, visibility, orderly.

  • jmm read and write operations to ensure that the underlying data types except for long and double are atomic. In addition keyword Synchronized may also provide atomicity guarantees. Synchronized atomicity is advanced by two java bytecode monitorenter and monitorexit instructions ensured.

  • jmm visibility guaranteed through a Synchronized, the other one is volatile. volatile variable assignment will be forced back to the main memory refresh sync, forced to read the variable is reloaded from main memory to ensure that different threads can always see the latest value of the variable.

  • jmm to ensure orderliness, mainly through a series of volatile and happens-before principle. Another effect is to prevent the volatile instruction reordering, so as to ensure an orderly read and write variables.

happens-before principles include a set of rules, such as the principle of program order, i.e., it must ensure serializability a semantic thread; lock rules, i.e., unlocking a lock on the same must occur before re-locking; also comprises happens-before principle transitive, thread start, suspend, terminate rules.

Reproduced in: https: //juejin.im/post/5cef945e6fb9a07ef1616765

Guess you like

Origin blog.csdn.net/weixin_33811961/article/details/91465643