This is the 19th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021
JVM memory structure
The thread isolation area includes:
-
The virtual machine stack
Describes a method to perform the memory model, thread is private, life cycle is the same as the thread, each method will be performed at the same time create a stack frame (see below), which save execution method of local variables and the operand stack method, dynamic connection and the return address information, such as method execution stack, the method of execution of the stack, The data is emptied out of the stack, and the timing of loading and unloading is clear, so this area does not need to be GC
-
Local method stack
Similar to the virtual stack function, the main difference is that the virtual stack serves when the VIRTUAL machine executes Java methods, while the local method stack serves when the virtual machine executes local methods. This area also does not require GC
-
Program counter
Thread-specific, it can be thought of as a line number indicator of the bytecode being executed by the current thread
Thread shared area:
- Methods area
Main class information, constants, static variables, just-in-time compiler compiled code, etc. This is partly implemented in the heap and managed by GC, but since the permanent generation has an upper limit of -xx :MaxPermSize, So if you generate classes dynamically (putting class information into the persistent generation) or do a lot of String.intern (putting field strings into the constant area of the persistent generation), you can easily make OOM
-
The heap area
Object instances and arrays are allocated on the heap,
-
Local memory (not available until java8)
Thread shared area. In Java 8, local memory, also known as off-heap memory, consists of meta-space and direct memory.
In Java 8, the implementation of the method area was moved to the local memory meta-space, so that the method area is out of the control of the JVM and GC is not performed, thus improving performance.
-
Direct memory
It is not part of the virtual machine run-time data area, nor is it an area of memory as defined in the Java Virtual Machine specification;
- If NIO is used, this area is frequently used and can be directly referenced and manipulated within the Java heap using directByteBuffer objects;
- This block of memory is not limited by the Java heap size, but is limited by the total native memory, which can be set by MaxDirectMemorySize (the default is the same as the maximum heap memory).
When does a memory stack overflow occur
- If the stack depth requested by the thread is greater than the maximum depth allowed by the virtual machine, StackOverFlowError is thrown, resulting from a recursive call to the method.
- An OutOfMemory exception is thrown if the JVM can scale dynamically, and the scaling action has been attempted, but there is not enough memory to complete the scaling, or there is not enough memory to create the corresponding JVM Stack when a new thread is created.
Describes the process of new an object
-
When virtual opportunities to a new instruction, first to check whether the parameters of this instruction can could locate in the constant pool into a class symbol references (i.e., class with path name), and check whether the notation to refer to the class of the representative has been loaded, parsed and initialization, authentication is the first time to use this class. If not (and not for the first time), the corresponding classloading procedure (class.forname()) must be performed first.
-
After the class load check passes, the virtual machine next allocates memory for the newly generated objects. The size of memory required by an object can be fully determined after the class is loaded. The task of allocating space for an object is equivalent to dividing a certain size of memory from the Java heap. There are currently two common methods, using different allocation mechanisms depending on the garbage collector used.
-
After memory is allocated, the virtual machine needs to initialize all data types in the allocated memory space to zero (excluding object headers).
-
The virtual machine makes the necessary Settings for the object header
Object memory layout:
-
The init() method of the object is called to assign a value to an object property based on the value of the property passed in
-
Create a new object reference in the thread stack and point to the newly created object instance in the heap
Will Java objects be allocated on the stack
Will be. The JVM provides a concept called allocation on the stack, which allocates memory not in the heap for objects whose scope does not escape the method, but in the stack (thread private, which belongs to the stack memory) after the object is allocated. The collection of stack space increases the overall performance of the application by reclaiming the scattered objects allocated on the stack without placing additional unwanted burden on the GC
What algorithms are available to determine whether an object is reclaimed, and what is used most by the actual virtual machine?
-
Reference counting algorithm
If an object is referenced once, the number of references is appended to its object. If it is not referenced (the number of references is 0), the object is recyclable. Disadvantages: References may not be garbage collected
-
Reachability algorithm (used by virtual machines)
Starting with a series of objects called GC Root, leading to the next node that they point to, and the next node that this node points to… Until all nodes have been traversed, objects that are not in any reference chain starting with GC Root are considered “garbage” and will be collected by GC.
The following objects can be used as GC_Root
- The object referenced in the virtual machine stack (the local variable table in the stack frame)
- The object referenced by the class static property in the method area
- The object referenced by the constant in the method area
- Objects referenced by JNI (commonly referred to as Native methods) in the Native method stack
What are the GC collection algorithms? What are their characteristics?
-
Mark clearing algorithm
First, the corresponding recyclable objects are marked according to the reachability algorithm, and the recyclable objects are recycled. It’s really easy to operate, and there’s no need to move data around, so there’s fragmentation.
-
Replication algorithm
Divide the heap into two regions, A and B. Region A is responsible for allocating objects, and region B is not allocated. Use the above notation for region A to mark the surviving objects. Then copy all the living objects from region A to region B (live objects are arranged right next to each other) and finally clean up all the objects from region A to free up space, thus eliminating the memory fragmentation problem
-
Tag sorting algorithm
It is the same as mark purge, but it adds a defragmenting process to mark purge, in which all surviving objects are moved to one end, next to each other (as shown), and then all areas of the other end are cleaned up, thus eliminating the memory fragmentation problem.
What does a complete GC flow look like in the JVM? How do objects advance to the old age?
Eden – “Survivor Zone -” The old days
If it’s a big object the first time, if it’s a big object it goes straight to the old age. If not, Eden will enter the Eden region. If the next GC starts, the object Eden needs to reference will be in the FROM region and the age of the object will be +1. In the next gc start, from will be moved to TO and age will be +1.
How many reference relationships are there in Java? What’s the difference between them?
-
Strong reference
When running out of memory, the Java virtual machine would rather throw OutofMemoryErrors to abort the program than randomly recycle objects with strong references to resolve the memory problem. If the strongly cited object is not in use, it needs to be weakened so that the GC can reclaim it
-
Soft references
The garbage collector only reclaims soft-reference reachable objects when memory runs out.
-
A weak reference
A weak reference is simply a reference that does not keep an object in memory very well. With WeakReference, the garbage collector helps you decide when the referenced objects are reclaimed and removed from memory.
-
Phantom reference
The object to which a virtual reference points is very fragile, and we cannot get it by using the GET method. Its only function is to add itself to the reference queue when the object to which it refers is reclaimed, to record that the object to which the reference refers has been destroyed
Final, finally, Finalize
Final: a keyword or modifier in Java. A). If A class is declared final, it means that it cannot be subclassed or inherited as its parent. Therefore, a class cannot be declared both as an abstract class and as a final class. If a variable or method is declared as final, it is guaranteed that it will not be changed during use. 2) Methods declared final can only be used and cannot be overloaded. Finally: A Java exception handling mechanism. Finally is the perfect complement to the Java exception handling model. The finally structure causes code to always execute, regardless of when no exception occurs. Using finally, you can maintain the internal state of an object and clean up non-memory resources. Especially when it comes to closing database connections, if programmers put the close() method of database connections in finally, they can greatly reduce the chances of errors. Finalize: A method name in Java. Java technology uses the Finalize () method to do the necessary cleanup before the garbage collector purifies objects from memory. This method is called by the garbage collector when it determines that the object is not referenced. It is defined in the Object class, so all classes inherit from it. Subclasses override the Finalize () method to collate system resources or perform other cleanup tasks. The Finalize () method is called on an object before the garbage collector removes it.
String s = new String(” XXXX “) creates several objects
2 object String constant pool, heap area new a String object