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

  1. First, during compilation, if a reference type is encountered and needs to be initialized, the initialized object will be loaded toHeap memory, and then replace the reference to the object with the current field as the identifier
  2. And then after the compilation is complete, all the compiled class information is stored indimensionJdk8 was previously called the method area
  3. When aThread call stackEach method called in the current thread passesdimensionInformation composition inThe stack frameTo make the current thread call in order
  4. inThe stack frameWhen created, we know that the stack frame size, stack depth, etc., have been calculated through each stack frameStack operationData pushing and out of the stack, after the stackThe current stack frameDestroyed, proceed to the next callThe stack frame, until the call chain ends, the result is returned, and the thread is destroyed
  5. If aThe stack frameWhile methods are defined in Java, they are actually made by C++, etc., to make actual computer resource callsThe stack frameThe occupied memory is divided into another area calledLocal method stackIn addition to the memory space is different, everything else and callThe stack frameThe same
  6. inThread call stackRun through the processProgram counterTo record where the next instruction is executed
  7. ifThread call stackAn out-of-memory exception is thrown if there is not enough space to create stack frames during runtime
  8. inThread call stackRun the process, will also encounter a lot of need to initialize the object, will also be initialized after the object loadHeap memory, if theHeap memoryToo 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).

  1. First of all, the newly created objects are placedEden Space, but if the object is large, it will be placed directly in the Old age
  2. whenEden SpaceWhen it’s full, it goesYoungGC (GC)That will beEden SpaceCopies of surviving objectsSurvive FromIn, and then willEden SpaceClear it all out, and continue onEden SpaceCreate object in
  3. whenEden SpaceIt’s full again, andSurvive FromThere are also living objects, and it goes onyoungGCBut what’s a little different about this is thatyoungGCThat will beEden SpaceandSurvive FromAll surviving objects are copied toSurvive To, and then clearEden SpaceandSurvive Fromarea
  4. The next timeEden SpaceIt’s full again andSurvive ToThere are living objects, and there will beyoungGC, this operation is the same as last time, but the change is willEden SpaceandSurvive ToAll surviving objects are copied toSurvive FromIn the
  5. This is recycled when some object is inSurvive FromandSurvive ToCycle N times (Default 15 times) after that, if it still exists, it will be put inThe Old Old s
  6. whenThe Old Old sWhen the memory is full, it will proceedFull GC.Full GCwillThe Old Old sandYoung, Young generationAll 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
  1. – xmx-xms allows you to set the heap size. It is better to set the same value. Otherwise, capacity expansion consumes resources
  2. If the heap memory is not set, the default is 1/4 of the total memory
  3. 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
  4. 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
  5. 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
  1. Java1.7 Use -xx :PermSize and -xx :MaxPermSize to set the size of the meta-space
  2. Java1.8 Uses the -xx :MetaspaceSize and -xx :MaxMetaspaceSize parameters to set the metasspace size
  3. 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
  1. 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
  2. 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.