JVM memory area, memory model + GC

JVM memory area

image.png

 

Method area

 

       Stores data such as class information, constants, static variables, and code compiled by the just-in-time compiler that have been loaded by the virtual machine. Simply put, the method area is used to store the metadata information of the type. A .class file is the form of the class before it is used by the java virtual machine. Once the class is to be used, the java virtual machine will load and connect it (verification, Preparation, analysis) and initialization. And the result of loading (is the conversion from the .class file to a specific data structure in the method area.

 

The size of the method area can be limited by the -XX:PermSize and -XX:MaxPermSize parameters.

The method area mainly has the following characteristics:

1. The method area is thread-safe . Since all threads share the method area, data access in the method area must be designed to be thread-safe. For example, if two threads are trying to access the same class in the method area at the same time, and this class has not been loaded into the JVM, then only one thread is allowed to load it, and other threads must wait

2. The size of the method area does not have to be fixed, the JVM can be dynamically adjusted according to application needs. At the same time, the method area is not necessarily contiguous, and the method area can be freely allocated in a heap (even the JVM's own heap).

3. The method area can also be garbage collected. When a class is no longer in use (unreachable), the JVM will unload this class for garbage collection

Constant pool

 

       The constant pool is an ordered collection of constants used by this type, including actual constants (string, integer, and floating point constants) and symbolic references to types, fields, and methods. The data items in the pool are like array items and are accessed by index .

 

JVM heap

 

       Jvm heap is also a memory area shared by threads. It is created when the virtual machine starts. It is the largest piece of memory managed by the Java virtual machine. It is mainly used to store object instances. Almost all object instances allocate memory here. Note The Java heap is the main area managed by the garbage collector, so it is often called the GC heap. If there is no memory in the heap to complete the instance allocation and the heap can no longer be expanded, an OutOfMemoryError will be thrown.

        

Virtual machine stack

 

The thread is private, and its life cycle is the same as that of the thread. The virtual machine stack describes the memory model of Java method execution: when each method is executed, a stack frame is created at the same time to store information such as the local variable table, operation stack, dynamic link, and method exit .

 

Native method stack

 

Native MethodStacks (Native MethodStacks) and virtual machine stacks play very similar roles. The difference is that the virtual machine stack serves the virtual machine to execute Java methods (that is, bytecode), while the native method stack serves the virtual machine. Native method service used by the machine.

 

Program counter

 

The program counter is a small memory space that can be regarded as a line number indicator of the bytecode executed by the current thread. Basic functions such as branches, loops, jumps, exception handling, and thread recovery all need to rely on this counter to complete.

 

Threaded

 

Memory model

The main goal: to define the access rules of each variable in the program, that is, to store variables in the virtual machine and take out the low-level details of variables from the memory.

image.png

 

The Java memory model stipulates:

1. All operations (reading, assignment) of variables by threads must be performed in working memory, and variables in main memory cannot be directly read and written

2. Different threads cannot directly access the variables in each other's working memory, and the transfer of variable values ​​between threads needs to be completed in the main memory

 

The memory model mainly uses two ways to solve the concurrency problem:

  • Limit processor optimization
  • Use memory barriers

 

nature

Atomicity

In Java, to ensure atomicity, two advanced bytecode instructions, Monitorenter and Monitorexit, are provided.


In the article on the realization principle of Synchronized, it was introduced that the keywords corresponding to these two bytecodes in Java are Synchronized.

Therefore, Synchronized can be used in Java to ensure that operations within methods and code blocks are atomic.

Visibility

The Volatile keyword in Java provides a function, that is, the modified variable can be synchronized to the main memory immediately after being modified.


The variables modified by it are refreshed from the main memory before each use. Therefore, Volatile can be used to ensure the visibility of variables during multi-threaded operations.

Orderliness

In Java, Synchronized and Volatile can be used to ensure the orderliness of operations between multiple threads.

 

Synchronized and Volatile comparison

 

Synchronized and Lock comparison

1. Lock can manually release and acquire the lock, synchronized is passive

2. Lock is based on jdk and synchronized is based on jvm

 

GC (garbage collection mechanism)

JVM heap memory structure

image.png

 

Heap memory is divided into three parts: young generation, old generation and permanent generation. The new generation is further divided into three areas: Eden, S0, and S1 (Survivor)

Objects can be distinguished by their survival time. As the survival time becomes longer and longer, they will go to the old generation, and those that will not be cleared go to the permanent generation.

 

Generational collection algorithm

Copy algorithm

 

Algorithm for the new generation: Divide the memory into two pieces and use only one piece. After using it, save the surviving objects in another block, then dispose of this block and reuse it.

The internal ratio of the new generation of hotspot virtual machines: 8:1:1

Mark-clear algorithm

mark

Mark the object space that needs to be reclaimed.

Clear

Clear the marked object space

 

insufficient

1. Marking and removal efficiency is not high enough

2. A large number of discontinuous fragments are generated after cleaning, resulting in the subsequent failure to allocate large objects

Mark-up algorithm

After marking the recycling, move the surviving object space together, and then clean up

Garbage collector

The new generation collectors mainly include Serial collector, ParNew collector and Parallel Scavenge collector. Old age collectors mainly include Serial Old collector, Parallel Old collector and CMS collector. Of course, it also includes a brand new G1 collector that is common to the new generation and the old generation.

Cenozoic collector

Serial collector

Is a single-threaded collector, based on the replication algorithm

Only a single thread is used during garbage collection and all user threads are suspended during the collection process.

Cause the app to stall

ParNew collector

Multi-threading and replication algorithms are used for garbage collection.

Multiple threads are recycled at the same time, so all user threads will be suspended

Cause the app to stall

Throughput = running user code time / (running user code time + garbage collection time)

Old age collector

serial old collector

It is the default old generation collector in jvm mode

Single thread + mark-sorting algorithm is used to realize garbage collection.

Suspend all user threads

Cause the app to stall

Generally, the capacity of the old generation is greater than that of the new generation, so when the garbage collection is made into the old generation, the stw energy time will be longer

parallel old collector

It is the old version of the parallel scavenge collector

Multithreading + marking-sorting algorithm

Used after jdk1.6

cms collector

The old-age collector that truly implements concurrent collection

Multi-threaded concurrency and mark-sweep algorithm to achieve garbage collection

stage:


(1) Initial mark (inital mark)

This stage only marks the objects that GC Roots can directly associate with. The speed is very fast, so basically you can't feel the pause caused by STW.

(2) Concurrent mark

The task of the concurrent marking phase is to start from the object references collected in the first phase, traverse all other object references, and mark all objects that need to be recycled. At this stage, the collection thread and the user thread are executed concurrently and alternately, and the user thread does not need to be suspended, so the application will not be stopped.

(3) Concurrent-pre-clean

The concurrent pre-cleanup phase is to prepare for the next phase, in order to minimize the application pause time.

(4) Remark

At this stage, the mark record of the part of the object whose mark is changed due to the continued operation of the user program during the concurrent mark will be corrected (it is possible that the object is re-referenced or the new object can be recycled). The pause time in this phase is longer than the initial marking phase, but much shorter than the concurrent marking time.

(5) Concurrent sweep

In this stage, garbage collection will be performed, and the memory of those objects that are not used will be reclaimed.

(6) Concurrent reset

The collector does some finishing work so that the next GC cycle can have a clean state.

 

What conditions will trigger GC? Which GC will be triggered?

Method of judging whether to recycle: reference counting method + reachability analysis method

Reference counting method: increment by one once called. Recycle the objects whose count is 0 (disadvantages: cannot check out the ones that call each other)

Accessibility analysis method:

minor GC: eden is full, new objects cannot be allocated

fullGC: The old generation collector is full

 

What are the java GC roots?

Method area, JVM stack, and Native stack are not managed by GC, so objects in these non-heap area are selected as GC roots, and objects referenced by GC roots are not reclaimed by GC.

 

How to tune jvm gc?

  • One is to minimize the number of objects transferred to the old age
  • The other is to reduce the execution time of Full GC

 

Guess you like

Origin blog.csdn.net/sulu0416/article/details/96764998