Java Virtual Machine memory management principle basics, Java Virtual Machine
Jdk: Java programming language, Java virtual machine, and Java API class library.
Jdk is the minimum environment used to support Java program development.
Jre: Java se api subset and Java Virtual Machine in the Java API class library.
Jre is a standard environment that supports Java program running.
New
Memory division method: Pointer collision (heap memory normalization, memory allocation by moving the pointer), idle list (heap memory normalization, memory allocation by updating the memory list ).
TLAB(Thread Local Allocation Buffer): a Buffer is allocated to the Local Thread. An independent space is allocated to the created Thread to avoid memory Allocation conflicts and improve the memory Allocation efficiency.
Object Memory Layout: Object Header, Instance Data, and Padding ).
Object Access and positioning: Java programs operate on specific objects on the stack through reference data on the stack.
Method: handle and direct pointer (Sun HotSpot ).
Heap memory
1. reference counting algorithm: Add a reference counter to the object. Every time a counter is referenced in a certain place, add 1 to the counter. When the reference fails, the counter is reduced by 1; objects whose counter is 0 at any time cannot be used again.
2. Accessibility Analysis Algorithm: the "GC Roots" object is used as the start point for downward search. It is proved that an object is unavailable when it is connected to GC Roots without any reference chain.
To be or not to be!
1. Use the above algorithm to determine whether an object is used;
2. If it is no longer used, Mark and filter it;
Whether it is necessary to execute the finalize () method for click farming, which is not required in the following situations:
L The object does not overwrite the finalize () method;
L The finalize () method has been called by the virtual machine;
3. If necessary, place the object in the F-Queue;
4. A virtual machine automatically creates a low-priority Finalizer thread to execute it, that is, it is triggered by the virtual machine;
5. GC marks the objects in the queue for the second time. That is, the collection can be removed after the object is referenced again;
Note: The finalize () method of any object is called only once by the system!
Method Area Recycling
Two main parts: discard constants and useless classes
Control Parameters
-Xnoclassgc: disables the class garbage collection function of virtual machines;
-Verbose: class: monitors the number of classes loaded;
-XX: TraceClassLoading,-XX: TraceClassUnLoading: prints the process information of the class being loaded and detached;
Note: In scenarios where reflection, dynamic proxy, CGLib, and other ByteCode frameworks are widely used, JSP is dynamically generated, and frequent custom ClassLoader such as OSGI needs to be uninstalled by virtual machines, to ensure that permanent generation will not overflow!
Garbage collection Algorithm
1. Mark-clearing algorithm: Memory fragments are easily generated
2. replication algorithm: the heap is divided into two different regions: Young is mainly used to store new objects and Old) it mainly stores memory objects with long lifecycles in applications. The Young generation is divided into three regions: Eden, From nation vor, And To nation vor.
By default, the ratio of Young to Old is.
Default Value: Edem: from: to = 8: 1: 1
3. Tag-Sorting Algorithm
4. Generational Collection Algorithms
HotSpot Algorithm for GC
Enumeration of root nodes: Check each node one by one. (Stop The World. During GC execution, all Java execution threads Must be paused)
With the help of OopMap, HotSpot can complete enumeration quickly and accurately.
Security point: GC can be paused only at the security point
Security Zone: security point extension. GC can be paused in the security zone.
Garbage Collector: the specific implementation of memory recycling.
GC log
Common GC Parameters
Memory Allocation Policy
1. Priority of objects in Eden allocation: New Generation GC (Minor GC) and old generation GC (Major GC/Full GC)
2. large objects directly enter the old age:
3. objects that survive for a long time enter the old Age: Define an Age counter for each object. If the object is still alive after the first Minor GC of the Eden, and can be accommodated by the incor, it will be moved to the same vor space and the age of the object is set to 1. Each Minor GC time for an object in a vor, the age increases by 1. When the age increases to a certain degree (15 by default), the object is promoted to the old age.
4. Dynamic Object age determination: virtual machines do not always require objects to be older to a certain extent. If the total size of all objects of the same age in the same vor space is greater than half of that in the same vor space, objects older than or equal to this age can directly enter the old age.
5. space allocation guarantee: the continuous space in the old age is larger than the total size of the new generation object or the average size of previous promotions, Minor GC will be performed; otherwise, Full GC will be performed.
Tools
In JDK1.5, getAllStackTraces () is used to obtain the StackTraceElement object of all threads in the virtual machine.
HSDIS: Code disassembly generated by JIT.
JConsole: Java monitoring and management control platform, based on JMX.
VisualVM: Multi-in-One troubleshooting Tool