[summary of JVM knowledge system framework]


JVM memory distribution

  • Thread shared data area:
    Method area – > class information, static variable
    Heap – > array object
  • Thread isolation area
    Virtual machine stack – > method
    Local method stack – > local method library native
  • Heap, program counter
  • JVM run data
    [summary of JVM knowledge system framework]

Program counter

Thread isolation, relatively small memory space, line number of bytecode executed by current thread
Thread is an independent execution unit, executed by CPU
The only place without oom is maintained by virtual machine, so oom will not appear

VM Stack

Executing Java methods

Method call is the process of stack frame into virtual machine stack
Stack frame: local variable table (variable), operand stack (store result of a + b), dynamic link (address of object reference), method exit (value of return)
The stack depth requested by thread is greater than the depth allowed by virtual machine stackoverflowerror

Native Method Stack

It is a JAVA memory area of native method, which also has stack frame
Hotspot combines Java virtual machine stack with local method stack
The JVM standard is to separate the Java virtual machine stack from the local method stack


The area in JAVA memory where object instances are stored. Almost all object instances are allocated here
Shared by all threads
New generation, old age
jmap -heap pid;

Method area

Memory area shared by each thread
Store information, constants, static variables, real-time compiler compiled code and other data of classes loaded by virtual machine
Hotspot implements method area with permanent generation (so that garbage collector can manage method area), and reclaims and unloads constant pool
Method area will throw oom when it cannot meet the memory allocation requirements

Runtime Constant Pool

The runtime constant pool is a part of the method area. In addition to the constant pool of fields, methods and interfaces, the class stores literal and symbol references generated by the compiler. This part of content is loaded by the class and stored in the runtime constant pool of the method area.

Stringtable yes HashSet structure
Part of the method area, restricted by the method area, still OO

Java object creation process

[summary of JVM knowledge system framework]
->Static method static code block

  1. New instruction to determine whether there is a symbol reference in the constant pool. If there is one, it has been loaded
  2. Determine whether the class is loaded, parsed and initialized
  3. Allocating memory space in the Java heap for new objects
    1) pointer collision (tidy memory)
    [summary of JVM knowledge system framework]
    Steps: 1. Allocate memory 2. Move pointer, non atomic steps may have concurrent problems. Java virtual machine adopts CAS with failed retry to ensure atomicity of update operation
    2) free list (disordered memory)
    Storage heap memory free address
    Steps: 1. Allocate memory 2. Modify the non atomic steps of the free list address. There may be concurrency problems. Java virtual machine uses CAS with failed retry to ensure the atomicity of the update operation
  4. Initialize zero values for all allocated memory spaces
  5. Set related information of object header (GC generation age, hashcode of object, metadata information)
  6. implementMethod

    Java object memory layout

    Value of object property – > instance data
    Object header 64 bit machine stores 64 bit, 32-bit machine stores 32 bit, multiple of 8
    [summary of JVM knowledge system framework]

Access to Java objects

  1. Direct pointer access
    [summary of JVM knowledge system framework]
  2. Handle access
    [summary of JVM knowledge system framework]
  3. Access efficiency: high efficiency of direct pointer access (used by hotspot)
  4. Garbage collection: handle access efficiency is high, garbage collection only updates handle pool, while direct pointer access mode updates reference address

Garbage collection algorithm

  1. Reference counter
    [summary of JVM knowledge system framework]
    When an object instance is assigned to a variable, the variable count is set to 1. When any other variable is assigned to the reference of this object, the count is + 1 (a = B, then the reference object instance counter of B is + 1). When a reference of an object instance exceeds the life cycle (method execution is completed) or is set to a new value, then the instance reference counter of this object is – 1
    Unable to resolve circular reference
    Accessibility analysis
    GC root (referenced object in virtual machine stack, referenced object in local method stack, referenced object in static property of method area, referenced object in constant of method area)

  2. Mark – clear
    [summary of JVM knowledge system framework]
    Mark the objects to be recycled, and recycle them uniformly after marking
    1. The efficiency of the two processes is not high
    2. Space problem. After the mark is cleared, a large number of discontinuous memory fragments are generated. When the program needs to allocate large objects, it cannot find enough continuous memory and has to trigger a garbage collection action in advance

  3. Tag copy
    [summary of JVM knowledge system framework]
    The objects survived by memory block a are copied to memory block B (survivor to), and then memory block a (Eden + survivor from) is emptied,
    Only a few objects are moved, and more are to be recycled
    Eden:Survivor from:Survivor to=8:1:1
    98% of the objects “live and die”, 90% of the available memory capacity of the new generation (80% + 10%), 98% of the objects can be recycled in general. When less than 90% of the objects are recycled (when more than 10% of the objects survive), there is not enough space for survivor to, so it needs to rely on the old age for distribution guarantee

  4. Marking – finishing
    [summary of JVM knowledge system framework]
    Old generation is not suitable for replication algorithm
  5. Increase of replication operation 2. Additional 50% space waste 3. Often need additional space allocation guarantee 4. Possible 100% survival of objects in the elderly generation


  1. sign
  2. Grooming moves live objects to one end (top left), never to be groomed, and then directly cleans up memory outside the boundary

Serial collector

[summary of JVM knowledge system framework]
Single thread garbage collector. When the user thread reaches the safe point, it is tentative, and then the GC thread is serial. After the GC thread is recycled, the user thread can continue
Features: stop the world
Scenario: desktop application (short GC time)
For the new generation, client side

Parnew collector

Multithreaded version of the serial collector
[summary of JVM knowledge system framework]
For the new generation, it can only work with CMS collector and run in server mode
-XX: parallelgcthreads limit the number of garbage collector threads = the number of CPU cores (too many will cause context switching consumption)
Parallel: multiple garbage collection threads work in parallel, and the user thread is still waiting
Concurrent: user thread and garbage collector execute at the same time, user thread and garbage thread execute on different CPUs

Parallel scavenge collector

New generation collector, replication algorithm, parallel multithreaded collector
The collector that focuses on throughput priority (throughput = CPU running user code execution time / total CPU execution time, for example: 99% time to execute user thread, 1% time to recycle garbage, and then the throughput is 99%). High throughput can make efficient use of CPU time, complete the operation task of the program as soon as possible, which is suitable for background operation without too many interactive tasks
CMS focuses on shortening garbage collection pause time, programs suitable for interaction with users, and good response speed can improve user experience
-XX: maxgcpausemillis parameter GC pause time, too small parameter will cause frequent GC
-XX: gctimeratio parameter, 99% by default (user thread time accounts for 99% of total CPU time)

Serial old collector

It’s an old-fashioned version of the serial collector
Single thread collector for the elderly adopts “mark organize” algorithm

Parallel old collector

Is the older version of the parallel scavenge collector
Multithreaded collector for the elderly adopts the “mark organize” algorithm

CMS collector

Get the collector with the shortest recovery pause time as the target, and use the “mark clear” algorithm for the response oriented system of the Internet and B / s system
[summary of JVM knowledge system framework]


  1. Initial marking (not run with user thread, short time) – mark the objects that GC roots can directly relate to, which is very fast
  2. Concurrent tagging (running with user thread, taking a long time) – the concurrent tagging phase is to perform GC roottracing and find GC reference chain
  3. Retag (not run with user thread, short time) – in order to fix the record of tag changes caused by user thread during concurrent tagging
  4. Concurrent cleanup (running with user thread, time-consuming) – scan the entire memory area


  1. Very sensitive to CPU resources (long concurrent tagging phase time, consuming user thread CPU time)
  2. Unable to process floating garbage (new garbage generated by user thread during concurrent cleanup)
  3. Mark – clear space debris

G1 collector

Garbage collector for server application
[summary of JVM knowledge system framework]
Region – > remember set
Check the reference (the program writes to the reference type, and checks the reference reference reference type)

  1. Initial tag — tag the objects that GC roots can directly relate to
  2. Concurrency tagging — starting from GC root, it analyzes the reachability of the objects in the heap to find the surviving objects. This stage takes a long time, but it can be executed concurrently with the user program
  3. Final tags (remember set logs – > remember set) – fixed the part of tag records that caused changes in tags due to the continuous operation of user programs during concurrent tagging. The virtual machine recorded the changes of objects in this period in the thread remember set logs. In the final tagging stage, the data of remember set logs needs to be merged into the remember set logs
  4. Live data counting and evaluation – just scan the remember set
  5. Based on “mark organize” and the replication algorithm between regions
  6. It can predict pause and reduce pause time, but in addition to pursuing low pause, G1 can also establish a predictable pause time model
  7. G1 directly reclaims regions in the Java heap (the new generation and the old generation are no longer physically isolated, they are all part of regions)
  8. Predictable pause time model, G1 tracks the value of garbage accumulation in each region (the space obtained by recycling and the experience value of the time required for recycling), maintains a priority list in the background, and preferentially recycles the region with the largest value according to the allowable collection time each time

Heap memory allocation

Java heap distribution
[summary of JVM knowledge system framework]

Rules for object assignment:

  1. The objects are mainly distributed in the new generation Eden area (Eden area, from area survival objects are copied to the to area, Eden area, from area are recycled, then to area objects are copied to the from area, and then the next garbage collection is carried out)
  2. If the local thread allocation buffer is started, it will be allocated on TLAB by thread priority
  3. In a few cases, it may also be directly allocated to the old age (those from and to districts can’t be put directly into the old age)

Large object allocation

Large objects refer to Java objects that need a large amount of continuous memory space. The most typical large objects are long strings and arrays
-XX: the objects with the preset sizethreshold setting greater than this value are directly allocated to the elderly, avoiding large amount of memory replication between Eden area and 2 survival areas

Escape analysis and stack allocation

Escape analysis: analyze the dynamic scope of an object. When an object is defined in a method, it may be referenced by an external method, which is called method escape. It may even be accessed by external threads, such as assigning to class variables or instance variables accessed by other threads, which is called thread escape.
Stack allocation: the variables and objects in the method are directly allocated to the stack. After the method is executed, it is automatically destroyed without garbage collection intervention, so as to improve the system performance
-XX: + doescapeanalysis enable escape analysis (JDK1.8 is enabled by default)
-XX: – do escape analysis


  1. ps -ef | grep java
  2. JPS – M (startup parameter) – L (class name) – V (JVM parameter)
  3. Jstat-gc 27660 250 20 monitoring various running status information of virtual machine
    [summary of JVM knowledge system framework]
  4. Jinfo 27660 viewing and adjusting process virtual machine (not shown) parameter information
  5. Jmap generate heap dump snapshot – XX: + heapdumponoutofmemoryerror
    jmap -heap 9366;
    Jmap – histo 9366 | more; display the statistics of objects in the heap
    Jmap – dump: format = B, file = / users / mousycoder / desktop / a.bin 9366 generate dump file
    -Xmx20m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/Users/mousycoder/Desktop/
    Jhat / users / mousycoder / desktop / java_pid9783.hprof graph analysis heap
    select s.toString() from java.lang.String s where (s.value != null && s.value.length > 1000 )
  6. Jstack thread snapshot (a collection of method stacks executed by each thread in the virtual machine, mainly used to locate thread problems)
    Shutdownhook tasks performed before shutdown
    Jstack-l-f PID forced output

Thread state

  1. NEW
  3. Blocked a thread waiting for a monitor is in this state (entry set) passive blocking
  4. Waiting a thread that is waiting indefinitely for another thread to perform a special action is in this state (wait set) the block of active explicit request
  5. Timed \
  6. Terminated thread completes an execution

[summary of JVM knowledge system framework]


Visual monitoring and management tool based on JMX
Open JMX port
nohup java -Xms800m -Xmx800m -Djava.rmi.server.hostname= -Dcom.sun.management.jmx
remote.port=1111 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -jar hc-charging-
server.jar &

Internet development process
[summary of JVM knowledge system framework]
Jconsole memory analysis thinking process
[summary of JVM knowledge system framework]


Minor GC: when Eden area is full, trigger minor GC

  1. Calling system. Gc() suggests the virtual machine to perform full GC. You can call system. Gc() by using – XX: + disableexplicitgc as RMI
  2. In order to avoid full GC caused by the above reasons, when tuning, try to make the object be collected in the minor GC stage, make the object live for a long time in the new generation, and do not create too large objects and arrays
  3. Space allocation guarantee fails. Minor GC using replication algorithm needs memory space of the old age as guarantee. If handlepromotionfailure guarantee fails, full GC will be triggered
  4. Reduce – Xmx size and GC time (the larger the heap memory setting, the longer the full GC time, and the longer the pause time)
  5. Cluster deployment

Internet problems

  1. White list issues
    Solution: list. Contain – > set. Contain – > bloom filter
  2. deadlock
    Solution: jstack and new thread with name
  3. Heap memory leak
    The normal frequency of fullgc is 1-2 times a day
    Solution: jmap, heap dump on oom + jhat
  4. Out of heap memory leak
    Heap heap usage is very low, but there are oom and full GC
    Solution: btrace

Learning Secrets

  1. Knowledge system
  2. Look at the knowledge system map before the interview
  3. Stick to win