(Note: This part is supposed to be a summary, but there is a more complete, more essential summary on Github, the whole series is missing a detailed explanation of the various garbage collectors and the tricolor algorithm, I will fill in later. Original address: github.com/bjmashibing…)

Basics of GC

1. What is garbage

To apply for memory in C, run the malloc free command

C + + : new delete

C /C++ manually reclaims memory

Java: new?

Automatic memory recovery, programming is simple, the system is not prone to error, manual release of memory, prone to two types of problems:

  1. Forget about recycling
  2. Many recycling

One or more objects without any reference (circular reference)

2. How to locate garbage

  1. Reference counting (ReferenceCount)
  2. RootSearching algorithm

3. Common garbage collection algorithms

  1. Mark Sweep – Low fragmentation efficiency due to location discontinuities (two sweep)
  2. Copying algorithms – No debris, waste of space
  3. Mark Compact – No fragmentation, low efficiency (double sweep, pointer adjustment)

4.JVM memory generational model (for generational garbage collection algorithm)

  1. Part of the model used by garbage collectors

    All GCS except Epsilon ZGC Shenandoah use the logical generational model

    G1 is a logical generation, not a physical generation

    In addition not only logical generation, but also physical generation

  2. Cenozoic + Old + permanent (1.7) Perm Generation/ Metaspace (1.8

    1. Permanent generation metadata – Class
    2. Permanent generation must specify size limit, metadata can be set or not set, no upper limit (limited to physical memory)
    3. String constants 1.7 – permanent generation, 1.8 – heap
    4. MethodArea Logical concept – permanent generation, metadata
  3. Cenozoic = Eden + 2 Suvivor

    1. After YGC collection, most of the objects will be collected and enter S0 alive
    2. YGC again, the living object Eden + s0 -> s1
    3. Again YGC, Eden + s1 -> s0
    4. Adequate age -> old age (15 CMS 6)
    5. Block S won’t fit -> Old age
  4. The old s

    1. diehard
    2. The old age is Full FGC Full GC
  5. GC Tuning (Generation)

    1. Minimize FGC
    2. MinorGC = YGC
    3. MajorGC = FGC
  6. Object allocation process diagram

  7. Dynamic age: (not important) www.jianshu.com/p/989d3b06a…

  8. Distribution during the period of guarantee: (not important) YGC survivor area space is not enough space guarantee directly into old age Reference: cloud.tencent.com/developer/a…

5. Common garbage collectors

  1. In order to cooperate with CMS, PN was born. CMS was introduced in the late 1.4 version. CMS is a landmark GC, which starts the process of concurrent collection, but CMS has many faults. So any current JDK version defaults to CMS concurrent garbage collection because STW is intolerable
  2. Serial Young generation Serial collection
  3. PS young generation parallel recycling
  4. ParNew Young generation with CMS parallel recycling
  5. SerialOld
  6. ParallelOld
  7. In the old days, when the garbage collection and application were running simultaneously, the STW time was reduced by 200ms, there were many problems with CMS, so now there is no default CMS version, we have to manually specify CMS as MarkSweep, which is bound to have fragmentation problems. When the fragmentation reaches a point where the CMS’s old age allocation objects are not available, use SerialOld for old age reclamation. Imagine this: PS + PO -> add memory for garbage collector -> PN + CMS + SerialOld (a few hours – a few days STW) dozens of G memory, single thread collection -> G1 + FGC dozens of G -> T memory server ZGC algorithm: Incremental Update
  8. G1(10ms) algorithm: three-color marker + SATB
  9. ZGC (1ms) PK C++ : ColoredPointers + LoadBarrier
  10. Shenandoah’s algorithm: ColoredPointers + WriteBarrier
  11. Eplison
  12. PS and PN difference read on: forced docs.oracle.com/en/java/jav…
  13. The relationship between garbage collector and memory size
    1. Serial dozens
    2. Hundreds of gigabytes of PS. – A couple gigabytes
    3. CMS – 20G
    4. G1 – hundreds of grams
    5. Zgc-4t-16t (JDK13)

1.8 Default garbage collection: PS + ParallelOld

Common garbage collector combination parameters :(1.8)

  • -XX:+UseSerialGC = Serial New (DefNew) + Serial Old

    • Small program. By default this is not an option and HotSpot automatically selects the collector based on calculation and configuration and JDK version
  • -XX:+UseParNewGC = ParNew + SerialOld

    • This combination is rarely used (deprecated in some versions)
    • Stackoverflow.com/questions/3…
  • -XX:+UseConc(urrent)MarkSweepGC = ParNew + CMS + Serial Old

  • Be insane. -XX:+ exploiture = exploiture + exploiture (1.8 default) 【PS + SerialOld】

  • -XX:+UseParallelOldGC = Parallel Scavenge + Parallel Old

  • -XX:+UseG1GC = G1

  • Linux does not find a way to view the default GC, while Windows will print UseParallelGC

    • java +XX:+PrintCommandLineFlags -version
    • It is identified by GC logs
  • What is the default garbage collector for Linux 1.8?

    • 1.8.0_181 Copy MarkCompact by default
    • 1.8.0_222 defaults to PS + PO

The first step in JVM tuning is to understand common JVM command-line arguments

  • The JVM command-line parameters reference: docs.oracle.com/javase/8/do…

  • HotSpot parameter Classification

    Standard: – Initially, all hotspots are supported

    Non-standard: Starting with -x, certain versions of HotSpot support certain commands

    Unstable: starts with -xx and may be cancelled in the next version

    java -version

    java -X

    Test procedure:

    import java.util.List;
    import java.util.LinkedList;
    
    public class HelloGC {
      public static void main(String[] args) {
        System.out.println("HelloGC!");
        List list = new LinkedList();
        for(;;) { byte[] b = new byte[1024*1024]; list.add(b); }}}Copy the code

    1. Memory leaks out of memory
    2. java -XX:+PrintCommandLineFlags HelloGC
    3. java -Xmn10M -Xms40M -Xmx60M -XX:+PrintCommandLineFlags -XX:+PrintGC HelloGC PrintGCDetails PrintGCTimeStamps PrintGCCauses
    4. java -XX:+UseConcMarkSweepGC -XX:+PrintCommandLineFlags HelloGC
    5. Java -xx :+PrintFlagsInitial Default value
    6. Java -xx :+PrintFlagsFinal Final parameter value
    7. Java – XX: + PrintFlagsFinal | grep XXX to find the corresponding parameters
    8. java -XX:+PrintFlagsFinal -version |grep GC

Description of PS GC logs

The logging format is different for each garbage collector!

PS Log Format

Heap dump section:

Eden space 5632 k, 94% informs [x00000000fff00000 x00000000ff980000 0, 0 x00000000ffeb3e28, 0) at the back of the memory address is, the starting address, using the address space is over, the overall end address spaceCopy the code

Total = Eden + 1 Survivor

Basic concepts before tuning:

  1. Throughput: User code time/(User code execution time + Garbage collection time)
  2. Response time: The shorter the STW, the better the response time

So tuning, first of all, what do you want? Throughput or response time? Or how much throughput is required to meet certain response times…

Question:

Scientific calculation, throughput. Data Mining, ThrPUT. Throughput priority general :(PS + PO)

Response time: Site GUI API (1.8 G1)

What is tuning?

  1. JVM planning and pre-tuning based on requirements
  2. Optimize the JVM runtime environment (slow, slow)
  3. Solve various problems during JVM running (OOM)

Tuning starts with planning

  • Tuning starts with a business scenario. Tuning without a business scenario is rogue

  • No monitoring (pressure test, results can be seen), no tuning

  • Steps:

    1. Familiarity with business scenarios (there is no best garbage collector, only the most appropriate garbage collector)
      1. Response time, pause time [CMS G1 ZGC] (need to respond to user)
      2. Throughput = User time /(User time + GC time) [PS]
    2. Select the collector combination
    3. Calculate memory requirements (experience 1.5GB 16GB)
    4. Select the CPU (the higher the better)
    5. Set age and upgrade age
    6. Setting log Parameters
      1. -Xloggc:/opt/xxx/logs/xxx-xxx-gc-%t.log -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=20M -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCCause
      2. Or generate a log file every day
    7. Observing logs
  • Case 1: Vertical e-commerce, up to million orders per day, what server configuration is required to process the order system?

    This is amateurish, as many different server configurations can support it (1.5g, 16GB)

    1 hour 360,000 concentrated time, 100 orders/second, (find the peak time within an hour, 1000 orders/second)

    Experience value,

    Have to calculate: How much memory does an order need to be generated? 512K x 1000 500 MB memory

    A professional question: the response time is required to be 100ms

    Pressure test!

  • Case 2: How should 12306 be supported when encountering large-scale ticket grabbing during Spring Festival?

    12306 should be China’s largest number of concurrent seckill website:

    It claims to have the highest concurrent volume of 100W

    CDN -> LVS -> NGINX -> Service System -> each machine 1W concurrent (10K problem) 100 machines

    General e-commerce order -> order -> Order system (IO) reduce inventory -> wait for user payment

    A possible model of 12306: order -> destocking and order (Redis Kafka) simultaneously asynchronously -> wait for payment

    Destocking also ends up putting pressure on a server

    You can do distributed local inventory + separate server for inventory balancing

    Treatment of large flow: divide and conquer

  • How to find out how much memory a transaction consumes?

    1. Get a machine, see how much TPS it can take? Did you achieve your goals? Expand or tune it to reach

    2. Use a pressure test to determine

Optimizing the environment

  1. There is a 500,000pv data type website (extract documents from disk to memory). The original server is 32 bit, 1.5G heap, user feedback site is slow, so the company decided to upgrade, the new server is 64 bit, 16G heap memory, the result of user feedback card is very serious, but the efficiency is lower than before
    1. Why is the original site slow? Lots of users browsing data, lots of data loading into memory, insufficient memory, frequent GC, long STW, slow response time
    2. Why is it more slow? The larger the memory, the longer the FGC time
    3. Do how? PS -> PN + CMS or G1
  2. The system CPU is often 100%, how to tune? (interview frequency) CPU100% then there must be a thread occupying the system resources,
    1. Find out which process has the highest CPU (top)
    2. Which thread in the process has the highest CPU (top-HP)
    3. Export the thread’s stack (jStack)
    4. Find which method (stack frame) consumes time (jStack)
    5. Worker threads of high | gc thread high proportion
  3. System memory is soaring. How to find the problem? (Interview frequency)
    1. Exporting heap memory (JMAP)
    2. Analysis (Jhat JVisualvm mat Jprofiler…)
  4. How to monitor the JVM
    1. jstat jvisualvm jprofiler arthas top…

Resolve problems with JVM running

A case study to understand common tools

  1. Test code:

    package com.mashibing.jvm.gc; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; /** * read credit data from database, apply model, */ public class T15_FullGC_Problem01 {private static class CardInfo {BigDecimal price = new BigDecimal (0.0); String name ="Zhang";
            int age = 5;
            Date birthdate = new Date();
    
            public void m() {}
        }
    
        private static ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(50,
                new ThreadPoolExecutor.DiscardOldestPolicy());
    
        public static void main(String[] args) throws Exception {
            executor.setMaximumPoolSize(50);
    
            for(;;) { modelFit(); Thread.sleep(100); } } private static voidmodelFit(){
            List<CardInfo> taskList = getAllCardInfo();
            taskList.forEach(info -> {
                // do something
                executor.scheduleWithFixedDelay(() -> {
                    //do sth with info
                    info.m();
    
                }, 2, 3, TimeUnit.SECONDS);
            });
        }
    
        private static List<CardInfo> getAllCardInfo(){
            List<CardInfo> taskList = new ArrayList<>();
    
            for (int i = 0; i < 100; i++) {
                CardInfo ci = new CardInfo();
                taskList.add(ci);
            }
    
            returntaskList; }}Copy the code

  2. java -Xms200M -Xmx200M -XX:+PrintGC com.mashibing.jvm.gc.T15_FullGC_Problem01

  3. Operations team is usually the first to receive the alarm message (CPU Memory)

  4. A problem is observed with the top command: the memory is increasing and the CPU usage is high

  5. Top-hp Watches the threads in the process and determines which thread has the highest CPU and memory ratio

  6. JPS locates the specific Java process jStack locates the thread status, focusing on: WAITING BLOCKED eg. WAITING on < 0x0000000088CA3310 > (a Java.lang.object) You must find out which thread holds the lock. RUNNABLE job: 1: write a deadlock program and watch it with jStack. 2: write a program where one thread holds the lock and the other threads wait

  7. Why does the Ali specification require that thread names (especially thread pools) be given meaningful names? (Custom ThreadFactory)

  8. jinfo pid

  9. Jstat-gc Observe GC dynamically/read GC log find frequent GC/Arthas observe/jconsole/jvisualVM/ Jprofiler (best use) Jstat-GC 4655 500: If the interviewer asks you how do you identify an OOM question? If you answered with a graphical interface (error) 1: What does a system already live use without a graphical interface? (CMdline Arthas) 2: Where exactly is the graphical interface used? The test! Monitor the test! (Pressure observation)

  10. Jmap histo – 4655 | head – 20, find how many objects

  11. Jmap – dump: the format = b, the file = XXX pid:

    If HeapDump is set to OOM, the HeapDump file will be automatically generated. If HeapDump is set to OOM, the HeapDump file will be automatically generated. If HeapDump is set to OOM, the HeapDump file will be automatically generated. Online location (generally smaller companies do not use)

  12. java -Xms20M -Xmx20M -XX:+UseParallelGC -XX:+HeapDumpOnOutOfMemoryError com.mashibing.jvm.gc.T15_FullGC_Problem01

  13. Use the MAT/the jhat to analyze the dump file/jvisualvm www.cnblogs.com/baihuitests… The jhat – J – mx512M XXX dump http://192.168.17.11:7000 at the end of the day: to find the corresponding link can use OQL find specific problems

  14. Find the problem with the code

Jconsole remote connection

  1. Program start add parameter:

    Java - Djava. Rmi. Server hostname = 192.168.17.11 - Dcom. Sun. Management jmxremote - Dcom. Sun. Management jmxremote. Port = 11111 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false XXXCopy the code

  2. If you encounter an error indicating Local host name unknown: XXX, modify the /etc/hosts file and add XXX to it

    192.168.17.11 Basic localhost localhost. Localdomain localhost4 localhost4. Localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6Copy the code

  3. Turn off the Linux firewall (should open the corresponding port in actual combat)

    service iptables stop
    chkconfig iptables off # Permanent shutdownCopy the code

  4. Open the JConsole remote connection to 192.168.17.11:11111 on Windows

Jvisualvm remote connection

www.cnblogs.com/liugh/p/762… (Simple)

Jprofiler (charge)

Arthas Online troubleshooting tool

  • Why online screening? In production, we often encounter some problems that are difficult to troubleshoot, such as thread safety problems, and it is difficult to find the cause of the problem with the simplest threaddump or heapdump. In order to troubleshoot these problems, we sometimes add some temporary logs, such as printing the input parameters in some key functions, and then repackage the release. If the log is still not found, then continue to add the log, and repackage the release. For companies with complex online process and strict audit, the process from code change to online needs layers of circulation, which will greatly affect the progress of problem investigation.
  • JVM Observe JVM information
  • Thread Indicates the thread fault
  • Dashboard Monitors the system
  • Heapdump + Jhat analysis
  • Jad: A problem with decompiling dynamic proxy generation classes
  • Redefine hot replacement at present some restrictive conditions: only change method (method has run complete), can’t change the method name, can’t change attributes of m () () – > mm
  • sc – search class
  • watch – watch method
  • Features not included: JMAP

Basic concepts of GC algorithm

  • When you do YGC, you need to scan the whole OLD section, which is very inefficient, so the JVM designed the CardTable. If there is an object in the OLD section CardTable that points to the Y section, it will be set to Dirty. Next time, you only need to scan the Dirty cards in the structure, Card Table is implemented using BitMap

CMS

The problem of the CMS

  1. Memory Fragmentation

    + UseCMSCompactAtFullCollection – – XX: XX: CMSFullGCsBeforeCompaction defaults to 0 refers to how many times before FGC is compressed

  2. Floating Garbage

    Concurrent Mode Failure: if the concurrent collector is unable to finish reclaiming the unreachable objects before the tenured generation fills up, or if an allocation cannot be satisfiedwith the available free space blocks in the tenured generation, then theapplication is paused and the collection is completed with all the applicationthreads stopped

    Solution: Lower the threshold that triggers the CMS

    PromotionFailed

    The solution is similar to keeping the old age with plenty of space

    – XX: CMSInitiatingOccupancyFraction 92% can reduce the value, keep the CMS old s enough space

CMS Log Analysis

Run the following command: Java -xms20m -xmx20m -xx :+PrintGCDetails -xx :+UseConcMarkSweepGC com.mashibing.jvm.gc.t15_fullgc_problem01

[GC (Allocation Failure) [ParNew: 6144K->640K(6144K), 0.0265885 secs] 6585K->2770K(19840K), 0.0268035 secs] [Times: User sys = = 0.02 0.00, real = 0.02 secs]

ParNew: young generation collector

6144->640: comparison before and after collection

(6144) : the entire young generation capacity

6585 -> 2770: the condition of the entire heap

(19840) : total heap size

[x] [x] [x] [x] [x] [x] [x] [x] [x] [x] [x] [x] [x] User = 0.01sys =0.00, real=0.00 secs] [cms-concurrent-mark-start] [cms-concurrent-mark: 0.018/0.018 secs] [Times: User = 0.01sys =0.00, real= 0.02secs] // The time is not significant because it is concurrent execution [cms-concurrent-preclean -start] [cms-concurrent-preclean: 0.000/0.000 secs] [Times: User =0.00 sys=0.00, real=0.00 secs] // Make Card Dirty [GC (CMS Final Remark) [YG occupancy: 1597 K (6144 K)][Rescan (parallel), 0.0008396 secs][class processing, 0.0000138 secs] [scrub symbol table, 0.0006169 secs][scrub string table, 0.0004903 secs][1 CMS-remark: 8511K(13690k)] 10108K(13690k), 0.003956secs] [Times: User =0.00 sys=0.00, real=0.00 secs] // OCCUPANCY: YG //[Rescan (parallel) : Live objects under the STW tag / / weak refs processing: a weak reference processing / / class unloading: unload in less than a class / / the scrub symbol (string) the table: //cleaning up symbol and string tableswhichhold class-level metadata and //internalized string respectively //CMS-remark: 8511K(13696K): Old age occupation and capacity after phase //10108K(19840K): Heap occupation and capacity after phase [cms-concurrent-swear-start] [CMs-concurrent-sweep: 0.005/0.005 secs] [Times: User =0.00 sys=0.00, real= 0.01secs] [cms-concurrent-reset-start] [cms-concurrent-reset: 0.000/0.000 secs] [Times: user=0.00 sys=0.00, real=0.00 secsCopy the code

G1

  1. Forced the www.oracle.com/technical-r…

Description of G1 logs

[GC pause (G1 Evacuation Pause) (young) (initial-mark), 0.0015790 secs] //young -> Young generation Evacuation-> replication of new objects //initial-mark hybrid collection phase, here is YGC hybrid old age collection [Parallel Time: 1.5ms, GC Workers: 1] // A GC thread [GC Worker Start (ms): 92635.7] [Ext Root Scanning (ms): 1.1] [Update RS (ms): 0.0] [Processed Buffers: 1] [Scan RS (ms): 0.0] [Code Root Scanning (ms): 0.0] [Object Copy (ms): 0.1] [Termination (MS): 0.0] [Termination (MS) Attempts: 1] [GC Worker Other (MS): 0.0] [GC Worker Total (MS): 1.2] [GC Worker End (ms): 92636.9] [Code Root Fixup: 0.0ms] [Code Root Purge: 0.0ms] [Clear CT: 0.0ms] [Other: 0.1ms] [Choose CSet: 0.0ms] [Ref Proc: 0.0ms] [Ref Enq: 0.0ms] [Redirty Cards: 0.0ms] [Humongous Register: 0.0ms] [Humongous Reclaim: 0.0ms] [Free CSet: 0.0ms] [Eden: 0.0b (1024.0k)-> 0.0b (1024.0k) Survivors: 0.0b -> 0.0b Heap: 18.8 M (20.0 M) - > 18.8 M (20.0 M)] [Times: The user sys = = 0.00 0.00, Real =0.00 secs] [GC concurrent-root-region-scan-start] [GC concurrent-root-region-scan-end, 0.0000078 secs] [GC concurrent-mark-start] // Unable to evacuation, FGC [Full GC (Allocation Failure) 18M->18M(20M), 0.0719656SECs] [Eden: 0.0b (1024.0k)-> 0.0b (1024.0k) Survivors: 0.0 - > 0.0 B B Heap: 18.8 M (20.0 M) - > 18.8 M (20.0 M)], [Metaspace: 38 76 k - > 3876 k (1056768 k)] [Times: User sys = = 0.07 0.00, real = 0.07 secs]Copy the code

Case summary

There are a variety of reasons for OOM generation, some programs may not generate OOM generation, but FGC(CPU surge, but memory reclamation is particularly low) (example above)

  1. Hardware upgrade system instead of the problem (see above)

  2. (see above) Adding objects to the List (too LOW)

  3. Smile JIRA problem actual system restart constantly solve the problem add memory + replace the garbage collector G1 where is the real problem? I don’t know

  4. Tomcat http-header-size is too large (Hector)

  5. Java -xx :MaxMetaspaceSize= 9m-xx :+PrintGCDetails

    "C: \ Program Files \ Java \ jdk1.8.0 _181 \ bin \ Java exe" -XX:MaxMetaspaceSize=9M -XX:+PrintGCDetails "-javaAgent :C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.1\lib\idea_rt.jar= 49316:c :\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.1\bin" -Dfile.encoding=UTF-8 -classpath "C: \ Program Files \ Java \ jdk1.8.0 _181 \ jre \ lib \ charsets jar; C: \ Program Files \ Java \ jdk1.8.0 _181 \ jre \ lib \ deploy the jar. C: \ Program Files \ Java \ jdk1.8.0 _181 \ jre \ lib \ ext \ access - bridge - 64. The jar. C: \ Program Files \ Java \ jdk1.8.0 _181 \ jre \ lib \ ext \ cldrdata jar; C: \ Program Files \ Java \ jdk1.8.0 _181 \ jre \ lib \ ext \ DNSNS jar; C: \ Program Files \ Java \ jdk1.8.0 _181 \ jre \ lib \ ext \ jaccess jar; C: \ Program Files \ Java \ jdk1.8.0 _181 \ jre \ lib \ ext \ JFXRT jar; C: \ Program Files \ Java \ jdk1.8.0 _181 \ jre \ lib \ ext \ localedata jar; C: \ Program Files \ Java \ jdk1.8.0 _181 \ jre \ lib \ ext \ nashorn jar; C: \ Program Files \ Java \ jdk1.8.0 _181 \ jre \ lib \ ext \ sunec jar; C: \ Program Files \ Java \ jdk1.8.0 _181 \ jre \ lib \ ext \ sunjce_provider jar; C: \ Program Files \ Java \ jdk1.8.0 _181 \ jre \ lib \ ext \ sunmscapi jar; C: \ Program Files \ Java \ jdk1.8.0 _181 \ jre \ lib \ ext \ sunpkcs11 jar; C: \ Program Files \ Java \ jdk1.8.0 _181 \ jre \ lib \ ext \ zipfs jar; C: \ Program Files \ Java \ jdk1.8.0 _181 \ jre \ lib \ javaws jar; C: \ Program Files \ Java \ jdk1.8.0 _181 \ jre \ lib \ jce jar; C: \ Program Files \ Java \ jdk1.8.0 _181 \ jre \ lib \ JFR jar; C: \ Program Files \ Java \ jdk1.8.0 _181 \ jre \ lib \ JFXSWT jar; C: \ Program Files \ Java \ jdk1.8.0 _181 \ jre \ lib \ jsse jar; C: \ Program Files \ Java \ jdk1.8.0 _181 \ jre \ lib \ management - agent jar; C: \ Program Files \ Java \ jdk1.8.0 _181 \ jre \ lib \ plugin jar; C: \ Program Files \ Java \ jdk1.8.0 _181 \ jre \ lib \ resources jar; C: \ Program Files \ Java \ jdk1.8.0 _181 \ jre \ lib \ rt jar; C:\work\ijprojects\JVM\out\production\JVM; C:\work\ijprojects\ObjectSize\out\artifacts\ObjectSize_jar\ObjectSize.jar"com.mashibing.jvm.gc.LambdaGC [GC (Metadata GC Threshold) [PSYoungGen: 1131k ->1880K(38400K)] 1131k ->1880K(38400K), 0.0022190 secs] [Times: User =0.00 sys=0.00, real=0.00 secs] [Full GC (Metadata GC Threshold) [PSYoungGen: 1880K->0K(38400K)] [ParOldGen: 8K->1777K(35328K)] 1888K->1777K(73728K), [Metaspace: 8164K->8164K(1056768K)], 0.0100681 secs] [Times: User =0.02 sys=0.00, real=0.01 secs] [GC (Last ditch Collection) [PSYoungGen: 0K->0K(38400K)] 1777K->1777K(73728K), 0.0005698secs] [Times: User =0.00 sys=0.00, real=0.00 secs] [Full GC (Last ditch Collection) [PSYoungGen: 0K->0K(38400K)] [ParOldGen: 1777K->1629K(67584K)] 1777K->1629K(105984K), [Metaspace: 8164K->8156K(1056768K)], 0.0124299secs] [Times: The user sys = = 0.06 0.00, Real = 0.01 secs] Java. Lang. Reflect the InvocationTargetException at sun, reflect the NativeMethodAccessorImpl. Invoke0 (Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:388) at sun.instrument.InstrumentationImpl.loadClassAndCallAgentmain(InstrumentationImpl.java:411) Caused by: java.lang.OutOfMemoryError: Compressed class space at sun.misc.Unsafe.defineClass(Native Method) at sun.reflect.ClassDefiner.defineClass(ClassDefiner.java:63) at sun.reflect.MethodAccessorGeneratorThe $1.run(MethodAccessorGenerator.java:399)
    	at sun.reflect.MethodAccessorGeneratorThe $1.run(MethodAccessorGenerator.java:394)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at sun.reflect.MethodAccessorGenerator.generate(MethodAccessorGenerator.java:393)
    	at sun.reflect.MethodAccessorGenerator.generateSerializationConstructor(MethodAccessorGenerator.java:112)
    	at sun.reflect.ReflectionFactory.generateConstructor(ReflectionFactory.java:398)
    	at sun.reflect.ReflectionFactory.newConstructorForSerialization(ReflectionFactory.java:360)
    	at java.io.ObjectStreamClass.getSerializableConstructor(ObjectStreamClass.java:1574)
    	at java.io.ObjectStreamClass.accessThe $1500(ObjectStreamClass.java:79)
    	at java.io.ObjectStreamClass$3.run(ObjectStreamClass.java:519)
    	at java.io.ObjectStreamClass$3.run(ObjectStreamClass.java:494)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.io.ObjectStreamClass.<init>(ObjectStreamClass.java:494)
    	at java.io.ObjectStreamClass.lookup(ObjectStreamClass.java:391)
    	at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1134)
    	at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
    	at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
    	at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
    	at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
    	at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
    	at javax.management.remote.rmi.RMIConnectorServer.encodeJRMPStub(RMIConnectorServer.java:727)
    	at javax.management.remote.rmi.RMIConnectorServer.encodeStub(RMIConnectorServer.java:719)
    	at javax.management.remote.rmi.RMIConnectorServer.encodeStubInAddress(RMIConnectorServer.java:690)
    	at javax.management.remote.rmi.RMIConnectorServer.start(RMIConnectorServer.java:439)
    	at sun.management.jmxremote.ConnectorBootstrap.startLocalConnectorServer(ConnectorBootstrap.java:550)
    	at sun.management.Agent.startLocalManagementAgent(Agent.java:137)
    Copy the code

  6. Deep Understanding of the Java Virtual Machine P59, using Unsafe to allocate direct memory, or using NIO issues

  7. Stack overflow problem -Xss setting is too small

  8. Compare the similarities and differences between these two programs, and analyze which is the better way to write:

    Object o = null;
    for(int i=0; i<100; i++) { o = new Object(); // Business processing}Copy the code

    for(int i=0; i<100; i++) {
        Object o = new Object();
    }Copy the code

  9. Overwriting finalize causes frequent GC mirecloud and HBase synchronization system, and the system raises timeout alarm through nginx access. Finally, why C++ programmers rewrite finalize causes frequent GC problems? Finalize (New Delete) Takes a long time (200ms)

  10. If you have a system that consumes no more than 10% of memory all the time, but you look at the GC log and you see that FGC is always occurring frequently, what causes it? System.gc() (this is Low)

  11. Distuptor has an option to set the length of the chain. If it is too large and the object is too large, it will overflow if it is not released after consumption.

  12. A temporary version of 1.6.5 has problems resolving the SQL subquery algorithm. The combined SQL of 9 exists causes millions of objects to be generated.

  13. The JVM memory is 50% to 80% of the physical memory. If the number of threads is too high, native Thread OOM will be generated. If the number of threads is too high, native thread OOM will be generated

Common GC parameters

  • – xmn-xMS-XMX-XSS Young generation minimum heap maximum heap stack space
  • -xx :+UseTLAB Uses TLAB, which is enabled by default
  • -xx :+PrintTLAB Prints the usage of TLAB
  • -xx :TLABSize Sets the TLAB size
  • -xx :+DisableExplictGC system.gc () Does not work, FGC
  • -XX:+PrintGC
  • -XX:+PrintGCDetails
  • -XX:+PrintHeapAtGC
  • -XX:+PrintGCTimeStamps
  • – XX: + PrintGCApplicationConcurrentTime (low) to print the application time
  • – XX: + PrintGCApplicationStoppedTime (low) to print the pause time
  • -xx :+PrintReferenceGC (low importance) records how many references of different reference types are recovered
  • -verbose: indicates the detailed class loading process
  • -XX:+PrintVMOptions
  • -xx :+PrintFlagsFinal -xx :+PrintFlagsInitial
  • -Xloggc:opt/log/gc.log
  • -xx :MaxTenuringThreshold Age of generation ascending. The maximum value is 15
  • Lock spin count -xx :PreBlockSpin Hotspot code detection parameter -xx :CompileThreshold Escape analysis scaler replace… These are not recommended

Parallel Common parameters

  • -XX:SurvivorRatio
  • How – XX: PreTenureSizeThreshold large object to the end
  • -XX:MaxTenuringThreshold
  • -xx :+ParallelGCThreads Number of parallel collector threads. This parameter is also applicable to the CMS and is usually set to the same number of CPU cores
  • -xx :+UseAdaptiveSizePolicy Automatically selects the size ratio of each area

CMS Common Parameters

  • -XX:+UseConcMarkSweepGC
  • -xx :ParallelCMSThreads Number of CMS threads
  • – XX: CMSInitiatingOccupancyFraction began after the CMS how much proportion of the old s collection, the default is 68% (approximation), if frequent SerialOld caton, should be small, frequent (CMS)
  • – XX: + UseCMSCompactAtFullCollection during FGC is compressed
  • How many times – XX: CMSFullGCsBeforeCompaction FGC after compressing
  • -XX:+CMSClassUnloadingEnabled
  • – XX: CMSInitiatingPermOccupancyFraction reaches what proportion in Perm
  • GCTimeRatio sets the percentage of GC time that occupies the running time of the program
  • -xx :MaxGCPauseMillis pause time, which is a suggested time that GC will try to achieve by various means, such as reducing the young generation

G1 Common Parameters

  • -XX:+UseG1GC
  • -xx :MaxGCPauseMillis recommended value, G1 will try to adjust the number of blocks in Young to reach this value
  • – XX: GCPauseIntervalMillis? The interval for GC
  • -xx :+G1HeapRegionSize Indicates the partition size. You are advised to increase the value gradually: 1 2 4 8 16 32. As size increases, garbage lives longer, GC intervals are longer, but the time per GC is also longer. ZGC has been improved (dynamic block size)
  • G1NewSizePercent Indicates the minimum percentage of the new generation. The default value is 5%
  • G1MaxNewSizePercent Indicates the maximum percentage of the new generation. The default value is 60%
  • GCTimeRatio Indicates the recommended GC time ratio. G1 adjusts the heap space according to this value
  • ConcGCThreads Number of threads
  • InitiatingHeapOccupancyPercent start G1 heap space utilization ratio

Think about:

  1. -xx: What does MaxTenuringThreshold control? A: the age at which the object moves into the old age B: the percentage of memory garbage when the old age triggers FGC

  2. Production environments tend to set maximum heap memory and minimum heap memory to :(why?) A: Same B: different

  3. The default garbage collector for JDK1.8 is: A: ParNew + CMS B: G1 C: PS + ParallelOld D: none of the above

  4. What is response time priority?

  5. What is throughput first?

  6. What is the difference between ParNew and PS?

  7. What is the difference between ParNew and ParallelOld? (Different years, different algorithms)

  8. For long computing scenarios, choose A: pause time B: throughput

  9. Large-scale e-commerce sites should choose A: pause time B: throughput

  10. What are the most common garbage collectors for HotSpot?

  11. What are the common combinations of HotSpot garbage collectors?

  12. What is the default garbage collector for JDK1.7 1.8 1.9? How do I view it?

  13. What is tuning?

  14. If the PS + ParrallelOld combination is used, what can be done to make the system produce almost no FGC

  15. If you use the ParNew + CMS combination, how can you make the system almost FGC free

    1. Increase the JVM memory

    2. Increase the proportion of Young

    Raise the age of Y-O

    4. Increase the proportion of S zone

    5. Avoid code memory leaks

  16. Is G1 generational? Does the G1 garbage collector generate FGC?

  17. If G1 produces FGC, what should you do?

    1. Expand the memory
    2. Improved CPU performance (faster collection, fixed rate of object generation by business logic, faster garbage collection, larger memory space)
    3. Lower the threshold for mixEDGCs to trigger, allowing mixedGCs to occur earlier (default: 45%)
  18. Q: Is it possible to dump casually in a production environment? Small heaps do not have much impact, heaps will have service pauses or throttles (which can be mitigated by adding live), and FGC before dump

  19. Q: What are some common OOM issues? Stack heap MethodArea direct memory

The resources

  1. blogs.oracle.com/jonthecollector/our-collectors
  2. Docs.oracle.com/javase/8/do…
  3. Java.sun.com/javase/tech…
  4. The JVM tuning reference documentation: docs.oracle.com/en/java/jav…
  5. www.cnblogs.com/nxlhero/p/1… Online Troubleshooting Tool
  6. www.jianshu.com/p/507f7e0cc… Common arthas commands
  7. Arthas handbook:
    1. Start arthas java-jar arthas-boot.jar
    2. Binding a Java process
    3. The dashboard command is used to view the overall system status
    4. Help View help
    5. Help xx Displays the help of a specific command
  8. Jmap command reference:www.jianshu.com/p/507f7e0cc…
    1. jmap -heap pid
    2. jmap -histo pid
    3. jmap -clstats pid