Android Java JVM Frequently Asked Questions Analysis and Summary

1. What is JVM

        JVM is the abbreviation of Java Virtual Machine (Java Virtual Machine). JVM is a specification for computing devices. It is a fictitious computer that is realized by simulating various computer functions on an actual computer.

The importance of the JVM

        The JVM is a concept-oriented module, but many large companies will take a fancy to it. Only by understanding the JVM can the performance of the project be better utilized and the quality of the project improved during the development process. The following is some information about the JVM that I have compiled for your reference.

2. Common problems of JVM

1. The main components of the JVM

1. class loader class loader: load class files into memory. Class loader only loads

2. Exection engine: The execution engine is also called an interpreter, which is responsible for interpreting commands and handing them over to the operating system for execution

3. native interface: local interface. The role of the local interface is to integrate different languages ​​for java

4.Runtimedata area running data area: the running data area is the focus of jvm, all the programs we write are loaded here, and then start running

5. Stack stack: The stack is also called the stack memory, which is the running area of ​​the java program. It is created when the thread is created. Its life cycle follows the life cycle of the thread. When the thread ends, the stack memory is released

6. Heap memory: There is only one heap memory in a JVM instance, and the size of the heap memory can be adjusted. After the class loader reads the class file, it needs to put the class, method, and constant variable into the heap memory to facilitate execution by the executor. The heap memory is divided into three parts

7. Method area: the method is shared by all threads,

8. Program counter: Each thread has a program counter, which is a pointer to the method bytecode in the method area, and the execution engine reads the next instruction

2. JVM runtime data area

The areas specified by the Java Virtual Machine Specification are divided into the following five parts

1. Program counter: The line number indicator of the bytecode executed by the current thread. The job of the bytecode parser is to change the value of this counter to select the next bytecode instruction to be executed.

2. Virtual stack: used to store local variable table, operand stack, dynamic link, method exit and other information, Java method;

3. Native stack: the local method stack serves for the virtual machine to call the Native method

 4. Heap: The largest piece of memory in the Java virtual machine is shared by all threads, and almost all object instances allocate memory here

5. Method area: used to store data such as class information, constants, static variables, and just-in-time compiled code that have been loaded by the virtual machine

3. The execution process of class loading (classload)

Class loading is divided into the following 5 steps

1. Loading: Find the corresponding class file according to the search path and import it;

2. Check: Check the correctness of the loaded class file;

3. Preparation: allocate memory space for static variables in the class;

4. Resolution: The virtual machine replaces the symbolic references in the constant pool with direct references. A symbolic reference is understood as a mark, and a direct reference directly points to an address in memory;

5. Initialization: Perform initialization work on static variables and static code blocks.

4. Determine whether the object can be recycled

1. Reference counter: Create a reference count for each object, the counter +1 when there is an object reference, and -1 when the reference is released

2. Reachability analysis: starting from GC Roots to search downwards, the path traveled by the search is called the reference chain

5. What are the reference types in Java

1. Strong reference: it will not be recycled when gc occurs.

2. Soft references: Objects that are useful but not necessary will be recycled before memory overflow occurs.

3. Weak references: Objects that are useful but not necessary will be recycled in the next GC.

4. Phantom reference (ghost reference/phantom reference): The purpose of the phantom reference is to return a notification when gc

6. Briefly describe how the generational garbage collector works

The generational collector has two partitions: the old generation and the new generation. The default space of the new generation accounts for 1/3 of the total space, and the default proportion of the old generation is 2/3.

7. Tools for JVM tuning

JDK comes with many monitoring tools, all of which are located in the bin directory of JDK, the most commonly used of which are the two view monitoring tools jconsole and jvisualvm.

8. What are the commonly used JVM tuning parameters?

XX is less stable than X, and the version update will not be notified and explained.

-Xms s is strating, indicating the initial size of the heap memory

-Xmx x is max, which means the largest heap memory

(Generally, -Xms and -Xmx are set to the same size, because when the heap is automatically expanded, memory jitter will occur, which will affect the stability of the program)

-Xmnn is new, indicating the size of the new generation

(-Xss: specifies the size of each thread virtual machine stack (stack))

-XX:SurvivorRator=8 indicates that the ratio of the new generation, the old generation and the permanent generation in the heap memory is 8:1:1

-XX:PretenureSizeThreshold=3145728 (creation size) indicates that when the created (new) object is larger than 3M, it will directly enter the old age

-XX:MaxTenuringThreshold=15 indicates that when the survival age of the object (minor gc plus 1) is greater than the number, enter the old age

-XX:-DisableExplicirGC indicates whether (+ indicates yes, - indicates no) to open the GC log

9. Get the memory used by the Java program? percentage of heap used

The remaining memory, total memory and maximum heap memory can be obtained through memory-related methods in the java.lang.Runtime class.

10. What is the difference between JRE, JDK, JVM and JIT

JRE: Stands for Java run-time and is required to run Java references.

JDK: stands for Java development kit (Java development kit), which is a development tool for Java programs

JVM: stands for Java virtual machine (Java virtual machine), whose responsibility is to run Java applications.

11. Tell me what garbage collection algorithms the JVM has

Mark-sweep algorithm: mark useless objects, and then clear and recycle them. Disadvantages: Inefficient, unable to remove debris.

Mark-sorting algorithm: mark useless objects, let all surviving objects move to one end, and then directly clear the memory outside the end boundary.

Copy algorithm: Divide two memory areas of equal size according to capacity, copy the living object to another block when one block is used up, and then clean up the used memory space at one time. Disadvantages: The memory usage is not high, only half of the original.

Generational algorithm: Divide the memory into several blocks according to the life cycle of the object, usually the new generation and the old generation. The new generation basically adopts the copy algorithm, and the old generation uses the marking algorithm.

12. What is the parent delegation model

If a class loader receives a class loading request, it will not load the class by itself first, but delegate the request to the parent class loader to complete. The same is true for each class loader, so that all Loading requests will be sent to the top-level startup class loader, and only when the parent loader cannot complete the load request (it does not find the required class in its search scope), the child loader will try to load the class

13. JVM life cycle

In the following situations, the Java virtual machine will end its life cycle

1. Execute the System.exit() method

2. The program ends normally

3. The program encounters an exception or error during execution and terminates the process

4. The Java virtual machine process is terminated due to an error in the operating system

おすすめ

転載: blog.csdn.net/qq36246172/article/details/132671303