This article has authorized Yu Gang to say the public number
Series of articles:
Deep dive into the Java Virtual Machine – Concludes the interview
Deep into the Java Virtual Machine: Love and hate of the JVM
JAVA garbage Collection mechanism (a) – Object collection and algorithm understanding
JAVA garbage collection mechanism (two) – GC collection concrete implementation
Deep Into the Java Virtual Machine — Class file Structure (bytecode)
Dive into the Java Virtual machine — class loading mechanism
In learning about the JVM, how to keep yourself motivated to read and think? In my opinion, it is better to introduce readers’ interest with some common interview questions at the beginning, so that there will be something to read. Therefore, this article will be conducted in the way of interview + summary, hoping readers can think and write answers first, and then check relevant knowledge.
JVM often meet questions
Deep dive into the Java Virtual Machine – Concludes the interview
- Introduces the Java memory region
- Java object creation process
- There are several ways to access an object
- What’s the difference between String, StringBuilder, and StringBuffer?
These are some of the common interview answers that many people see online, but do you know why?
1.1 This section describes the Java memory area
Let’s look at the first one, the Java memory area, and see a compilation diagram:
Deep into the Java Virtual Machine: Love and hate of the JVM
1.2 Java object creation process
Java object creation is divided into 5 steps, as shown below:
1.3 There are several types of object access positioning
There are two ways: handles and direct Pointers; Objects are created to use objects, and the virtual machine needs reference in the stack to get objects on the heap.
1.4 What is the difference between String, StringBuilder, and StringBuffer
String is a class that is final. Because of its immutability, such as concatenating and clipping strings, new objects can be created. StringBuffer takes care of the above concatenation object and provides a class that can be concatenated using methods like Append. It’s thread-safe. It’s less efficient because it’s thread-safe. So StringBuilder is preferred
Why does String produce new objects? For example, String a = “1” String b = a + “2”. When executing this instruction, an object pointing to A is generated in the constant pool, and b’s object is regenerated in the constant pool when creating B. Multiple creates tend to trigger GC, which is why it is not recommended to use the String class for concatenation.
Java recycling mechanism often meet questions
Deep into the Java virtual machine — The JVM love hate Hate Java garbage collection mechanism (ii) — GC collection concrete implementation
- A brief introduction to strong references, soft references, weak references, and virtual references (the difference between virtual references and soft references and weak references, and the benefits of using soft references)
- What are the differences between Final, finally and Finalize
- Does the method area recycle resources?
- What are the algorithms for garbage collection and their characteristics?
2.1 This section briefly introduces strong reference, soft reference, weak reference, and virtual reference
First, before explaining these references, understand why virtual machines are explained by these references. We all know that objects need to be recycled, so how do we determine which objects need to be recycled? This requires some judgment to determine which objects need to be reclaimed. There are generally several methods:
reference
2.2 What are the differences between Final, finally and Finalize
Final and finally are easier to understand. First, final is used to modify objects that are immutable; Finally is a mechanism to ensure that important code must be executed. It is typically used in try-catch-finally statements. But what is Finalize? Before explaining the standard code, it’s back to the GC algorithm. First, Finalize is a method of Object for the collection of a particular resource. As mentioned above, when GC Roots are unreachable, the object is considered to be no longer in use, but the object does not have to be “dead”. When GC Roots are unreachable, the system will first judge whether to finalize the object. If not, the object will be recycled directly. If it can be executed, it will be put in the queue and finalize thread will execute it. If there are other objects associated with Finalize thread, it will judge that the object cannot be recycled. Otherwise, Finalize thread will execute it once, as shown in the following figure:
Due to its uncertainty, as of JDK9, it has been marked deprecated
2.3 Will the method area recycle resources?
Although the Java heap can reclaim 70% to 95% of the space, the method area can also reclaim some resources. The method area mainly recycles discarded constants and useless classes in two parts.
2.4 What are the algorithms for garbage collection and their characteristics?
Based on the above mentioned, GC Roots are unreachable and can be recycled. The derived algorithms are shown below (it is recommended that the default understanding of each algorithm be adopted) :
JAVA garbage Collection mechanism (a) – Object collection and algorithm understanding
JAVA garbage collection mechanism (two) – GC collection concrete implementation
Third, class loading problems
Dive into the Java virtual machine — class file structure (bytecode) Dive into the Java Virtual machine — class loading mechanism
- Class loading process
- Print the following code to print the information, if changed to system.out.println (child.c_value); To the System. The out. Println (Child. Value); How to?
public class Parent{
static {
System.out.println("Parent");
}
public static int value = 123;
}
public class Child extends Parent{
static {
System.out.println("Child"); } public static int c_value = 123; Public static void main(String[] args) {system.out.println (child.c_value); }Copy the code
- Tell me what you know about classloaders
- What is the parental delegation model
3.1 Class loading process
The process of class loading is shown below (it is recommended that the understanding of each step be default) :
Load, validate, prepare, initialize, and unload
3.2 Write the following code to print information, if changed to system.out.println (child.c_value); To the System. The out. Println (Child. Value); How to?
public class Parent{
static {
System.out.println("Parent");
}
public static int value = 123;
}
public class Child extends Parent{
static {
System.out.println("Child"); } public static int c_value = 123; Public static void main(String[] args) {system.out.println (child.c_value); }Copy the code
Print the following information:
Parent
Child
123
Copy the code
System.out.println(child.value)
Parent
123
Copy the code
Java Virtual Machine class loading mechanism extension
class Parent{
public static int value = 1;
static {
value = 2;
}
}
class Child extends Parent{
public static int B = value ;
}
public static void main(String[] args) {
System.out.println(Child.B);
}
Copy the code
Output what?
3.3 Tell us what you know about classloaders
From the above we know that when a class is loaded, it loads the binary stream with a fully qualified name, which is automatically done by the system. If this action is done externally so that we can get the required classes, we become class loaders. For example, a class bytecode is retrieved from a path, and the corresponding information is retrieved through reflection.
3.4 What is the parental delegation model
The engineering flow is as follows: when a classloader receives a classload request, it first does not try to load the class itself, but delegates it to its parent classloader. This is true at every level of classloaders, so all loaders are passed to the parent. Only if the parent loader fails to complete, the child loader will attempt to load itself. Its model is as follows: