Satisfied with the offer of the JVM virtual machine Summary Q & A

JVM is how to make java code is executed machine?

Java source file, by a compiler, to produce the corresponding .Class file, which is byte code file, the file byte code by a Java virtual machine and interpreter, compiled into machine code on a particular machine.

JVM thread relationship with the system thread?

Hotspot JVM in Java threads and native operating system thread has a direct mappings. When the thread local storage, buffer allocation, synchronization objects, stack, program counter, ready, it will create an operating system native threads. Java thread end, native threads will be recycled. Operating system is responsible for scheduling all the threads, and assign them to any available CPU. When the original thread initialization is complete, it will call run Java thread () method. At the end of the thread, it releases all resources native Java threads and threads.

What are the main JVM system thread a few?

Virtual machine threads (VM thread) This thread waits JVM reach a safe point operation occurs. These operations must be performed in a separate thread, because when the heap can not be modified, thread requires JVM in a safe spot. These types of operations are: stop-the- world garbage collection, thread stack dump, thread suspension, thread biased locking (biased locking) is released.
Periodic task thread This thread is responsible for the timer event (ie interrupt), to schedule periodic operations performed.
GC threads These threads supported JVM garbage collection in different activities.
The compiler thread These threads at runtime byte code dynamically compiled to native machine code platform-dependent.
Signal distribution thread The thread receives a signal sent to the JVM and JVM calls the appropriate method for processing.

JVM memory into which areas? Each area contains what?

JVM thread private memory area is divided into areas [of the program counter, the virtual machine stack, native method stacks], threads share the region [JAVA heap, the method area], direct memory.

What is the program counter (thread private), it OutOfMemoryError it?

A smaller memory space, is the line number indicator byte code executed by the current thread, each thread should have a separate program counter, this type of memory is also known as "thread private" memory. Not OutOfMemoryError

What is a virtual machine stack (thread-private)?

Java memory model is a description of a method of performing, for each frame, which will create a stack (Stack Frame) while performing the local variable table for storing information, the operand stack, dynamic link, the method exports. Each method until the completion of the execution procedure is called, a corresponding stack frame to push the stack in a virtual machine process stack.

What is a native method stacks (thread-private)?

Stack Java virtual machine to perform a method for service, and the local Native method stack method, compared with the service, but put the HotSpot VM native method stacks and stacks into one virtual machine directly.

Brief the JVM heap and method area

  1. Heap thread is shared area of ​​memory, and an array of objects created are stored in the Java heap memory, the garbage collector also be the most important area of ​​memory garbage collection. As modern VM using generational collection algorithm, so the Java heap GC from the point of view can also be broken down into: the new generation (Eden District, From Survivor To Survivor district and region) and the old era.
  2. We often say that the method area permanent substituting (Permanent Generation), for storing information to be loaded by the JVM class, constants, static variables, the real-time data such as the code compiler. HotSpot VM to GC generational collector region extends to a method that the use of permanent generation of Java heap to implement a methodology region, such HotSpot garbage collector can manage the Java heap like this as part of memory management, without having to develop a special memory manager for the method area.

JVM new generation Eden District, ServivorFrom, ServivorTo three areas of meaning

  1. Eden area: the birthplace of a new Java object (if the newly created object occupies a lot of memory, then directly assigned to the old year). When enough of the Eden area of ​​memory when it will trigger MinorGC, the new generation garbage collection zone.
  2. ServivorTo: preservation of primary MinorGC process of survivors.
  3. A survivor of the GC, as it is scanned by a GC: ServivorFrom.

What is the use of the new generation MinorGC algorithm? Execution is kind of how?

MinorGC using replication algorithm. First, copy the Eden area and ServivorFrom surviving objects to ServicorTo area (if there is to the age and older met the criteria, then assigned to the district's old), while the age of these objects +1 (if the location is not enough ServicorTo to put tenured); then emptied and Eden ServicorFrom objects; Finally, ServicorTo ServicorFrom and interchangeable, the original ServicorTo become ServicorFrom zone next GC.

MajorGC using what algorithm? Execution is kind of how?

MajorGC using clear labeling algorithm: first scans once all the old era, marked survival of the subject, and then recovering the object is not marked. MajorGC relatively long time-consuming, because you want to scan recycling. MajorGC will produce memory fragmentation, in order to reduce memory consumption, we generally need to be merged or marked for next direct distribution. When the old year was full fit and they will throw OOM (OutOfMemory) exception.

After java8 JVM permanent generation of what changes?

In Java8, permanent behalf has been removed, the region "metadata area" (element space) is called by a substituent. Yuan and permanent nature of space to like, the biggest difference between the yuan and the permanent space on behalf of that: Yuan is not space in a virtual machine, but the use of local memory.

How the JVM is to determine the object data is garbage?

  1. Reference counting: If an object is not associated with any references that have their reference count is zero, then the object is less likely to be used, then the object is recyclable objects.
  2. Reachability analysis: If you do not reach the path between the "GC roots" and an object, the object is said to be unreachable. Unreachable object is not equivalent to the object recoverable, recyclable object becomes unreachable objects is at least two marking process. After two marks are still recyclable objects, it will face recovery.

Clear labeling brief algorithm (Mark-Sweep)

The most basic garbage collection algorithm is divided into two phases, marked and cleared. Mark phase mark all objects need to be recovered, the recovery phase is marked clear space occupied by an object. The algorithm is the biggest problem of memory fragmentation severe, may occur subsequent large object can not find the problem available space.

Brief replication algorithm (copying)

Mark-Sweep algorithm to solve the memory fragmentation algorithm is proposed. Press memory partitioning the memory into two equal size. Every time use only one copy to another block when the object of this one is full up after the memory is still alive, the memory has been cleared for use, although this algorithm is simple, high memory efficiency, difficult to produce debris, but the biggest the problem is that the available memory is compressed to half of the original. And the number of live objects, then the efficiency Copying algorithms will be greatly reduced.

Brief tags to organize algorithm (Mark-Compact)

A combination of the above two algorithms proposed in order to avoid defects. Mark phase and Mark-Sweep same algorithm, the tag is not clean up the object, but towards the end of the live objects in memory. Then clear the outer end of the object boundary.

Brief generational collection algorithm

Generational collection method is the method most used by the JVM, the core idea is based on the life cycle of different objects alive the memory is divided into different domains, in general, the GC heap is divided into the old generation (Tenured / Old Generation) and New Generation (Young Generation). Most of the JVM GC for the new generation have taken replication algorithm, because in every new generation garbage collection should recover most of the objects that you want to copy operation is relatively small, but not usually in a 1: 1 to divide the new generation. Generally divided into a new generation of larger and two smaller Eden space Survivor space (From Space, To Space), and wherein each space using Eden space of a Survivor, when recovered, the two space also surviving copy objects to another piece of Survivor space. And because every time the old generation to recover only a small amount of objects, so the use of tags to organize algorithm. When there is insufficient new generation of Eden Space and From Space space occurs once GC, after GC, live objects Eden Space and From Space area will be moved To Space, then the Eden Space and From Space to clean up. To Space If not enough memory to an object, the object will be stored in the old generation. After conducting GC, is used and Eden Space To Space, and so forth cycle. When the objects in the district Survivor escaped a GC, the age will be +1. Ages 15 to reach the target of default will be moved to the old generation.

In JAVA four reference types which are four brief

  1. Strong references: The most common is a strong reference in Java, the object is assigned a reference to a variable, the reference variable is a strong reference. When an object is strong reference variable references, it is up to the state, it is unlikely to be recycled garbage collection mechanism, even if the object in the future will never be used JVM will not be collected. So strong reference is one of the main causes Java memory leaks.
  2. Soft Quote: soft references need SoftReference class to implement, subject only to the soft references, it will not be recovered when the system memory is sufficient, when there is insufficient system memory space it will be recycled. Soft references are usually used for memory-intensive programs.
  3. Weak References: A weak reference class WeakReference need to achieve, it refers to the lifetime of softer than shorter, only weak references to an object, as long as a garbage collection operation, regardless of the JVM memory is not enough, there is always recovered memory occupied by the object.
  4. Virtual references: class virtual reference PhantomReference needed to achieve, it can not be used alone, and the reference queue must be used in combination. The main role of the virtual reference is to track the object is garbage collected state.

There are several GC garbage collector, what they are?

  1. Serial garbage collector, Features: CPU maximum utilization, ie the user pauses a long time to wait. Application scenarios: mini-applications by JVM parameters -XX: + UseSerialGC to serial garbage collector.
  2. ParNew garbage collector, control parameters: -XX: + UseParNewGC ParNew collector -XX: ParallelGCThreads limit the number of threads
  3. Parallel Scavenge collector, by scanning using multiple threads and stack compression characteristics: short dwell time, high recovery efficiency, high-throughput requirements. Applicable scene: large-scale applications, scientific computing, large-scale data acquisition. By JVM parameters XX: + USeParNewGC Open concurrent garbage collector marks the scan.
  4. Serial Old collectors
  5. Parallel Old collectors
  6. Concurrent mark sweep (CMS) collector, Advantages: concurrent collection, low pauses disadvantages: a large amount of space debris, concurrent phase will reduce the throughput characteristics: priority response time, reduce garbage collection pause time to adapt to the scene: large-scale servers. By JVM parameters -XX: + UseConcMarkSweepGC provided
  7. G1 collector, features: support for large heap, high throughput, by JVM parameter -XX: + UseG1GC use G1 Garbage Collector

JVM class loading mechanism which is divided into five parts, What do you do?

JVM class loading mechanism is divided into five parts: loading, validation, preparation, parsing, initialization, here we look at each of these five processes.

  1. Load: loading a class loading stage process, a representative of this phase will generate java.lang.Class object of this class, as the various data entry area methods of this class in memory. Note that this does not have to obtain the document from a Class, where either read from ZIP package (such as reading the packet from the jar and war package) may be calculated to generate (dynamic proxy) at run time may also be from other generating a file (such as JSP file converted to the corresponding class class)
  2. Verification: The main purpose of this stage is to ensure that the information byte stream file contains Class meets the requirements of the current virtual machine and the virtual machine will not jeopardize their own safety.
  3. Preparation: preparation phase is formally allocate memory for the class variables and class variables set the initial value of the phase, i.e., memory space is allocated variables used in the process area.
  4. Analysis: parsing stage is a virtual machine symbolic constant pool of reference for the process of replacing direct references. Constant reference symbol is class file
  5. Initialization: Initialization Phase class loader is the last phase, after the preceding loading phase type, in addition to a custom class loader except by other operations in loading JVM dominant phase.

Briefly explain the three types of JVM loader?

  1. Start class loader (Bootstrap ClassLoader): responsible for loading the JAVA_HOME \ lib directory, or by - Xbootclasspath specified path parameter, and is recognized by the virtual machine (by file name recognition, such as rt.jar) classes
  2. Extension class loader (Extension ClassLoader): responsible for loading the JAVA_HOME \ lib \ ext directory, or the path specified by java.ext.dirs library system variables.
  3. Application class loader (Application ClassLoader): responsible for loading the library on the user's path (classpath).

What is the class loader?

  1. Class load refers to the Class class file is read into memory, and whom to create a java.lang.Class objects, class file is loaded into memory after, can be referenced by other class
  2. jvm startup, and it does not load all the class files one time, but needed to dynamic loading
  3. java class loader is part of the jre, java class is responsible for dynamically loaded into memory java virtual machine
  4. The only kind of sex and class by the class loader joint decision (parents delegate mechanism)

How to implement your own custom class loader?

JVM by parent delegation model to load class, of course, we can implement a custom class loader through inheritance java.lang.ClassLoader.

JVM class loading delegation model by parents, how it is loaded?

When a class received a class load request, he first does not attempt to load their own class, but put the request to delegate to the parent class to complete each level class loader is true, so all requests should load transfer to start the class loader which, only when the parent class loader feedback they can not complete the request time (class not found the required load under its load path), sub-class loader will try to load your own.

Common parameters

-XX:+PrintGC 每次触发GC的时候 打印相关日志  
-XX:+UseSerialGC 串行回收   
-XX:+PrintGCDetails  更详细的GC日志 
-Xms 堆初始值   
-Xmx 堆最大可用值   
-Xmn 新生代堆最大可用值 
-XX:SurvivorRatio 用来设置新生代中eden空间和from/to空间的比例.  
-XX:NewRatio 配置新生代与老年代占比 1:2 
含以-XX:SurvivorRatio=eden/from=den/to  
**总结:在实际工作中,我们可以直接将初始的堆大小与最大堆大小相等,**
**这样的好处是可以减少程序运行时垃圾回收次数,从而提高效率。**
复制代码

Talk about your understanding of the modular development of JAVA

What is bytecode? What are the benefits of using bytecode is?

  1. JVM code can be appreciated that the bytecode is called (i.e. .class file name extension)
  2. Java byte code language by the way, to a certain extent, solve the low efficiency of traditional interpreted language problems, while retaining the features of interpreted languages ​​portable. So when running a Java program more efficient, and that can run on a variety of different computers due to the byte code is not specific to a particular machine, therefore, Java programs without having to recompile.

In the Java language, as GC Roots objects include the following:

  1. VM stack (local variables in the stack frame of the table) the object reference
  2. Method region static property class object reference
  3. Method zone object literal references
  4. JNI native method stack objects references

Why would often say Java is compiled and interpreted languages ​​coexist.

The introduction of the JVM JIT compiler, compile-time JIT belong run. After the completion of the first JIT compiler compiler, which will preserve the bytecode corresponding to the machine code, the next can be used directly.

Why Java language can "compile once, run anywhere"?

JVM running Java bytecode virtual machine. The JVM has a particular implementation (Windows, Linux, macOS) for different systems, the aim is to use the same bytecode, it will give the same result. JVM byte code and implement different systems, the Java language "compile once, run anywhere" key.

java GC will take place at what time, for what, do what?

  1. Specifically, when the programmer can not control time, the system call System.gc () in unpredictable function of time; of course, can be tuned by controlling the ratio NewRatio newObject and oldObject, the number of entries into oldObject with MaxTenuringThreshold control, such storage space oldObject Full retardation reached gc, gc so that timer initiation time delay OOM delay time, to prolong the lifetime of the object.
  2. Beyond the scope or null object reference count; gc root from the beginning of the search object can not be found, and after a mark, clear, still no object resurrection.
  3. As the new generation to do is copy the clean-up, from survivor, to survivor is doing with old's responsibility to mark clean up the debris to clean up not finishing mark, copy, clean up and mark clear up what advantages and disadvantages, etc.

JVM tuning commonly used parameters have what?

  • -Xms2g: Initialization push 2g size;
  • -Xmx2g: maximum heap memory is 2g;
  • -XX: NewRatio = 4: Set Memory proportions of young and old age is 1: 4;
  • -XX: SurvivorRatio = 8: Set the new generation Eden and Survivor ratio of 8: 2;
  • -XX: + UseParNewGC: Specifies ParNew + Serial Old garbage collector composition;
  • -XX: + UseParallelOldGC: Specifies ParNew + ParNew Old garbage collector composition;
  • -XX: + UseConcMarkSweepGC: Specifies CMS + Serial Old garbage collector composition;
  • -XX: + PrintGC: gc open print information;
  • -XX: + PrintGCDetails: Print gc details.

Tuning summarize: the larger the initial value and the maximum heap memory heap memory, the higher the throughput. Parallel collector is preferably used, because the speed is higher than the parallel collector Serial certain speed. The proportion of the new generation ratio set heap memory's old and preferably 1: 2 or 1: 3. GC reducing the recovery of old age.

Brief generational garbage collector is how to work?

Generational collector has two partitions: the old generation and the new generation, the new generation of 1/3 of the total space default spatial proportion, the proportion of the old generation of default is 2/3. Using a new generation of replication algorithm, the new generation, there are three partitions: Eden, To Survivor, From Survivor, accounting for their default is 8: 1: 1, it performs the following process:

  • The Eden + From Survivor To Survivor live objects into regions;
  • Clear and Eden From Survivor partition;
  • From Survivor 和 To Survivor 分区交换,From Survivor 变 To Survivor,To Survivor 变 From Survivor。

Each time From Survivor to move all live objects To Survivor age to +1, when reaching the age of 15 (15 is the default configuration), upgrade to the old generation. Large Objects also go directly to the old generation. When the space occupied by the old generation reaches a certain value will trigger the recovery of the global garbage, general use tags to organize the execution of the algorithm. These constitute the overall implementation of the cycle processes across generational garbage collection.

The new generation and the old generation garbage collector garbage collector has what? What's the difference?

  • New Generation collector: Serial, ParNew, Parallel Scavenge
  • Years old collector: Serial Old, Parallel Old, CMS
  • Whole stack collector: G1

New generation garbage collector copy algorithm is generally used, the advantages of replication algorithm is efficient, disadvantage is that memory utilization is low; year old collector is generally used to mark - sorting garbage collection algorithm.

What to say about the JVM garbage collection algorithm have?

  • Mark - sweep algorithms: mark useless objects, and then clear the recovery. Disadvantages: efficiency is not high enough to clear the garbage debris.
  • Mark - Collation Algorithm: mark useless objects, so that all live objects are moved to the end, and then clear out the memory directly outside the terminal boundary.
  • Replication algorithm: capacity divided according to the size of the memory area equal to two, when one runs out of copying live objects onto another piece of memory space has been used once and then clean out. Cons: memory usage is not high, only half of the original.
  • Generational algorithms: Depending on whom the survival period of the memory is divided into a few pieces, usually the new generation and the old era, the basic use of the new generation of replication algorithm, the use of tags to organize years old algorithm.

Talk about the implementation process of class loading?

The class loader into the following five steps:

  • Load: find the appropriate class file and import according to the search path;
  • Verification: check the correctness of the loaded class files;
  • Preparation: allocate memory space for the class static variable;
  • Analysis: VM reference symbol the constant pool replaced directly referenced process. To be understood as a reference symbol mark, and in direct reference directly to the memory address;
  • Initialization: Performing initialization of static variables and static code block.

Talk about the difference between the stack?

  • Function: stack is used to store objects, the stack is used to execute the program.
  • Share: the heap is shared by the threads, thread stack is private.
  • Space: the heap size is much larger than the stack.

Talk about the JVM run-time data area?

Run-time data area different virtual machines may be slightly different, but will comply with Java Virtual Machine Specification, an area defined Java Virtual Machine Specification divided into the following five sections:

  • Program counter (Program Counter Register): a row number designator bytecode currently executing thread, the work of the bytecode parser by changing the value of the counter to select the next bytecode instruction to be executed, the branch , looping, branching, exception handling, thread resume and other basic functions, need to rely on the counter to complete;
  • Java Virtual Machine stack (Java Virtual Machine Stacks): to store information local variable table, the operand stack, dynamic link, for export and the like;
  • Native method stacks (Native Method Stack): the virtual machine stack effect is the same, but the Java virtual machine stack is the method of service, and the local stack is a method for virtual machines Native method invocation services;
  • Java heap (Java Heap): Java Virtual Machine in the biggest piece of memory, is shared by all threads, almost all objects are instances where memory allocation;
  • The method area (Methed Area): class information is used to store the virtual machine has been loaded, constants, static variables, the real-time data such as compiled code.

Talk about the main components of the JVM? And its role?

  • Class loader (ClassLoader)
  • Run-time data area (Runtime Data Area)
  • Execution Engine (Execution Engine)
  • Local library interface (Native Interface)

Effect components:  first by the class loader (ClassLoader) will Java code into byte code, the runtime data area (Runtime Data Area) bytecode then loaded into memory, but only the JVM byte code file a instruction set specification sets, and not directly to the underlying operating system to perform, and therefore requires a specific execution command parser engine (execution engine), translating the byte code instructions into the underlying system, then by the CPU to perform, and this process You need to call in other languages local library interface (native Interface) to implement the functionality of the entire program.

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

Guess you like

Origin blog.csdn.net/weixin_33671935/article/details/93176371