Overview:
When the Java virtual machine is started, heap memory and method areas are created
The method area mainly stores shared information such as class information, constants, and static variables loaded by the virtual machine.
When a thread is generated, the virtual machine creates a virtual machine stack, a local method stack, a counter, and assigns it to the thread. When the thread executes a method, a stack frame is generated. The local variables and parameters in the method (basic data) Type and reference type) will be stored in the stack frame, the object instance and array instance information in this method will be stored in the heap memory
When entering a method, the previous stack frame will be pushed on the stack, and the new method will be placed on the top of the stack. After executing the new method, the execution code segment of the previous method will be found according to the return address in the stack frame, and the stack will be popped.
When the execution of this thread is completed, the stack will be destroyed accordingly
The JVM memory area is divided into five parts, namely heap, method area, virtual machine stack, local method stack, and program counter
Program counter (PC Register)-thread private
The program counter is a line number indicator that records the bytecode executed by the current threadSo that when multi-threaded concurrent processing, the CPU informs the CPU of the completed log after context switching, avoiding repeated operations
Virtual machine stack-thread private
The virtual machine stack is a memory area in the virtual machine memory, which is private to the threadIt is used to store stack frames, a stack frame corresponds to a methodThe pop and push of the stack frame correspond to the end of the method and the beginning of the call-Xss refers to setting the stack size of each thread. Before JDK1.5, the default stack size was 256K, and the default size after that was 1M
Stack frame : A complete stack frame holds the following information:
Local variable table Operand stack Stack frame information
Local variable table----StackThe local variable table stores method parameters and local variablesThese entries can be basic type data and reference data type
Operand stackThe operand stack can be understood as a temporary data storage area used for calculation in the java virtual machine stack. The result will be pushed onto the stack and then popped up to the local variable table.
Stack frame informationThe stack frame information includes: the entry address of the method itself, the return address of the method, debugging information or other additional information
There is an exception :
-
StackOverflowError :
- JVM will allocate a certain memory size for the virtual machine stack of each thread ------> -Xss parameter
Therefore, the number of stack frames that the virtual machine stack can hold is limited. If the stack frames continue to be pushed into the stack but not out of the stack, the memory space of the current thread virtual machine stack will eventually be exhausted and an exception will be thrown.
-
OutOfMemoryError (OOM) :
- If the virtual machine cannot apply for enough memory when expanding the stack, it will throw an OutOfMemmoryError exception
Native Stack-thread private
Like the virtual machine stack, it has the characteristics of thread isolation and can throw StackOverflowError and OutOfMemoryError exceptionsThe object served by the local method stack is the native method executed by the JVM, while the virtual machine stack serves the java method executed by the JVM
Java heap (Head)-thread sharing
The Java heap is a memory area shared by all threads, created when the virtual machine startsThe only purpose of this memory area is to store object instances. Almost all object instances allocate memory here
JVM memory is divided into heap memory and non-heap memoryHeap memory:(1/3) Young Generation:||===(8/10)Eden||===(2/10) Survivor area||===(1/10)FromSpace||===(1/10)ToSpace(2/3) Old GenerationNon-heap memory:Permanent Generation (Permanent Generation)Heap memory:Objects are stored, and the garbage collector collects these objects and then recycles them according to the GC algorithmNon-heap memory:The permanent generation, also called the method area, stores long-lived objects such as class metadata, methods, constants, attributes, etc.Young generation (New) :The young generation is used to store the Java objects just allocated by the JVMTenured (Tenured):Objects that are not collected by garbage collection in the young generation will be copied to the old generationPermanent Generation (Perm):Permanently store Class and Method meta-information, the size of which is related to the scale of the project, the amount of classes, and methods. Generally, 128M is sufficient. The setting principle is to reserve 30% of the space
Remarks:JDK1.8 version obsolete the permanent generation, instead of MetaSpace (MetaSpace)Metaspace is similar to permanent generation, and both are the realization of method areaThe biggest difference between them is: the meta space is not in the JVM, but uses local memoryThere are two parameters in the meta space:MetaspaceSize: Initialize the size of the metaspace and control the GC thresholdMaxMetaspaceSize: Limit the upper limit of the metaspace size to prevent abnormally occupying too much physical memory
Method Area
Method area is also called "permanent generation"It is used to store class information, constants, and static variables loaded by the virtual machine, and is a memory area shared by each threadIn the HotSpot JVM before JDK8, these "permanent" areas are called "permanent generation"Permanent Generation is a contiguous heap space before JVM startup by setting parameters on the command line -XX: MaxPermSize to set the permanent generation ofThe maximum allocatable memory space, the default size is 64M (64-bit JVM default is 85M)After JDK8, JVM no longer has a permanent generation (PermGen)But the class metadata information (metadata) is still there, but it is no longer stored in a continuous heap space, but moved to a local memory called "Metaspace" (Native memory)
Method area or immortal generation related settings-XX:PermSize=64MB minimum size, initial allocation-XX:MaxPermSize=256MB Maximum allowable allocation size, allocated on demandSet garbage not recycling-XX:+CMSClassUnloadingEnabled-XX:+CMSPermGenSweepingEnabledDefault sizeThe default MaxPermSize under the -server option is 64mThe default MaxPermSize under the -client option is 32m
JVM memory parameter settings
-Xms—————————— Set the minimum space size of the heap
-Xmx—————————— Set the maximum space size of the heap
-Xmn—————————— Set the young generation size
-XX:NewSize—————————— Set the minimum space size of the new generation
-XX:MaxNewSize—————————— Set the maximum space size of the new generation
-XX:PermSize—————————— Set the minimum space size of the permanent generation
-XX:MaxPermSize—————————— Set the maximum space size of the permanent generation
-Xss—————————— Set the stack size of each thread
-XX:+UseParallelGC————————Select the garbage collector as the parallel collector
This configuration is only valid for the young generation-that is, under the above configuration, the young generation uses concurrent collection, while the old generation still uses serial collection
-XX:ParallelGCThreads=20—————————— Configure the number of threads of the parallel collector——that is: how many threads are garbage collected together at the same time
Typical JVM parameter configuration reference:
java-Xmx3550m-Xms3550m-Xmn2g-Xss128k
-XX:ParallelGCThreads=20
-XX:+UseConcMarkSweepGC
-XX:+UseParNewGC
Analysis:
-Xmx3550m:
Set the maximum available memory of JVM to 3550M
-Xms3550m:
Set JVM initial memory to 3550m
This value can be set the same as -Xmx to avoid the JVM reallocating memory after each garbage collection is completed
-Xmn2g:
Set the young generation size to 2G.
The entire heap size = young generation size + old generation size + persistent generation size
The permanent generation generally has a fixed size of 64m, so after increasing the young generation, the size of the old generation will be reduced
-Xss128k:
Set the stack size of each thread.
After JDK5.0, the stack size of each thread is 1M, and the stack size of each thread is 256K before.
It can be adjusted according to the memory size required by the threads of the application. Under the same physical memory, reducing this value can generate more threads
However, the operating system still has a limit on the number of threads in a process, and it cannot be generated indefinitely. The experience value is 3000~5000