JVM memory distribution

  • Threads share data areas: method area -> class information, static variable heap -> array objects
  • Thread isolation Virtual machine stack -> Methods local method stack -> Native method library
  • Heap, program counter
  • JVM Running data

Program counter

Thread isolation, relatively small memory space, the line number of bytecode executed by the current thread thread is an independent execution unit, executed by the CPU, the only place without OOM, maintained by the virtual machine, so there is no OOM

The virtual machine stack

Java methods are executed

Method call is the process stack frame of the virtual machine stack: local variable table (variable), operand stack (store the result of a+ B), dynamic link (reference to the address of the object), method exit (return value) thread request stack depth greater than the virtual machine allowed StackOverflowError

Local method stack

The JVM standard is to separate the Java virtual machine stack from the local method stack

The heap

The area of Java memory where object instances are stored, where almost all object instances are allocated. All threads share the new generation and old generation Jmap-heap PID.

Methods area

Individual threads share area of memory storage has been a virtual machine class loading information, static variables, constants, and the real-time compilers compiled code data such as the Hotspot with permanent generation method area (let the garbage collector can management method), the recovery and discharge method of constant pool area will be thrown OOM, when he was unable to meet the demand of memory allocation

Run-time constant pool

The runtime constant pool is part of the method area. The Class contains fields, methods, and interfaces for storing compiler-generated literal and symbolic references that are loaded by the Class into the runtime constant pool of the method area.

StringTable is part of the method section of the HashSet structure

Java object creation process

-> static method static code block

  1. The new command checks if there are any symbol references in the constant pool, or if they have been loaded
  2. Determines whether the class is loaded, parsed, and initialized
  3. Allocates memory space in the Java heap for newborn objects

    1) Pointer collision (memory is tidy)



    Procedure: 1. Allocate memory 2. Move Pointers

    2) Free list (memory is messy)

    Storage heap memory free address

    Procedure: 1. Allocate memory. 2. Modify idle list ADDRESSES
  4. The allocated memory space is initialized to zero
  5. Set object header information (GC generation age, object HashCode, metadata information)
  6. Execution method

Java object memory layout

The value of the object property -> instance data object header 64 bit machine store 64 bit, 32 bit machine store 32 bit, multiples of 8

Access to Java objects

  1. Direct pointer access

  2. Handle access



    Contrast:
  3. Access Efficiency: Direct pointer access is efficient (hotspot uses this approach)
  4. Garbage collection: The handle access efficiency is high. Garbage collection only updates the handle pool, while direct pointer access updates the reference address

Object in the JVM process

New generation: After the object is born in Eden, when Eden is full, and after a Minor GC occurs, if the object is still alive and can be contained in S1, the replication algorithm is used to copy the object to S1 and clean it once (Eden + S0). When the age reaches 15, it will enter the old age), and then swap the positions of S0 and S1, and finally get an empty Cenozoic era (Eden + S0,s1 is always inactive and passive).

New generation, old age, permanent generation

-xx :NewRatio Sets the ratio of the size of the young generation to that of the old age. -xx :OldSize Sets the size of the old age. -xx :MaxPermSize Sets the size of the permanent generation (method area)

Garbage collection algorithm

Reference counter

When object instance is assigned to a variable, the variable count is set to 1, when any of the other variables are assigned to the object reference, count + 1 (a = b, b is a reference to the object instance counter + 1), when an object instance of a reference method (performed) or more than the life cycle is set to a new value, GC Root (objects referenced in the virtual machine stack, objects referenced in the local method stack, objects referenced by static properties of the method area, objects referenced by method area constants)

Mark-clear

Mark the objects that need to be recovered, and the unified recovery is insufficient after the completion of marking: 1. Efficiency problem, the two processes of mark removal are not efficient 2. When the program needs to allocate large objects, it cannot find enough contiguous memory and has to trigger a garbage collection action in advance

Mark-copy

The surviving objects of memory block A are copied to memory block B (Survivor to), and then the memory block A (Eden + Survivor from) is emptied. Only A few objects are moved. More objects are Eden:Survivor from:Survivor to=8:1:198% of objects “die soon”, the available memory capacity of the new generation is 90% (80%+10%), 98% of objects can be recycled is the general situation, When less than 90% of the objects are reclaimed (more than 10% of the objects survive), Survivor to space is not enough, and it needs to rely on the old generation for allocation guarantee

Mark-tidy

The old days were not good for copying algorithms

  1. Increased replication operations 2. Additional 50% space waste 3. Frequent need for additional space allocation guarantees 4. It is possible that objects in the old age are 100% alive

Steps:

  1. tag
  2. Uncluttering moves living objects to one end (top left), from uncluttered to tidy, and then directly cleans up memory beyond the boundary

Garbage collector

Serial collector (Next-generation collector)

Single-thread garbage collector, the user thread to the safe point first temporarily, then the GC thread single-thread serial, wait for the GC thread to complete, then the user thread to continue features: Stop the world

Scenario: Desktop application (short GC time) for the new generation and client

ParNew collector (New generation collector)

Multithreaded version of Serial collector-xx :ParallelGCThreads Limit the number of garbage collector threads = 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 concurrent in a waiting state: the user thread executes at the same time as the garbage collector, and the user thread and garbage thread execute on different cpus

Parallel Avenge collector

New generation collectors, replication algorithms, parallel multithreading collectors focus on throughput-first collectors (throughput = CPU run user code execution time/total CPU execution time, e.g. : 99% of the time to execute the user thread, 1% of the time to recycle garbage, at this time the throughput is 99%) High throughput can efficiently use the CPU time, as soon as possible to complete the operation task of the program, suitable for the background operation without too much interaction task CMS focus on reducing the garbage collection pause time, suitable for the user interaction program, -xx :MaxGCPauseMillis parameter GC pause time, too small parameter will be frequent gC-xx :GCTimeRatio parameter, default 99% (user thread time is 99% of the total CPU time)

Serial Old collector

The Serial collector is an older version of the single-threaded older collector that uses a mark-tidy algorithm

Parallel Old collector Parallel Old collector

The Parallel Exploiter is an older version of the Parallel Exploiter multithreaded collector, using the mark-collation algorithm

CMS collector (old + persistent collector)

To obtain the shortest recovery pause time for the target collector, using the “mark-clear” algorithm, for the Internet, B/S system to attach importance to the response of the system trigger threshold: old or permanent generation reached 92%-XX:+CMSClassUnloadingEnabled so that the CMS can reclaim the Perm area

Steps:

  1. Initial tag (not run with the user thread, short time) – tag objects that GC Roots can be directly associated with quickly
  2. Concurrent marking (running with the user thread and taking a long time) – The concurrent marking stage is GC RootsTracing, looking for GC reference chains
  3. Relabeling (run without user thread and take a short time) – to correct tag records that change during concurrent tagging due to user thread
  4. Concurrent cleanup (running with the user thread, which takes a long time) – scans the entire memory region

Disadvantages:

  1. Very SENSITIVE to CPU resources (concurrent marking phases take a long time, consuming user thread CPU time)
  2. Unable to handle floating garbage (new garbage generated by user threads while the program is concurrently cleaning)
  3. Mark – Clear creates space debris

G1 collector

Garbage collector for server-side applicationsRegion->Remembered Set

Check the type of the Reference Reference.

  1. Initial tag – marks objects to which GC Roots can be directly associated
  2. Concurrent marking – Reachability analysis of objects in the heap, starting with GC Root, to find viable objects, is a longer phase, but can be performed concurrently with user programs
  3. Remembered Set Logs->Remembered Set — Fixes the part of the mark record that changed during the concurrent mark because the user’s program continued to operate, The virtual machine records the changes made during this time in the Remembered Set Logs thread. The final marking phase needs to merge the data from the Remembered Set Logs into the Remembered Set thread
  4. Live Data Counting and Evacuation — just scan Remembered Set

Advantage:

  1. Based on “mark-collation” between the main Region and the replication algorithm
  2. Predictable pauses and lower pause times, but G1 models predictable pauses in addition to pursuing low pauses
  3. G1 directly reclaims regions in the Java heap (new generation and old generation are no longer physically isolated, they are all part of regions)
  4. With a predictable pause time model, G1 tracks the value of garbage accumulation in Regions (the amount of space available for collection and the empirical value of time required for collection), maintains a priority list in the background, and prioritizes the Region with the highest value for collection based on the allowed collection time each time

Heap allocation

Java heap distribution diagram

Rules for object allocation:

  1. Objects are mainly distributed in Eden zone of the new generation (surviving objects are copied To To zone, Eden zone, From zone are recycled, and then exchanged between To zone and From zone, and then garbage collection is carried out next time)
  2. If a local thread allocation buffer is enabled, it is allocated on TLAB thread first
  3. In rare cases, it is possible To assign To the old age directly.

Large object allocation

Large object “refers to a large number of continuous memory space Java objects, that is the most typical large object is a long string and array – XX: PretenureSizeThreshold set is greater than the value distribution of object directly in old age, Avoid massive memory replication between Eden region and two Survivior regions

Escape analysis and on-stack allocation

Escape analysis: Analyzes the dynamic scope of an object that may be referenced by external methods after it is defined in a method, called method escape. It may even be accessed by an external thread, such as an assignment to a class variable or an instance variable accessed in another thread, called thread escape. Stack allocation: -xx :+DoEscapeAnalysis Enables escape analysis (enabled by default in JDK1.8). -xx: -doEscapeAnalysis disables escape analysis

Virtual machine tuning command

  1. ps -ef | grep java
  2. JPS -m (startup parameters) -l (class name) -v (JVM parameters)
  3. Jstat -gc 27660 250 20 Monitor vm running status information

  4. Jinfo 27660 Views and adjusts the parameters of a process VM (not specified)
  5. Jmap heap dump snapshot – XX: + HeapDumpOnOutOfMemoryError jmap – 9366 heap; jmap -histo 9366 | more; Displays statistical jmap – object heap dump: the format = b, the file = / Users/mousycoder/Desktop/a. in 9366 to generate the dump file – Xmx20m – XX: + HeapDumpOnOutOfMemoryError – XX: HeapDumpPath = / Users/mousycoder/Desktop/the jhat/Users/mousycoder/Desktop/java_pid9783 hprof Heap the select graphics analysis s.toString() from java.lang.String s where (s.value ! = null && s.value.length > 1000 )
  6. Jstack thread snapshot (a stack of methods being executed by each thread in the virtual machine, mainly used to locate thread problems) shutdownHook Task executed before closing jstack -l -f PID force output

Thread state

  1. NEW
  2. RUNNABLE
  3. BLOCKED A thread waiting for a monitor is passively BLOCKED in this state (Entry Set)
  4. WAITING A thread that is WAITING indefinitely for another thread to perform a particular action is actively blocking in this Wait Set
  5. TIMED_WAITING The state in which a thread is waiting for another thread to perform an action
  6. The TERMINATED thread completes an excution

JConsole

Visual monitoring, management tool based on JMX open port nohup Java JMX Xms800m – Xmx800m – Djava. Rmi. Server hostname = 192.168.1.250 -Dcom.sun.management.jmxremote.port=1111 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -jar hc-charging-server.jar &

Internet Development ProcessJconsole memory analysis thought process

FullGC

Minor GCFullGC: Triggers Minor GCFullGC when Eden is full:

  1. Call System.gc() It is recommended that the VM perform Full GC. You can disable RMI from calling System.gc() by using -xx :+DisableExplicitGC.
  2. When the Full GC is performed, OutOfMemoryError is raised. In order to avoid Full GC, try to make the object be collected during the Minor GC. Make objects live longer in the new generation and don’t create objects and arrays that are too large
  3. A Minor GC with a copy algorithm requires older memory space as a guarantee, and if a HandlePromotionFailure guarantee fails, the Full GC recommendation is triggered:
  4. Reduce the -xmx size to shorten the 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. Contain ->set. Contain -> Block filter
  2. Deadlock solutions: jStack and New Threads with names
  3. Heap memory leak FullGC occurs once or twice a day. Solution: Jmap, heap dump on oom + jhat
  4. Outside the heap memory leak outside the heap memory (Unsafe allocateMemory, ByteBuffer. AllocateDirect, Runtime.getruntime ().freememory () allows you to view the size of the heap not occupied.) Heap heap usage is very low, but there are OOM and Full GC solutions: Btrace traces the DirectByteBuffer constructor, which is analyzed using Google Perftools on the non-Java side
  5. Unequal data solution: MQ

This article is published by OpenWrite!