JVM Tuning Series 1: "Proficient in JVM tuning, have experience in JVM tuning", do you dare to write your resume? Tips to raise your salary by 5k!


Preface

Except for some great gods with work and development experience, few people dare to write "proficient in JVM tuning and have experience in JVM tuning" on their resumes, because if the candidate writes this sentence, it means that your interview will be It is very "sad". The interviewer will change the way to ask you how to tune the JVM series. If you have a weak foundation or only quick test questions on the back, then you are likely to show up in the interview. As the core of Java, JVM interviews with back-end development engineers or architects are all necessary skills. Since JVM is so important, then I will go through it in this series completely, let you dare to write "proficient in JVM tuning, have experience in JVM tuning" on your resume, and your salary will increase by 5k!

Insert picture description here


Before talking about JVM tuning and GC, let's take a look at some related interview questions about JVM and GC in the current employment environment, and see what kind of talents are needed for enterprise development posts.

1. Some big companies' interview questions for JVM GC

Note : The following data comes from interview questions from some major companies from 2019 to 2020.

1.1 Baidu

  • The similarities and differences between CMS and G1.
  • When does G1 trigger FullGC?
  • Say one of the most familiar garbage collection algorithms.
  • What are the collectors that prioritize throughput and response time?

1.2 SF Express

  • How to judge a memory leak?
  • Talk about the process of CMS.

1.3 JD

  • Why does the compressed pointer fail if it exceeds 32G?

1.4, Taobao

  • What is a memory leak? Do you have experience in GC tuning? How do you solve general GC problems?

1.5 Ali, Mogujie

  • Does ThreadLocal have memory leaks?
  • G1 The two regions are not continuous, and there are reachable references between them. I want to reclaim one now, what should I do with the other?
  • Talk about JVM heap memory management (object allocation process).
  • Have you heard of concurrent preprocessing and concurrent interruptible preprocessing of CMS?
  • Just how big an object will be thrown directly into the old age?

What do you think after reading the interview questions? Some people may think it is simple, some have a little knowledge, but have heard it but won't. Don't worry! Let's take it slowly.

2. How to use Garbage Collectors (GC)

First of all, we all know that in Java, after we create an object, we don't need to worry about it. After the object is not used and becomes a garbage, who will deal with it? Garbage Collectors.

Remarks : Garbage Collectors will be replaced by GC in subsequent articles.

Three, ten types of GC

As the hub of Java, JVM manages all memory, and the allocation and recovery of memory are controlled by the garbage collector.

From the birth of JVM to 2021, a total of ten types of garbage collectors have been produced , as shown in the following figure:

Insert picture description here
Here you can only understand these ten types of garbage collectors, and we will introduce them one by one in subsequent chapters.

Four, JVM version parameter information

Currently , the version used by most enterprise development is 1.8 . My local development version is also 1.8, and we will use this version to demonstrate.

Insert picture description here
If we deploy the program directly to the JVM without any tuning actions, the default GC of version 1.8 is Parallel .

Some people may have questions: How to check the default GC version information of the JVM in the development environment? Don't worry, look down.

First of all, let's get to know the following commands. Let's open the CMD command interface.

4.1. View all default command line parameters when Java starts

Enter the following Shell command:

java -XX:+PrintCommandLineFlags

This is where we can see all the default command line parameters printed when Java starts, as shown in the following figure:

Insert picture description here

4.2. View GC parameter information when Java starts

If you only need to view the GC parameter information in the local environment, we can enter the following Shell command:

java -XX:+PrintCommandLineFlags -version

We can know from the console's return information that the default GC corresponding to version 1.8.0_251-b08 is Parallel , as shown in the following figure:

Insert picture description here

V. Generation model and partition model of GC

As we have mentioned above, the allocation and recycling of memory is controlled by the garbage collector.

We divide the use of GC into two categories, namely: generational model and partition model .

  • Generational model: Two garbage collectors hybrid control garbage collection in the system.
  • Partition model: No more generations, one garbage collector is enough.

In this section we will focus on the introduction of the generational model.

5.1, generational model

The generational model divides the memory into two large parts, the new generation and the old generation .

As shown in the figure below, the left half is the new generation, and the right half is the old generation:

Insert picture description here
Application of recovery algorithm corresponding to the new generation and the old generation :

  • The new generation died in large numbers and survived in small numbers, using a replication algorithm.
  • The old age has a high survival rate and less recovery, using MC or MS.

5.1.1, Cenozoic

The newly born object is "Newborn", which is stored in the Cenozoic area. As the program runs, most "new objects" are garbage collected.

5.1.2, the old age

Objects that the garbage collector has collected many times but have not been collected are called "old age" and are stored in the old age.

By default, JDK 1.8 has no tuning parameters, and tuning is the generational model of GC.


to sum up

In this article, through interview questions, we know the requirements of large manufacturers for JVM development, temporarily understand the ten types of GC that exist in the JVM, learn how to view the default GC of the local development environment, and master the two ways of using GC, the generation model With the partition model, understand what kind of objects belong to what generation and where they are stored in memory. At the same time, various garbage collection algorithms will be born in the new and old generations, which we will explain in the next section.

Insert picture description here


I am Bailu, a programmer who works tirelessly. Hope this article can be of benefit to you, welcome everyone's one-click three-connection! If you have any other questions, suggestions or supplements, you can leave a message at the bottom of the article, thank you for your support!
More information WeChat search public accountWDeerCode code circle

Guess you like

Origin blog.csdn.net/qq_22695001/article/details/114830531