Java Virtual Machine (JVM)

1. Overall structure of JVM

  1. Compile Java files into class files using Javac.
  2. The ClassLoader loads the class bytecode into the JVM’s corresponding memory.
  3. The JVM allocates memory to the method area, heap area, stack area, and local mode stack, each of which stores a different part of the bytecode.
  4. The garbage collector (GC) manages garbage in the entire memory space.

2. Compilation and execution of Java code

Java files are compiled into class files using javac commands, and then run with Java commands.

javac Hello.java
java Hello
Copy the code

Class loaders

1) Class loaders in Java

2) Loading process

  • Loading: Class information is fetched from a file and loaded into the JVM’s memory.
  • Verifying: Check whether the structure read in is as described in the JVM specification.
  • Preparing: Allocate a structure to store class information.
  • Considerations: Add direct references to all symbolic references in a class’s constant pool.
  • Initializing: Performs a static initialization procedure to initialize a static variable to a specified value.

4. Memory management

Memory management in Java refers to memory operations in the “Memory space” section below.

1) Java stack area:

Function: Stores all data for Java method execution. Composition: Consists of stack frames, each stack frame representing the execution of a method.

Java stack Frames: Each method is pushed onto and off the virtual machine stack from the time it is invoked to the time it is executed. It describes a method’s local variables, stack operands, dynamic links, and method exits.

2) Local method stack

Consistent with the Java stack area.

What it does: The native method stack is designed to serve native methods.

3) Method area

Stores information about classes loaded by the virtual machine, constants, static constants, just-in-time compiler compilation, etc. (this data takes up memory forever after the program is started).

4) heap area

Action: All objects created by new are allocated in the heap. Features: This is the largest chunk of memory in the virtual machine, and is the part of the GC to reclaim.

For the heap area, the memory structure is a little different, first look at the following figure:

In simple terms, the heap is divided into the Young Generation and the Old Generation. When the program creates objects, the objects are first allocated to the new Generation. When the memory in the new Generation is insufficient, the JVM will transfer the objects from the new Generation to the Old Generation by certain algorithm rules. The JVM throws an OOM exception when neither the new generation nor the old generation has enough memory.

5. Recycling

1. Garbage collection algorithms

1) Reference counting algorithm (before JDK1.2)

Create objects in memory at the same time, for it to create a reference counter, and refer to counter plus 1, each time there is a reference reference to this object, the counter will be cumulative add 1, and when one of the references to destroy, counter will be minus 1, when the reference counter to 0, indicate that the object is garbage objects, the next time the gc, the object will be recycled.

Disadvantages: when object A and object B refer to each other, their reference counters are always positive. When objects are not referenced by other objects, their reference counters are not collected by GC because their reference counters are not 0.

2) Reachability algorithm (JDk1.2 +)

Also known as the root search algorithm. All reference relations of the program are regarded as a graph (directed graph), and all reference nodes are searched from GC Root node. When all reference nodes are searched, the remaining nodes are considered as unreferenced nodes, that is, unreachable nodes, and garbage objects.

Above, ObjD, E, and F are garbage objects because there is no path to them.

2. Type of reference

There are four types of references in Java: strong reference, soft reference, weak reference, and virtual reference. Strong and weak references are the most common in development.

Creation of weak references

/ / strong reference
Object obj = new Object();
Obj and wf both refer to Object
WeakReference<Object> wf = new WeakReference<Object>(obj);
// Disconnect strong references. Only WF references this Object
obj = null;
// Universal weak reference to get Object (possibly null)
wf.get();
Copy the code

When using wf.get(), determine if the object is null, because weak references do not prevent the object from being recycled.

3. Garbage collection algorithm

1) Mark-clear algorithm

Traverse all the references from the root set, above, the root collection cites A, A reference to the C, B is unreachable object references, in the stage of the scanning, B will be marked as spam object, when garbage collection mechanism to perform, will directly to the object B is empty, the memory block will be left with A and C object reference, B will be garbage collected for recycling. Advantages: There is no need to move objects, and only the non-living objects are processed, which is extremely efficient in the case of a large number of living objects. Disadvantages: Because the mark-clear algorithm will directly recycle the non-viable objects, it will cause internal fragmentation, which is not conducive to the allocation of subsequent objects

2) Replication algorithm

Starting from the root set traversal, pictured above, traversed by A is to put A copy to another piece of free memory, continue to traverse, found that B inaccessible, skip, and later found that C can reach, the copy of C in the same way on the free memory, etc. All copy when processed, the original memory space is empty, only keep copy after the memory space. Advantages: Extremely efficient when the number of living objects is small, and there is no memory fragmentation. Disadvantages: Requires a block of memory as swap space for object movement.

3) Mark-collation algorithm

Starting from the root set traversal, based on the scanning of the whole memory area, recycled object scanning, above, in the second stage, will be marked as recycled object B, in the third stage, direct scanning and remove the memory object is marked, at the same time, don’t live in recycling object space, all live objects in the memory will be moving toward the left idle place, And update the corresponding pointer.

Each of these three algorithms has its advantages and disadvantages, and the JVM uses a combination of them when dealing with garbage, not just one of them. When there are few living objects in memory, the “copy algorithm” is used to deal with garbage objects. When there are too many living objects in the memory, the system uses the mark-Tidy algorithm or the Mark-clean algorithm to process garbage objects.

4. Trigger reclamation

  • The Java virtual machine can no longer allocate memory space for new objects
  • Calling the system.gc () method manually (strongly not recommended, not immediately, but stressing the virtual machine)
  • The low-priority GC thread was started.

2. Android VIRTUAL machines

1. Differences between Dalvik VM and JVM

  • The execution files are different, one is class, the other is dex.
  • Class-loaded systems are quite different from JVMS.
  • Multiple DVMS can exist simultaneously, but only one JVM can exist.
  • Dalvik is register-based, whereas the JVM is stack-based.

2. Differences between Dalvik VM and ART

  • DVM uses the JIT to convert bytecode to machine code, which is inefficient.
  • ART uses AOT precompilation technology for faster execution.
  • ART takes up more application installation time and storage space.