“This is the 14th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

1 introduction

As I was interested in JVM, I happened to see the blog of one of the big guys in the former Ali JVM team. Coincidentally, he also invested in the tech company founded by the tycoon.

There is an article in the big guy’s blog that asks a question about JVM object allocation. If you are interested, read on to learn more!

2 Introduction

After one GC occurs, from Space utilization is 12%, but by the time the next YGC occurs, from space utilization is 99%.

See here, please think for a moment, I don’t know what you think about this. Here are the comments from the big guys:

  • Answer 1: If you think this is abnormal, you have some understanding of JVM memory analysis, but you still don’t fully understand it.
  • Answer 2: If this is ok with you, most of the time you are not familiar with JVM memory allocation, and very few of the time you are familiar enough with it to understand its implementation pros and cons.

3 Principle Analysis

By default, everyone knows about the CMS generation and the old generation, and the replication algorithm of the new generation GC.

In THE JVM, the heap is mainly composed of the new generation and the old generation, and the new generation is composed of Eden + S0 (from space)+ S1 (to space) respectively. Usually, one of S0 or S1 must be empty, which is mainly used for GC copy.

When we create a normal object (ps: not too large), we request a memory allocation. This memory is allocated in the Eden region of the new generation. Of course some special cases can be allocated directly to old generation.

Under this rule, memory is not normally allocated from space, so it is not possible to allocate from space after the last GC until the next GC.

Rut also found the next school around the small partner, just out of the campus theoretical knowledge also forget not much πŸ˜‚. His answer was no problem. What was your answer?

4 simulation GC

Here we simulate the normal and abnormal GC, and then look at the results through the GC information and heap information.

4.1 the JVM configuration

-Xms1024m
-Xmx1024m
-XX:+PrintGCDetails
-XX:+PrintHeapAtGC
-XX:+UseParNewGC
-XX:+UseConcMarkSweepGC
Copy the code

4.2 normal GC

First we create in a loopBigDataObject, which triggers YoungGC, and you can see that the space from space has not changed Before GC. So normally, the space from space doesn’t change.

Answer two friends go from penalty three cups 🍺🍺

4.3 abnormal GC

  1. Trigger the CMS GC through the OOM code described in the previous GC log

  1. When the Young GC is triggered, you can see that the space from space is also allocated objects before the GC

4.4. Reasons for allocation

According to the big guy’s explanation is a bug in JVM, I dare not ask 🀣🀣, really interested friends can refer to the article to see ~

At the end of may

This section introduces the problem of the copy algorithm of THE JVM memory allocation. If you don’t understand the problem, you are advised to make up the class.

6 Reference Article

– Here’s a GC question for PerfMa interview (Definitely rising posture)