Original statement: this article by the public account [ape Lighthouse] original, reproduced please note the source notes

“365 Original Project” 15.

Today! Mr. Lighthouse told us:

JVM source code analysis of heap memory initialization



Pile of initialization


The initialization entry for the Java heap is located
Universe::initialize_heapMethod, the implementation is as follows:

UseParallelGC, UseG1GC, and UseConcMarkSweepGC can all be set by startup parameters. The whole initialization process is divided into three steps:


1. Initialize GC policy;


2. Initialize generation generator;


Initialize the Java heap manager.


GC policy initialization

HotSpot’s GC policy is implemented as follows:

Including MarkSweepPolicy GC policy is based on the tag – clear thought, if the virtual machine startup parameter is not specified the GC algorithm, using the default use UseSerialGC, ASConcurrentMarkSweepPolicy strategy, for example, the initialization process of GC policy analysis:

Calls the superclass ConcurrentMarkSweepPolicy construction method, in which the initialize_all defined in GenCollectorPolicy, implementation is as follows:

initialize_flags

Responsible for memory size alignment for new generation, old generation and permanent generation Settings.

1. Adjust the permanent generation

by
CollectorPolicy::initialize_flagsThe default initial value of the permanent generation is 4M, and the maximum value is 64M
-XX:PermSizeand
-XX:MaxPermSizeTo reset.


Adjust the new generation

by
GenCollectorPolicy::initialize_flagsImplementation:
1. Initial value of Cenozoic
NewSizeThe default value is 1 MB. You can set the maximum value by setting this parameter
-XX:NewSizeand
-XX:MaxNewSizeor
-XmnTo set;
2,
NewRatioIs the size ratio of the old age to the Cenozoic age, which is 2 by default.
3,
SurvivorRatioIs the size ratio of Eden and Survivor in the new generation, which defaults to 8.


initialize_size_info

Sets the capacity of new generation, old generation, and permanent generation, including initial, minimum, and maximum values

Set heap capacity

Among them
InitialHeapSizeand
Arguments::min_heap_size()You can use parameters
-XmsSet.
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 the New generation

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

2, otherwise through scale_by_NewRatio_aligned method according to NewRatio option and _max_heap_byte_size recalculate max_new_size values, including NewRatio option defaults to 2, said the size of the new generation about a third of the whole heap;

3. If the maximum heap _max_HEAP_byte_size is equal to the minimum heap _min_HEAP_byte_size, set the initial, minimum, and maximum values of the new generation to max_new_size. Otherwise, go to Step 4.

4. If NewSize has been reset (that is, the -xmn parameter is set), use NewSize to set _min_gen0_size; otherwise, use scale_by_NewRatio_aligned to recalculate the new generation minimum and initial values as follows:

Set the old age
1. If the parameters are not set
OldSize, use the
min_heap_byte_size() - min_gen0_size(), that is, the difference between the minimum heap size and the Cenozoic minimum value is set as the minimum value in the old age, and the initial value is similar;
2, otherwise according to the Settings
OldSizeThrough the
adjust_gen0_sizesMethods The minimum and initial values of Cenozoic generation were reset.


Initialize the generation generator

Generation generator stores the initial and maximum values of each memory generation, and the new generation and the old generation pass through
GenerationSpecImplementation, permanent generation through
PermanentGenerationSpecThe implementation.


GenerationSpec implementation

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


PermanentGenerationSpec implementation

In addition to
GenerationSpecInstance data, if set
UseSharedSpacesand
DumpSharedSpacesYou also need to save additional data.

ConcurrentMarkSweepPolicy::initialize_generationsThe generation generator initialization method is implemented as follows:

Create instances of generators for new generation, old generation, and permanent generation.


Initialize the Java heap manager

GenCollectedHeap is the manager of the entire Java heap, responsible for the memory allocation and garbage collection of Java objects, through the initialize method initialization, implementation as follows:

1, through the GC policy
number_of_generationsMethod to get a generational number, if use ASConcurrentMarkSweepPolicy, default points algebra for 2;
2, through the
alignMethod to align the initial and maximum values of the generator (why always align, I think it was aligned many times when I initialized the GC policy earlier)

Allocate space for the heap by allocate.

4. By generation generator
initMethods Allocate memory space for the generation.

5, if the current GC policy for ConcurrentMarkSweepPolicy, created by create_cms_collector GC thread.

[Information] [interview] [resume] I have prepared the interview materials and resume template of the first-line big factory