🔉 introduction

When we say that memory management in Java technology is about two things: allocating memory for objects and collecting it. I’ve covered a lot of recycling, so let’s talk about allocating memory for objects that we didn’t cover earlier.

Object allocation are generally allocated in the heap, according to the generational theory, a new object is assigned in the new generation, a small number of cases, may also be directly allocated in old age, the distribution of the object rule is not certain, the Java virtual machine specification, also did not give specific rules, it depends on what we use the garbage collector.

Object allocation

Objects are allocated in Eden first. What if there is no space in Eden? It triggers a MinorGC. Here’s a practical breakdown of the distribution process:

Actual GC mode

Java -xx :+PrintCommandLineFlags -version Java -xx :+PrintCommandLineFlags -version Java -xx :+PrintCommandLineFlags -version

If you want to change the Serial, you can change the -xx :+UseSerialGC configuration.

Actual code

And then I made a piece of code that made three 2MB objects and one 4MB object, don’t ask why, because of the mood.

package jvm; public class TestAlloCation { private static final int _1MB = 1024 * 1024; public static void main(String[] args) { byte[] bytes1 = new byte[_1MB * 2]; byte[] bytes2 = new byte[_1MB * 2]; byte[] bytes3 = new byte[_1MB * 2]; byte[] bytes4 = new byte[_1MB * 4]; System.out.println(bytes1); System.out.println(bytes2); System.out.println(bytes3); System.out.println(bytes4); }}Copy the code

Parameter Settings

After that, we configure the virtual machine parameters, no parameters of the virtual machine that can be called a virtual machine?

Let’s analyze these parameters as follows:

  1. Specifies that the heap size is 20M and is immutable
  2. Print the GC details, or what are we looking at?
  3. Specify an 8-to-1 ratio between Eden and Survivor zones for the new generation

Log analysis

Eden Space 8192K, 78% used from space 1024K, 88% used to pace 1024K, it is indeed 8 to 1, the available space of the new generation is 9126K, allocation failed, when allocating the fourth object, it was found that the space was insufficient 4M. A MinorGC took place, making the Cenozoic by [PSYoungGen: 6804K->959K(9216K)], but the total memory is not reduced. The reason is that the first three objects are still viable objects, and the Eden area is not enough, Survivor is only 1M, so the three objects in Eden area are allocated to the old age, as shown in the figure below.

Log analysis error correction

It stands to reason that there should be in the old days6MOh, how so4M?

After 10 minutes of thinking, I found that the extra 1M was the member variable we defined. Let’s rearrange it: 1M members and 2 2 m objects are allocated. At this point, Eden is already more than 6M, which is about 6742k, so there is still 2474k left. There is not enough space to play, so the first MinorGC happens. 6578K->968K(9216K)], and then allocate the third object, the memory occupation is 3182K, put the remaining 4M objects into the old age, the memory occupation of the old age is 4104K, and then add another object, the new generation is 7369K, which corresponds to the scene before our analysis.

Figure is as follows:

📝 digression

When there are too many factors at work in a phenomenon complex, the scientific method is overwhelmingly ineffective. People only need to think about the weather to know, even if it is only a few days ahead of the forecast is not possible. No one doubts, however, that we are facing a causal link, the components of which are largely known to us. It is the diversity of factors at work, not the lack of order in nature, that makes precise predictions about what happens in this field impossible.