Summary of JVM memory management

Summary of JVM memory management


foreword

What is JVM? First of all, what is J, what is VM, and what are its characteristics?
How is our JVM memory divided, what is the responsibility of each part of the division, and what is the main storage?
What are our common garbage collection algorithms, and what are their advantages and disadvantages?
What are some parameter configurations of the JVM, and how to configure them?


1. Overview of JVM

Virtual machine: VM, Virtual Machine

  • Logically, a virtual computer
  • In effect, a piece of software that executes a series of virtual computing instructions
  • system virtual machine
    • emulation of a physical computer
    • Such as VMWare, Oracle VirtualBox, etc.
  • software virtual machine
    • dedicated to a single computing program
    • Such as JVM, etc.

JVM now mainly includes Oracle's JDK (Hospot VM / JRockit VM) and OpenJDK

Two, JVM memory classification

2.1. Java automatic memory management

  • Traditional programming languages: memory is managed manually by the programmer

① C / C++, malloc to apply for memory and free to release memory
② Due to programmer negligence or program exceptions, memory leaks

  • Modern Programming Languages: Automatic Memory Management

① Java / C#, using automatic memory management
② Programmers only need to apply for use, the system will check useless objects and reclaim memory
③ The system manages memory in a unified manner, the memory usage is relatively high, but exceptions may also occur

JVM memory model diagram:
insert image description here
The picture comes from Zhihu @creep

JVM memory:

  • thread private memory

① Program Counter Register
② Java Virtual Machine Stack (JVM Stack)
③ Native Method Stack

  • multithreaded shared memory

① Heap
② Method Area
runtime constant pool

① Program Counter Register

  • Program Counter Register, a small memory, each thread has
  • PC stores the current method
  • The method a thread is executing is called the thread's current method
  • When the current method is a local (native, method written in C language) method, the value of pc is undefined (undefined)
  • When the current method is a non-native method, pc contains the address of the currently executing instruction
  • The only block currently that does not raise an OutOfMemoryError exception

②JVM stack (JVM Stack, Java stack)

  • Each thread has its own independent Java virtual machine stack, thread private
  • -Xss set per thread stack size
  • Java method execution is stack-based
  • Each method corresponds to the process of pushing and popping a stack frame from the call to the completion of the stack
    • Stack frame stores local variable table, operand stack, etc.
    • There are things in the "stack" in the local variable table storage method
  • exception raised
  • The depth of the stack exceeds the depth specified by the virtual machine, and StackOverflowError is abnormal
  • Unable to expand memory, OutOfMemoryError exception

③Native Method Stacks

  • Store the execution information of the native method, thread private
  • The VM specification does not clearly stipulate the local method stack
  • exception raised
  • The depth of the stack exceeds the depth set by the virtual machine, and StackOverflowError is abnormal
  • Unable to expand memory, OutOfMemoryError exception

④ Heap (Heap)

  • Created when the virtual machine starts, shared by all threads, occupying the largest area
  • Both object instances and arrays allocate memory on the heap
  • Main areas of garbage collection
  • set size
    • -Xms initial heap value, -Xmx maximum heap value
  • exception raised
  • Unable to meet memory allocation requirements, OutOfMemoryError exception

⑤Method Area (Method Area)

  • Stores the structure of classes loaded by the JVM, shared by all threads
  • Runtime constant pool, class information, constants, static variables, etc.
  • Created when the JVM starts, it is logically part of the heap (Heap)
  • rarely do garbage collection
  • exception raised
  • Unable to meet memory allocation requirements, OutOfMemoryError exception

⑥ Run-Time Constant Pool (Run-Time Constant Pool)

  • Runtime representation of the constant pool in the Class file

  • part of the method area

  • dynamic

    • The Java language does not require constants to be generated only at compile time
    • Such as the String.intern method
  • Exception raised
    - Unable to satisfy memory allocation requirements, OutOfMemoryError exception

insert image description here

3. JVM memory parameters

JVM default running parameters:

  • Important configurations that support JVM running vary according to operating system/physical hardware
  • Use -XX: +PrintFinal to display the parameters of the VM
  • Such as java -XX:+PrinntFinal -version | findstr HeapSize to view the configuration parameters of the heap memory

Two types of parameters for program startup:

  • Program parameters: required by the program, stored in the formal parameter group of the main function
  • Virtual machine parameters: change the default configuration to guide the process to run

Heap

  • Shared, large memory, stores all objects and arrays
  • -Xms initial heap value, -Xmx maximum heap value

JVM stack

  • Thread-private, storing the content of each method in the class
  • -Xss maximum stack size

Method Area

  • Storage class information, constant pool, etc.
  • Before 1.7, permanent area (Perm), -XX:PermSize, -XX:MaxPermSize
  • 1.8 and later, metadata area, -XX: MetaspaceSize, -XX:MaxMetaspaceSize

4. Java object reference

garbage collector:

  • The JVM has a built-in garbage collector
    • GC,Garbage Collector
    • Automatically clear useless objects and reclaim memory
  • What the Garbage Collector Does (John McCarthy, Lisp)
    • What memory needs to be collected (judging useless objects)
    • When to recycle (when to start, does not affect the normal execution of the program)
    • How to recycle (recycling process requires fast speed/short time/little impact)

Determine useless objects and recycle them:

  • Determining useless objects based on object references
    • Zero references, cross references, etc.
  • object reference chain

    Through a series of objects called "GC Roots" as the starting point, starting from these nodes to search downwards, the path traveled by the search is called a reference chain (Reference Chain), when an object is connected to GC Roots without any reference chain ( In the words of graph theory, when the object is unreachable from GC Roots), it proves that the object is unavailable.

GC Roots objects include:

  • Objects referenced in the virtual machine stack
  • Objects referenced by class static properties in the method area
  • Objects referenced by constants in the method area
  • Objects referenced in the native method stack

Strong reference:

  1. 例如 Object obj = new Object(); Object obj2 = obj;
  2. As long as the strong reference still exists, the object will not be recycled, even if an OOM (Out Of Memory memory overflow) exception occurs

Soft references:

  1. Describe objects that are useful but not necessary
  2. These objects will be listed as recyclable before a memory overflow exception occurs in the system
  3. JDK provides the SoftReference class to implement soft references

Weak Quote:

  1. Describe non-essential objects, weaker than soft references
  2. Objects associated with weak references can only survive until the next garbage collection occurs
  3. JDK provides the WeakReference class to implement weak references

phantom reference:

  1. The weakest reference relationship, JDK provides PhantomReference to realize virtual reference
  2. The only purpose of setting a phantom reference association for an object is to receive a system notification when the object is recycled by the collector for object recycling tracking
  3. It may be recycled anytime and anywhere, maybe it is defined now, and it will be recycled the next time it is used

Five, garbage collection algorithm

1. Reference counting

  1. an ancient algorithm
  2. Every object has a reference counter
  3. There is a reference, the counter is incremented by one, and when the reference is invalid, the counter is decremented by one
  4. Objects with a counter of 0 will be recycled

Advantages: simple, high efficiency
Disadvantages: mutual circular references between objects cannot be identified

insert image description here

2. Mark-Clear

  1. Mark phase: Mark all objects that need to be recycled
  2. Recycling phase: Collect all marked objects uniformly

Advantages: Simple
Disadvantages: Low efficiency, memory fragmentation As
insert image description here
you can see here, there will be memory fragmentation after clearing

3. Replication Algorithm

  1. Divide the available memory into two pieces of equal size according to capacity, and only use one of them at a time.
  2. When the memory of this block is used up, copy the surviving object to another block.
  3. Then clean up the used memory space at once

Advantages: Simple and efficient
Disadvantages: 1. Less available memory 2. When the object survival rate is high, there are more copy operations

insert image description here

4. Marking-Organizing

  1. Mark phase: Same as "mark-clear" algorithm
  2. Finishing phase: Let all surviving objects move to one end, and then directly clean up the memory outside the end boundary

Advantages: 1. Avoid fragmentation 2. No need for two pieces of the same memory
Disadvantages: 1. High calculation cost, mark clearing + defragmentation 2. Update reference address

insert image description here

5. Generational collection

  1. The life cycle of Java objects is different, long and short
  2. According to the life cycle of the object, the memory is divided into the new generation and the old generation
  3. The Young Generation
    mainly stores objects with a short life cycle.
    Newly created objects are put into the Young Generation first, and most of the newly created objects are recycled during the first GC
  4. Tenured Generation (Tenured Generation)
    An object that survives several gcs will be placed in the Tenured Generation.
    These objects can live for a long time, or accompany the program for a lifetime, and need to be resident in memory, which can reduce the number of recycling

Adopt suitable collection algorithm according to the characteristics of each age

  • New Generation: Replication Algorithm

insert image description here
Our newly created objects will first be placed in Eden Space and From Space. After a gc, we will put the surviving objects in the two areas of Eden and Space into the To area, and then clear Eden and Space. Next time The newly created objects will be placed in the Eden and To areas, and then the surviving objects will be placed in the From area after gc, and so on.

  • Old Generation: Mark Sweep or Mark Collate
    insert image description here

Six, JVM heap memory parameter settings

Heap memory parameters:

  1. -Xms initial heap size
  2. -Xmx maximum heap size
  3. -Xmn young generation size
  4. -XX: SurvivorRatio sets the ratio of eden area/from(to)
  5. -XX: NewRation sets the ratio of old generation/new generation
  6. -XX:+PrintGC / -XX:+PrintGCDetails Print GC process information

HotSpot's existing garbage collector (JDK 13)

  • Serial Collector
  • Parallel Collector
  • CMS Collector (Concurrent Mark Sweep Collector)
  • G1 Collector (Garbage-First Collector)
  • Z Garbage Collector

Guess you like

Origin blog.csdn.net/CXgeng/article/details/123482410