Java related built-in commands

Terminal command tool

  • Javap: Can be used to check if a part of a compiled file is included
  • Jstat: Heap memory size and GC analysis, jstat-csDN
  • JCMD: Heap analysis, out-of-heap memory analysis, jcmd-cnblog
  • jinfo: To viewModify theJava process information, such as the system parameters, the JVM parameter. Jinfo – flag [+ | -] flag_name pid, such as: jinfo – opts
  • Jmap: View the information in the heap, for example, jmap-histo PID
  • JHSDB (>=java9) : Analyze the core information related to the crashed JVM
  • Jhat: HTML page to display heap memory analysis results
  • JPS: View the Java process
  • Jstack: Analyzes process stack data

JVM parameter commands

-xmx // -XMX5g, maximum memory -xms // Minimum memory -xmn // New generation memory size, the default recommended ratio of new to old: 3/8, Equivalent to MaxNewSize= NewSize -xx :MaxNewSize= 715653120-XX :NewSize= 89128960-xx :InitialHeapSize=268435456 -xx :MaxHeapSize=2147483648 -xx :OldSize=179306496 -xss // Thread stack size. -xx :NewRatio=4. // New generation/old generation = 1/4-XX :SurvivorRatio=4 // Eden /survivor= 1/4-XX :MaxTenuringThreshold=0 / / the old generation to experience the number of gc - XX: CICompilerCount = 3 - XX: MinHeapDeltaBytes = 524288 - XX: + UseCompressedClassPointers -xx :+UseCompressedOops recycle parameter -xx :+UseParallelGC Parallel old-xx :ParallelGCThreads= 20-XX :+UseParallelOldGC -xx :+UseParNew Parallel collector -xx :+UseConcMarkSweepGC -xx :+UseParNewGC // For the new generation, use + or - to modify related JVM parameters while runningCopy the code

The memory model

Since Java 1.8, the method area has been moved to the metadata area, which is off-heap memory.

Pile model

  • New generation: Eden, Survivor1, survivor2
  • The old generation
  • Metadata area: contains method area, storage class information, constants, static variables, constant pool

The garbage collection

Recycling principle

  • Technical citation method
  • Root search – Reachability analysis
Objects that can be used as GC Root in Java are 1. Objects referenced in the virtual machine stack (local variable table) 2. Object referenced by static attribute in method area 3. Object referenced by constant in method area 4. Objects referenced in the Native method stack (Native objects)Copy the code

Reclamation trigger mechanism

  • Minor GC is triggered when a new generation is full or periodically
  • Full GC is triggered when the old generation is Full

Recovery algorithm

  • Mark replication: Eden zone to S zone
  • Mark cleared: s-zone to old age
  • Mark finishing: old age

Collector type

Reference: jvm_cnblogs, gc_csdn

  • Serial collector: SerOld, SerNew
  • Parallel collector: prioritized throughput, such as ParallelGC, ParNew
  • Concurrent collector: priority response time, such as CMS
The new generation of single-threaded collector, marking and cleaning are single-threaded, the advantage is simple and efficient. A single-threaded collector, an older version of the Serial collector. 3. ParNew collector (stop-copy algorithm) The new generation collector, which can be considered a multi-threaded version of Serial collector, performs better than Serial on multi-core CPUS. A new generation of Parallel collectors, the pursuit of high throughput and efficient CPU utilization. Throughput is typically 99% and throughput = user thread time/(user thread time +GC thread time). Suitable for scenarios with low requirements on interaction, such as background applications. The Parallel Collector, a Parallel collector, is insane. The Parallel Collector is insane. The CMS collector is a Concurrent Mark Sweep collector. Most concurrent MS, no collation, using freelist to record the recovered address; The default new generation is ParNew and STW tag is copied. 7. Compared with CMS, G1 provides the function of memory collation to reduce memory fragmentation. Focus on high throughput and controllable GC recovery time. The G1 offers two GC modes, Young GC and Mixed GC, both of which are completely Stop The World 8. ZGC JDK11 is available for Java applications with large memory and low latency. Support 8 m ~ 4 t.Copy the code

Reference: CNblog-CMS, Meituan-G1, Meituan-ZGC,

Performance optimization

Reference program New horizon – JVM performance tuning

Record performance analysis data

- XX: + HeapDumpOnOutOfMemoryError / / mistake archived heap memory problemsCopy the code

Performance analysis tool

  • jmap + hprof
  • jmap + jhat
  • VisualVM: Idea has plug-ins, runtime analysis