Now we know that a Java class can be roughly divided into class abstract, constant pool, method stack frame, and so on. After the JVM starts, these classes are loaded into the JVM through the class loader. So how can a class be used and called after it is loaded?
A brief introduction to some of the memory parts encountered during JVM execution
It’s important to remember that all of the footprint of the JVM running is direct memory, so the biggest footprint for Java applications is memory
Operation process
- First, during compilation, if a reference type is encountered and needs to be initialized, the initialized object will be loaded to
Heap memory
, and then replace the reference to the object with the current field as the identifier - And then after the compilation is complete, all the compiled class information is stored in
dimension
Jdk8 was previously called the method area - When a
Thread call stack
Each method called in the current thread passesdimension
Information composition inThe stack frame
To make the current thread call in order - in
The stack frame
When created, we know that the stack frame size, stack depth, etc., have been calculated through each stack frameStack operation
Data pushing and out of the stack, after the stackThe current stack frame
Destroyed, proceed to the next callThe stack frame
, until the call chain ends, the result is returned, and the thread is destroyed - If a
The stack frame
While methods are defined in Java, they are actually made by C++, etc., to make actual computer resource callsThe stack frame
The occupied memory is divided into another area calledLocal method stack
In addition to the memory space is different, everything else and callThe stack frame
The same - in
Thread call stack
Run through the processProgram counter
To record where the next instruction is executed - if
Thread call stack
An out-of-memory exception is thrown if there is not enough space to create stack frames during runtime - in
Thread call stack
Run the process, will also encounter a lot of need to initialize the object, will also be initialized after the object loadHeap memory
, if theHeap memory
Too many objects, unable to load new objects will throw an out-of-memory exception
The space used
- Heap memory
- Meta space (method area)
- Thread stack
- The stack frame
- Program counter
Memory partition
We know that the method is called by a thread, we already passed single-core CPU time, and now the CPU, dual-core, four, eight nuclear, plus hyper-threading technology, now the CPU can be N multi-threaded scheduling together at the same time, in the JVM, too, may be a class method, at the same time there are N threads at the same time in use. So the JVM classifies the memory portion into two categories, thread shared memory and thread private memory
Threads share
Heap memory
From the start of the JVM, all objects created are stored in the heap. Given Java’s object-oriented concept, we can guess that most of the MEMORY of the JVM is occupied by the heap. If we’re constantly creating objects, and we’re constantly creating objects, and we’re constantly saving objects, we’re never going to have enough memory. So based on that, the JVM doesn’t care how we use memory, it manages the heap itself. Using the garbage collection mechanism allows Java objects that we create to be recycled when not in use, freeing up memory, thus creating a concept of the entire object life cycle, from creation to destruction
Partition of heap memory
- Young, Young generation
- Eden Space (Freshmen Area)
- Survive From (Zone 1)
- Survive To (Zone 2)
- The Old Old s
Why do I divide?
Java objects are already in the heap, so why split the heap into so many chunks? In order to facilitate the use of THE GC algorithm, briefly describe how objects move in the heap partition area (using the garbage collection algorithm of mark collation).
- First of all, the newly created objects are placed
Eden Space
, but if the object is large, it will be placed directly in the Old age - when
Eden Space
When it’s full, it goesYoungGC (GC)
That will beEden Space
Copies of surviving objectsSurvive From
In, and then willEden Space
Clear it all out, and continue onEden Space
Create object in - when
Eden Space
It’s full again, andSurvive From
There are also living objects, and it goes onyoungGC
But what’s a little different about this is thatyoungGC
That will beEden Space
andSurvive From
All surviving objects are copied toSurvive To
, and then clearEden Space
andSurvive From
area - The next time
Eden Space
It’s full again andSurvive To
There are living objects, and there will beyoungGC
, this operation is the same as last time, but the change is willEden Space
andSurvive To
All surviving objects are copied toSurvive From
In the - This is recycled when some object is in
Survive From
andSurvive To
Cycle N times (Default 15 times
) after that, if it still exists, it will be put inThe Old Old s
- when
The Old Old s
When the memory is full, it will proceedFull GC
.Full GC
willThe Old Old s
andYoung, Young generation
All non-living objects are cleared
It is roughly the above process. How to remove garbage in the young generation and the old generation will be explained in detail in the next article
Will know
- – xmx-xms allows you to set the heap size. It is better to set the same value. Otherwise, capacity expansion consumes resources
- If the heap memory is not set, the default is 1/4 of the total memory
- By default, the ratio between the young generation and the old generation is 1:2. You can set this parameter by running the -xx :NewRatio parameter
- By default, the ratio of Eden Space, Survive From, and Survive To is 8:1:1 of the total young generation. You can set the ratio by using the -xx :SurvivorRatio parameter
- All of the above is explained in terms of java8’s default garbage collection, parallel GC
dimension
Java8 used to be called a method section, or persistent generation, which uses the same block of memory as the heap. After Java8, it separated the heap from the method section and created a separate block of memory for Java metadata
Will know
- Java1.7 Use -xx :PermSize and -xx :MaxPermSize to set the size of the meta-space
- Java1.8 Uses the -xx :MetaspaceSize and -xx :MaxMetaspaceSize parameters to set the metasspace size
- In java1.8, if you do not specify the size of a meta-space, the default is infinite
Thread private
Thread stack
A thread stack is what we call a stack, which is created as a thread is created, and each method in the call chain of a method in this thread is a stack frame, and all variables in the thread stack are private, and as the thread terminates, the whole thread stack is destroyed. We have covered the contents of stack frames in detail in the bytecode section, so we won’t go into more details
Will know
- When a thread stack to use global Shared variables, is not used directly, but rather to variable values loaded onto a local variable, and then after the operation, to update the back, so between threads and thread, variables are not visible, so under the condition of multithreading, will encounter many threads with variable problem, specific can separate subsequent articles
- The size of a thread stack can be set with the -xss argument
Local method stack
The local method stack is the same concept as the upper thread stack, except that the local method stack is usually called by native methods, which are wrapped in C++
Program counter
Program counter this part of the main function is to record the current thread call information, only this part of the space will not have memory overflow problem, this part did not do a detailed understanding
conclusion
This article focuses on the allocation of JVM memory space, how and why each space is used at runtime, and gives you a deeper understanding of the STRUCTURE of JVM memory.