The JVM in full
Local method interface
- Local method, one
native method
Is a Java interface that calls non-Java code, the implementation of which is not implemented in Java - Native method interface, fusing different programming languages for Use in Java, originally intended to fuse C/C ++ programs
native
A modified method has a method body, not an abstract method, and the code is not implemented in Java
- To interact with the Java environment, A Java application needs to interact with the environment outside of Java and exchange information with the underlying system, such as the operating system or some hardware. The local method is a communication mechanism that provides some very concise interfaces
- Interacting with the operating system, which is c/ C ++ at the bottom, the JVM relies on a real operating system and needs to use features of an operating system that Java does not provide encapsulation for
- Sun’s Java, Sun’s interpreter is implemented in C, can interact with the outside like some ordinary C, Java needs to call C/C ++ methods
- Change methods are used less and less, except in hardware-related applications such as Java programs driving printers or systems managing production equipment
Local method stack
- Used to manage calls to local methods, thread-private
- Allows memory sizes to be implemented as fixed or dynamically expandable, existing
StackOverflowError/OutOfMemoryError
abnormal
- Thread requests allocate more stack capacity than the maximum allowed by the local method stack
- When scaling dynamically, there is not enough memory to allocate when trying to scale, or not enough memory to create the corresponding local method stack when creating a new thread
- When a local method is called, the local method is pushed onto the local method stack, and the execution engine loads the local method library
- When a thread calls a local method, it enters a new world that is no longer restricted by the virtual machine, with the same permissions as the virtual machine
- Access the runtime data area inside the virtual machine through the local method interface
- Registers in the local processor can be used directly
- Allocate any amount of memory directly from the heap of local memory
- Not all JVMS support local methods and implement local method stacks; in
hotspo jvm
The virtual machine stack and the local method stack are merged into one
The heap
An overview of the
- Multiple threads in a process (per program) share a heap space
- A JVM instance has only one heap. The heap is created at startup and its size is determined. The size of the heap can be adjusted
- The heap can be in a physically discontinuous memory space (virtual memory can build a map from physical memory, physically discontinuous, virtual memory can be contiguous), but logically contiguous
- All threads share the heap, where thread-private buffers can be partitioned
Thread Local Allocation Buffer, TLAB
- All object instances, as well as arrays, should be allocated on the heap at run time; Arrays and objects may never be stored on the stack because the stack frame holds a reference to the location of the object or array in the heap
- Objects in the heap are not removed immediately after the method ends, but only during garbage collection; The heap is
Garbage Collection
Focus areas for performing garbage collection
Memory segment
- Modern garbage collectors are largely based on
Generation collection theory
- Before JKD7, it was divided into new area/New generation/young generation, old-age area/old age area/old age area, and permanent area/permanent generation
- After JKD8, it is divided into newborn area/New generation/young generation, elderly area/old age area/old age area and meta-space
- The permanent generation/meta-space does not belong entirely to the heap, but to the method area in the concrete logical implementation
- In fact, the heap allocates memory for the new generation (
eden space + survivor * 2
) + the old days
- Actual storage process
eden space
andAny survivor
Can hold classes, and there is always onesurvivor
Is empty and involves a copy algorithm for garbage collection
Set and view the heap size
- By default, the initial memory size is
Physical computer memory size /64
; The maximum memory size isPhysical COMPUTER memory size /4
. Is a small part of the computational memory sizesurvivor
The size of the garbage collection results in two replication algorithmssurvivor
There is always one that cannot be used
Long initialMemory = Runtime.geTruntime ().totalMemory() / 1024/1024; Long maxMemory = Runtime.geTruntime ().maxMemory() / 1024/1024;Copy the code
- run->eidt configurations->modify options->add vm options->
-Xms20m -Xmx20m
-Xms
Set heap start memory, equivalent to-XX:InitialHeapSize
-Xmx
Set the maximum heap memory, equivalent to-XX:MaxHeapSize
-XX:+PrintGCDetails
Print the details of gc garbage collection- -x JVM running parameters; Ms Memory Start
- It is common to set the two values to the same value to avoid resizing heap memory, causing additional stress on the system and improving program performance
jps
View the process of the current Java programjstat -gc 23372
View the memory usage of a process
- Once the heap size exceeds the maximum memory, it is thrown
OutOfMemoryError: Java heap space
Young generation and old generation
- YoungGen is equal to
Eden + Survivor0/from + Survivor1/to
- Almost all Java objects are created in Eden
- Most of the destruction of Java objects occurs in the new generation
-Xmn100m
This parameter is used to set the memory size of the new generation. If the ratio is set at the same time and there is a conflict, this parameter is used
- Old age, OldGen
- Set the ratio of new generation to old generation
-xx :NewRatio=2 New-generation ratio 1 Old-generation ratio 2 The default value is 2Copy the code
jinfo -flag NewRatio 26371
The console looks at the scale of the process- Set the ratio of Eden to Survivor
-xx :SurvivorRatio=8 8:1:1 Default is 8, actually 6:1:1, need to display the specified -xx: -useadaptivesizePolicy disables the adaptive memory allocation policy, which is actually invalid and 6:1:1Copy the code
jinfo -flag SurvivorRatio 26371
The console looks at the scale of the process
Object allocation procedure
- Garbage collection, frequently in the new generation collection, less in the old age collection, almost no permanent generation/meta-space collection
- General process
- The object of new goes first
Eden area
- when
Eden area
When it is filled, it is recycledYGC YoungGC
, determine which objects need to be reclaimed;survivor0/survivor1
Filled will not triggerYGC
.YGC
toEden and survivor0 / survivor1
All are recycled- Move the remaining objects to
survivor0
, and send itAge counter
- Continue generating objects, repeat Step 2, and move the remaining objects to
survivor1
At this time,survivor1
forEmpty space (to space)
- right
survivor0
Object inYGC
To transfer the remaining objects tosurvivor1
.Age counter +1
- Every time
YGC
And when it’s done,survivor0/survivor1
The space where is empty isto
- When the age counter reaches a certain value (threshold), promotion is required to move the object to
OldGen
- Threshold Settings
-XX:MaxTenuringThreshold=<N>
, the default is 15- Trigger when the old age is full
MGC Major GC
, memory cleaning; If the object cannot be saved at this timeOOM
abnormal
- The whole process
YGC
Later,eden
I still can’t put the super-large object, just put itOldGen
- If the old days don’t let go,
MGC/FGC
Old age garbage collection- if
MGC/FGC
After, the old age also can not put down, throwOOM
abnormalYGC
Later,survivor0/survivor1
Can’t let go, straight promotion inOldGen
The garbage collection
- For HotSpot VM, according to the recycling area, it is divided into two categories, partial collection
Partial GC
; The whole pile of collectionFull GC
- Part of the collection
- Garbage collection, Minor GC/Young GC, is only garbage collection for new generation, and only
Eden
Trigger only when full- Old GC, Major GC/Old GC, Old GC, only
CMS GC
There will be a separate collection of old age- Mixed collection, Mixed GC, collect the whole generation and part of the old generation garbage collection, currently only
G1 GC
There is this kind of behavior
- Whole heap collection, Full GC, garbage collection that collects the entire Java heap and method area/meta-space
- Young generation GC
- Eden full trigger
- Very often, very quickly
- Will lead to
STW Stop The World
, suspend other users’ threads and wait for the garbage collection to finish before resuming the user thread
- The GC old s
- GC that happens in the old days,
Major GC / Full GC
Will result in smaller objects in the old age- for
Major GC
Before, it used to happen a lotMinor GC
, but inParallel Scavenge
The collector’s collection policy is directMajor GC
- When running out of space in the old era, it will try to trigger first
Minor GC
After the space is not enough, triggerMajor GC
Major GC
Velocity general ratioMinor GC
More than 10 times slowerMajor GC
If the memory is insufficient, an OOM exception is raised
- Full GC
- call
System.gc()
, the system recommends executing this commandFull GC
, manual call- There is not enough space in the old era
- Insufficient space in the method area/meta-space
Minor GC
After that, the size of objects entering the old age is greater than the memory available in the old age- by
Eden and the from
toto
When copying, the object size is greater thanto
, the object needs to be moved to the old age, but the object size is larger than the memory size of the old age- Avoid it in development and tuning
Generational idea
- Different objects have different lifecycles, and to optimize GC performance, objects with different lifecycles can be treated differently
- If there is no generational, all regions of the heap are scanned each time
- If you put the newly created objects in one place by generation, after GC, the area where the dying objects are stored is reclaimed to make space
Memory allocation policy
- Object promotion rule
- Preferentially assign to
Eden
- Large objects are allocated directly to the old age, as required
Relatively long continuous memory
Object, avoid too many large objects as much as possible - Long-lived objects are assigned to the old age
- Dynamic age judgment, if
Survivor
The sum of all objects of the same age is greater thanSurvivor
Half of the space, objects whose age is approximately or equal to that age can be directly entered into the old age without reaching the threshold - Space allocation guarantee
-XX:HandlePromotionFailure
, a large number of objects survive after GC, willSurvivor
The object is moved to an older age - TLAB.
Thread Local Allocation Buffer
- The heap is a thread shared area where any thread can access the shared data
- Because object instances are created so frequently, it is not thread-safe to partition memory from the heap in a concurrent environment
- In order to avoid multiple threads operating on the same address, you need to use mechanisms such as locking, but it will affect the allocation speed
- in
Eden
A private cache area is allocated to each thread in theTLAB
When multiple threads allocate memory at the same time, the problem of thread insecurity is avoided and the throughput of memory allocation is improved, which is the fast allocation strategy-XX:+/-UseTLAB
Set whether to enable TLAB space. By default, TLAB space is enabled- By default, TLAB space is very small, accounting for only 1% of Eden space
-XX:TLABWasteTargetPercent
Set up the TLAB space,-XX:TLABWasteTargetPercent=2
- Once the object fails to allocate memory in TLAB space, it will directly try to ensure atomicity of data operation through locking mechanism and allocate memory directly in Eden space
Special case
- The Eden area is too large, resulting in too small S1/S0 space. When doing
Minor GC
Object cannot be moved toS1/S0
Will go straight to the old days, soMinor GC
It doesn’t make sense, and it doesn’t make sense, right S1/S0
Too much,YGC
The frequency of occurrence is too highSTW
User processes are affected
Heap space parameter Settings
-XX:+PrintFlagsInitial
View the default values of all parameters-XX:+PrintFlagsFinal
View the final values of all parameters after setting-Xms<size>
Initial heap space memory-Xmx<size>
Maximum heap space memory-Xmn<size>
Cenozoic size-XX:NewRatio=2
Ratio of Cenozoic to old age in heap structure-XX:SurvivorRatio=8
Eden:S0:S1-XX:MaxTenuringThreshold=<N>
The largest age of new generation garbage-XX:+PrintGCDetails
The detailed GC processing log printing brief information is displayed-XX:PrintGC / -verbose:gc
-XX:HandlePromotionFailure=true/false
Is there a space allocation guarantee that allows promotions to fail and expire after JDK7?
- Occur in the
Minor GC
Before, check whether the maximum available continuous space of the old age is greater than the total space of all objects of the new generation. If so, it is safe.- If less, if set to true, check whether the maximum available space of the old age is greater than the average size of objects promoted to the old age over time
- If the value is greater than that, it is highly likely to fit, and Minor GC is at risk
- If less than, it means that there is a high probability that it will not be released, then change to Full GC.
- If set to FLase, do Full GC
- After JDK7, as long as the old continuous space is greater than
Total size of new generation or average size of previous promotions
Will be inMinor GC
, otherwise proceedFull GC
Is the heap the only option for allocating object storage
- Escape analysis technology is mature,
On-stack allocation/scalar replacement optimization techniques
However, there is no guarantee that escape analysis can bring certain optimization after performance consumption - If an object does not escape a method after escape analysis, it may be optimized for stack allocation without garbage collection
TaoBaoVM
In theGCIH GC invisible heap
implementationoff-heap
To move objects with long life cycles out of the heap, and the GC cannot manage objects inside the GCIH, reducing the frequency of GC collection- Escape analysis, cross-function global data flow analysis algorithm to reduce synchronous load and memory heap allocation stress in Java programs
- The ability to analyze the scope of a reference to a new object and decide whether to allocate that object to the heap
- The basic behavior is to analyze object dynamic scope
- When an object is defined in a method and is used only inside the method, no escape is considered to have occurred and the object is placed in the stack space, which is removed after the execution of the method
- When an object is defined in a method, it is referenced by an external method, escapes, and is passed as a call parameter to other methods
- After JDK7, escape analysis is enabled by default
-XX:+DoEscapeAnalysis
Open escape analysis;-XX:+PrintEscapeAnalysis
View escape analysis results, available only in debug versions- Escape analysis cannot
At static compilation time
Proceed, must be inJust-in-time JIT compilation
When completed. The reason is that, contrary to the dynamic nature of Java, dynamic proxies change the behavior of a class at run time, and static compilation does not know about class changes
- Escape analysis can only be enabled in Server mode, which is the default on the client
Code optimization
- On the stack allocation, an object can be optimized for stack allocation after escape analysis and never escape
Escape scenario: member variable assignment, method return value, instance reference passing
- Synchronization elision. If an object is found to be accessible only from one thread, operations on that object may be performed without synchronization
- Thread synchronization is expensive, with the consequence of reduced concurrency and performance
- Use escape analysis to determine whether the lock object used by the synchronized block can only be accessed by one thread and is not published to other threads. If not,
JIT
The compiler, when compiling the block, unsynchronizes this part of the code, eliminating the lock/eliding the synchronization- It is still present in the bytecode file, but may be omitted at run time
- Detached objects or scalar substitutions, where an object may be accessible without being a contiguous memory structure, part (or all) of the object may be stored on the stack instead of in heap memory
- Scalar, data that cannot be decomposed into smaller data, primitive data type in Java
- Aggregate the amount of data that can also be decomposed into objects in Java
- After escape analysis, it is found that an object will not be accessed by the outside world. After JIT optimization, the object will be disassembled into several member variables to replace it
- Reduce memory usage
-XX:+EliminateAllocations
, which is enabled by default, allowing objects to be scattered on the stack
- Instead of stack allocation, hotspot VM actually uses scalar substitution
- Intern strings and static variables are allocated on the persistent generation before JDK7, and then directly on the heap, and object instances are allocated on the heap