JVM object creation process

1. Check the loading

Checks whether the argument to this directive locates a symbolic reference to a class in the constant pool. And check to see if the class is initialized by load parsing.

Symbolic reference: The use of a group of symbols to describe the referenced object. At compile time, each JAVA class is compiled into a.class file, but the virtual machine does not know the actual address of the referenced class at compile time, so symbolic references are used instead. In the class parsing phase, symbolic references are replaced with real addresses.

2. Allocate memory:

Allocating memory requires dealing with two major issues: how memory is divided and how concurrency is handled safely.

1) The way to partition memory: pointer collision, free list

Pointer collision (regular memory):

Free list (unstructured memory) :

2) Solve concurrency security: CAS failed retry, local thread allocation buffer (TLAB).

CAS failed to retry:

TLAB:

3. Initialize the memory space.

Member variables are assigned initial values.

4. Set the object header.

Alignment padding: The space taken up by the object needs to be an integer multiple of 8 bytes.

5. Initialize the object.

Execute the constructor.

Second, object access location

1. Handle.

There is a pool of handles in the heap, and references stored in the operand stack refer to the pool. It then finds the real object through the handle pool.

Advantages: The address in the operand stack can still refer to the handle pool, but the address of the object corresponding to the handle pool can be changed when memory is defiled.

Disadvantages: Time consuming requires secondary conversion.

2. Direct pointer. (Used by hotspot)

The object address of the operand stack is directly the real address of the object.

Advantages: Fast search speed.

Disadvantages: When the object address changes, you need to change the object address in the thread.

Garbage collector.

1. Check the survival of the object

Core: Useful and executable.

1) Reference counting.

Problem: Two objects refer to each other.

Python solves this problem by starting an extra thread.

2) Root-reachable algorithm

A set of roots

Gcroots: Generally static variables, variables in local variable lists in thread stacks, constant pools, JNI(Pointers)

Extension:

1) Internal references: class objects, exception objects, and class loaders 2) Synchronized locks: objects in a synchronized lock. 3) Internal objects: 4) Temporary objects: cross-generation references (old generation holds references to new generation objects)Copy the code

Interview question: Can CLASS objects be reclaimed?

1. This class object cannot have instance objects. 2. The classloader for this class was reclaimed. 3. The class object is not referenced anywhere and is not used by reflection. 4. Parameter control: Allow recycling of class objects.Copy the code

2. The finalize () method.

When an object does not satisfy the root reachability, when memory is insufficient, finalize method will be called once. If it becomes reachable internally, it will not be recycled (theoretically). When unreachable again, the method is not entered and is directly collected by the garbage collector.

However, the thread priority of this method is very low, and many times the object will not execute this method until it is too late.

3. Various references

1) Strong references: will not be recycled.

2) Soft reference: When memory runs out (will be OOM), it will be reclaimed.

3) Weak references: Garbage is collected when garbage is collected.

4) Virtual reference: Object cannot be obtained through this reference.

4. Object allocation policy

Object allocation technology: 1) object allocation principle. 2) Virtual machine optimization technology.

1. Object allocation principle

  • Objects are allocated in Eden area first
  • Space allocation assures: Cenozoic generation upgrade enters old age tacitly old age is put below. If the old age is not released, fullGC will be carried out again. The scope of guarantee is dynamically calculated according to the recycling amount belt of each FULLGC.
  • Large objects go straight to the old age: serial, parnew garbage collectors have this behavior >4M.
  • Long-lived objects enter the old age. The maximum value is 15, and the CMS default value is 6.
  • Dynamic object age determination.

1) According to the escape algorithm, judge whether the object escapes or not. If not, perform scalar replacement and split the object into stacks. 2) Escape, then determine whether the object is a large object, if it is a large object, then put the large object directly into the old age. 3) Create objects in TLAB first if they are not large objects. 4) If it is not fit in TLAB, put it in Eden area.Copy the code

2. Virtual machine optimization technology

  • Escape analysis + triggering jit -> Two points are needed for on-stack allocation.
  • The local thread allocates the buffer

5. Garbage collection algorithm

Copy algorithm, tag clearing algorithm, tag sorting algorithm

1) Replication algorithm

Features :(used in zone 0,1 of Cenozoic survivors)

  • Realize simple operation and high efficiency.
  • No memory fragmentation.
  • Utilization is only half.

Appel recycling (Division of New Generation)

Because most of it is garbage. So the new generation is divided eight to one. (90 percent space utilization)

2) Mark clearing algorithm

Features:

  • Discontinuous position, resulting in debris.
  • You can do it without pausing

3) Label sorting algorithm

Features :(the mark sorting algorithm used is sorting and then clearing)

  • No memory fragmentation.
  • Pointer needs to move, time consuming.