Initialization of heap memory for JVM source code analysis

Posted May 22, 20205 min read

Original statement:This article is original by the public number [Ape Lighthouse], please indicate the source

Part 15 of "365 ArticlesOriginal Project".

Today! Dengta Jun told you:

Initialization of heap memory for JVM source code analysis

Heap initialization

The initialization entry of the Java heap is located in the Universe::initialize_heap method, implemented as follows:

Among them, UseParallelGC, UseG1GC, UseConcMarkSweepGC can be set through the startup parameters, the entire initialization process is divided into three steps:

  1. Initialize the GC strategy;
  2. Initialize the generation generator;
  3. Initialize the Java heap manager;

GC strategy initialization

HotSpot's GC strategy is implemented as follows:

Among them, MarkSweepPolicy is a GC strategy based on the mark-and-clear idea. If the virtual machine startup parameter does not specify the GC algorithm, the default uses UseSerialGC. Taking the ASConcurrentMarkSweepPolicy policy as an example, the initialization process of the GC policy is analyzed:

Call the parent class ConcurrentMarkSweepPolicy constructor, in which initialize_all is defined in GenCollectorPolicy, the implementation is as follows:


Responsible for the alignment adjustment of the memory size set by the new generation, old generation and permanent generation.

1, adjust the permanent generation

Implemented by CollectorPolicy::initialize_flags, the initial value of the permanent generation defaults to 4M and the maximum value is 64M, which can be reset by the parameters -XX:PermSize and -XX:MaxPermSize.

2. Adjust the new generation

Implemented by GenCollectorPolicy::initialize_flags:

  1. The initial value of the new generation NewSize is 1M by default, the maximum value needs to be set, and can be set by the parameters -XX:NewSize and -XX:MaxNewSize or -Xmn;

  2. NewRatio is the ratio of the size of the old generation to the new generation, the default is 2;

  3. SurvivorRatio is the size ratio of Eden and Survivor in the new generation, the default is 8;


Set the capacity of the new generation, old generation and permanent generation, including the initial value, minimum value and maximum value

Set heap capacity

Among them, InitialHeapSize and Arguments::min_heap_size() can be set by the parameter -Xms.

  1. Set the initial heap capacity _initial_heap_byte_size;

  2. Set the minimum heap capacity _min_heap_byte_size;

  3. Set the maximum heap capacity _max_heap_byte_size;

Set new generation

  1. If MaxNewSize has been reset, that is, the -Xmn parameter is set, then max_new_size is set according to different circumstances;

  2. Otherwise, recalculate the value of max_new_size according to NewRatio and _max_heap_byte_size through the scale_by_NewRatio_aligned method, where NewRatio is 2 by default, indicating that the size of the new generation accounts for 1/3 of the entire heap;

  1. If the maximum heap _max_heap_byte_size is equal to the minimum heap _min_heap_byte_size, set the initial value, minimum value and maximum value of the young generation to max_new_size, otherwise perform step 4;

  1. If NewSize has been reset, that is, the -Xmn parameter is set, then use NewSize to set _min_gen0_size, otherwise use the scale_by_NewRatio_aligned method to recalculate the minimum and initial values of the new generation. The implementation is as follows:

Set the old generation
  1. If the parameter does not set OldSize, then use min_heap_byte_size()-min_gen0_size(), that is, the difference between the minimum heap size and the minimum value of the new generation sets the minimum value of the old generation, the initial value is similar;

  2. Otherwise, according to the set OldSize, reset the minimum value and initial value of the new generation through the adjust_gen0_sizes method;

Initializing the generation generator

The generation generator saves the initial value and maximum value of each memory generation, the new generation and the old generation are realized by GenerationSpec, and the permanent generation is realized by PermanentGenerationSpec.

GenerationSpec implementation

Each generator GenerationSpec instance saves the current generation of the GC algorithm, the initial value and maximum value of the memory.

PermanentGenerationSpec implementation

In addition to the data in the GenerationSpec instance, if UseSharedSpaces and DumpSharedSpaces are set, additional data needs to be saved.

The ConcurrentMarkSweepPolicy::initialize_generations method implements the initialization of the generation generator, which is implemented as follows:

Create generator instances corresponding to the new generation, old generation, and permanent generation.

Initialize the Java heap manager

GenCollectedHeap is the manager of the entire Java heap. It is responsible for the memory allocation of Java objects and the recycling of garbage objects. It is initialized by the initialize method and is implemented as follows:

  1. Obtain the number of generations through the number_of_generations method of the GC strategy. If you use ASConcurrentMarkSweepPolicy, the default generation number is 2;

  2. Align the initial value and the maximum value of the generator through the align method(why do you need to align all the time, I think it has been aligned many times when the GC strategy was initialized earlier)

  1. Apply space for the heap through allocate;

  1. Use the init method of the generation generator to allocate memory space for the corresponding generation;

  1. If the current GC strategy is ConcurrentMarkSweepPolicy, create a GC thread through create_cms_collector.

365 days dry goods constantly WeChat search "Ape Lighthouse" read the first time, reply [Information][Interview][Resume]I have prepared the first-line interview materials and resume templates