Welcome to our GitHub repository Star: github.com/bin39232820… The best time to plant a tree was ten years ago, followed by now to encourage people to blog on the tech road
omg
Previous chapters
- JVM class loading mechanism from entry to ground JVM
- The class file structure of the JVM from inception to burial
- The runtime data area of the JVM from entry to burial
- Memory allocation strategy and garbage collector for JVM from entry to ground JVM
Today, I’m going to give you the interview questions about the JVM, but there are so many that I can’t pick them all. I’m going to talk about the typical ones, but the answers to all the questions are still based on the previous four chapters. I think the most difficult one is the class file structure, which is really confusing. Ha ha, without further ado let’s see
How many partitions are there in the JVM memory, and what does each partition do?
Java virtual machines are divided into the following areas:
- Methods area:
- It is also sometimes called a permanent generation, where garbage collection rarely occurs, but it does not mean that GC does not occur, where the main GC is the unloading of constant pools and classes in the method area
- The method area is used to store information about classes that have been loaded by the virtual machine, constants, static variables, and code compiled by the just-in-time compiler.
- This area is shared by threads.
- The method area has a pool of runtime constants to store literal and symbolic references generated by static compilation. This constant pool is dynamic, meaning that constants are not necessarily determined at compile time, but are generated at run time in the constant pool.
- Virtual machine stack:
- The virtual machine stack, also known as stack memory, serves Java methods, and each method creates a stack frame to store information about local variables, operation stacks, dynamic links, and method exits.
- The virtual stack is thread-private and has the same lifetime as the thread.
- The local variable table stores the basic data types, the returnAddress type (the address that points to a bytecode instruction), and the object reference, which may be a pointer to the object’s starting address, a handle to the object, or a location associated with the object. The memory space required for local variables is determined between compilers
- Operand stack is mainly used to store operation results and operands. It is different from local variable table to access by index, but the way of pushing and pushing
- Each stack frame contains a reference to the method that the stack frame belongs to in the runtime constant pool, and this reference is held to support dynamic concatenation during method calls. Dynamic linking is the conversion of symbolic references in a constant pool to direct references at run time.
- Local method stack
- The local method stack is similar to the virtual machine stack, except that the local method stack serves the Native method.
- The heap
- The Java heap is a chunk of memory shared by all threads, created at virtual machine startup, where almost all object instances are created, so garbage collection often occurs in this area.
- Program counter
- Since the memory space is small, the bytecode interpreter can select the next bytecode instruction to be executed by changing the value of this counter. Branches, loops, jumps, exception handling, thread recovery and other functions need to be completed by this counter. This area of memory is the only area where the Java Virtual Machine specification does not specify any OOM condition.
Such as and determine whether an object is alive? (or GC object determination method)
The answer is in the previous chapter
How about Java garbage collection?
In Java, programmers do not need to display to free the memory of an object, but the virtual machine does it itself. In the JVM, there is a garbage collection thread, which is low priority and does not execute under normal circumstances, but only when the VIRTUAL machine is idle or the current heap is out of memory, which sweeps through objects that are not referenced by any of them and adds them to the collection for collection.
What are the methods of garbage collection in Java?
Ask about the three recycling algorithms in the previous section
Java class loading process?
What does each of these 7 processes do, which was also in my previous chapter
Class loader parent delegate model mechanism?
It’s just eight words: check up, load down. See my previous chapters for details
What is a class loader, and what are class loaders?
A block of code that implements retrieving the binary stream of a class by its permission name is called a classloader.
There are four main types of loaders:
- The Bootstrap ClassLoader (Bootstrap ClassLoader) is used to load Java core class libraries and cannot be directly referenced by Java programs.
- Extensions Class Loader: It is used to load Java extension libraries. The Implementation of the Java Virtual machine provides an extension library directory. The class loader finds and loads Java classes in this directory.
- System Class Loader: It loads Java classes based on the CLASSPATH of the Java application. In general, Java application classes are loaded by it. Through this. GetSystemClassLoader () to get it.
- User-defined class loaders are implemented by inheriting java.lang.ClassLoader classes.
Tomcat’s class loading model
First, let’s ask a question:
How about using the default class loading mechanism for Tomcat? Let’s consider: Tomcat is a Web container, so what problem does it solve:
- A Web container may require two applications to be deployed, and different applications may rely on different versions of the same third-party class library. You cannot require only one copy of the same class library on the same server. Therefore, ensure that class libraries of each application are independent and isolated from each other.
- The same version of the same class library deployed in the same Web container can be shared. Otherwise, if the server has 10 applications, then 10 copies of the same class library are loaded into the virtual machine, which is bullshit.
- The Web container also has its own libraries that it relies on and cannot be confused with the application’s libraries. For security reasons, the container’s libraries should be separated from the program’s libraries.
- The Web container has to support JSP modification, and we know that JSP files eventually have to be compiled into class files to run in the virtual machine, but it is already common to modify JSPS after the program runs. Otherwise, why bother? Therefore, the Web container needs to support JSP modifications without a restart.
Let’s take a look at our question: Does Tomcat work if it uses the default class loading mechanism? The answer is no. Why is that? If you use the default class loader mechanism, you can’t load two different versions of the same library. The default accumulator only cares about your fully qualified class name and only has one copy. Second, the default class loader can be implemented because its job is to ensure uniqueness. The third question is the same as the first. Let’s look at the fourth problem, we want to how to implement JSP file hot modification, JSP file is actually also the class file, so if modified, but the class name is still the same, the class loader will directly take the method area already exists, the modified JSP is not reloaded. So what to do? We can unmount the class loader for this JSP file directly, so you should be aware that each JSP file has a unique class loader. When a JSP file is modified, the JSP class loader is unmounted directly. Re-create the class loader and reload the JSP file.
Have you ever had an OutOfMemory problem? How did you deal with the problem? What are the gains from the process?
Common reasons
- Too much data is loaded in memory: too much data is fetched from the database at one time;
- There are references to objects in the collection class, which are not cleared after use and cannot be collected by GC.
- There is a loop in the code that produces too many duplicate objects;
- Startup parameter heap memory value is small.
What changes have Perm Space made since JDK 1.8? Is MetaSpace size unlimited by default? Or are you going to specify the size in some way?
JDK 1.8 replaced Perm Space with meta Space; String constants are stored in heap memory.
The MetaSpace size is unlimited by default, generally based on the size of system memory. The JVM changes this value dynamically.
-xx :MetaspaceSize: specifies the initial high-water-mark (the initial high-water-mark on Oracle logical storage) allocated to the class metadata space in bytes. This value is an estimate, and too high a MetaspaceSize value will prolong the garbage collection time. After a garbage collection, the size of the class metadata space that causes the next garbage collection may increase. -xx :MaxMetaspaceSize: The maximum amount of metadata space allocated to a class, beyond which Full GC is triggered. This value is unlimited by default, but depends on the size of system memory. The JVM changes this value dynamically.
What does JStack do? Jstat? If an online application periodically freezes and you suspect GC is the cause, how do you troubleshoot this problem? What part of a thread log do you usually look at?
Jstack is used to query the stack information of Java processes.
Jvisualvm monitors memory leaks, tracks garbage collection, execution time memory, CPU analysis, thread analysis.
Have you encountered StackOverflow exceptions? Under what circumstances would you guess it would be triggered? How do I specify the stack size of a thread? How much do you usually write?
Stack memory overflow, generally caused by the stack memory local variables overburst, resulting in memory overflow. In recursive methods, too many parameters, too deep recursion, recursion has no exit
Diagrams of several core parameters related to JVM memory
How to set the JVM startup parameters when booting the system
At the end
You need to have actual JVM tuning experience. The interview is a plus. As for the theory, it is only secondary.
Daily for praise
Ok, everybody, that’s all for this article, you can see people here, they are real fans.
Creation is not easy, your support and recognition, is the biggest motivation for my creation, we will see in the next article
Six pulse excalibur | article “original” if there are any errors in this blog, please give criticisms, be obliged!