jvm-object creation and allocation and garbage collection

CAS (compare AND swap) mechanism: comparison and exchange mechanism to solve the concurrency problem of allocating object memory. Methods of allocating memory: pointer collision and free list

 In the past, JVM used CAS to solve the problem of thread concurrency, but there was a continuous loop of CAS that wasted CPU. Nowadays, different memory addresses are allocated to each thread. In the virtual machine, the default memory allocation will be the second way. Developers Use CAS only after it is closed

A java object contains these things:

GC recycling

Among the methods to determine the survival of an object, the commonly used reference counting algorithm is +1 when an object has a reference. This is relatively simple, but it becomes troublesome when two objects point to each other, because it is reasonable to say that these two objects are useless. The object should be garbage and should be recycled, but it caused a deadlock, so it cannot be recycled. The current virtual machine uses a reachability analysis algorithm.

The principle of the reachability algorithm: There is an array in the GC that stores the stack, static variables, constant pool and other data as the root node. Traverse it. If the object is not traversed, it will be recycled. There is an object in the object and nothing refers to it. , is regarded as garbage and should be recycled. For example, in the deadlock below, they just guide each other in the heap, but there is nothing to guide them in the GC roots, so GC treats them as garbage.

 Quote from Java:

 

 Strong reference: GC will not recycle as long as there is a strong reference.

Soft drink: only when the application memory is not enough, it will be recycled

Weak drinking: as long as the GC is triggered, it will be recycled

Object allocation principles for garbage collection:

1. Another aspect of age promotion is dynamic age determination.

For example: from to to old age

It is added starting from the smallest age in the TO area. When it is greater than half of the TO setting memory, it will be directly moved to the old generation from the age when the age is added to the end to the largest age.

2. There is also the issue of age. This threshold is set by the virtual machine. Most of the time it is 15. If it is greater than this threshold, it will be promoted.

Why is the new generation divided into 3 areas? This is because the new generation recycling uses a copy algorithm, and the sizes of the three areas are Eden:from:to=8:1:1 as shown below.

 

 Garbage collection algorithm:

Root reachability algorithm to find unrecyclable objects

Copy algorithm: Copy objects that are not recycled to reserved space, and then garbage collect them

For example: the Eden area of ​​the new generation has a reserved space s0 and s1 in the Eden area. When the Eden area is full, GC will start.

The old generation uses a clearing algorithm. The main reason is that the old generation is basically alive and there is no need to copy.

Garbage collection:

 Young GC only recycles the new generation, while full GC recycles the old generation and the method area.

Question 1: Will expanding the new generation increase efficiency?

 

Guess you like

Origin blog.csdn.net/xueyoubangbang/article/details/121517474