Object creation
- Pointer collision: Assume that the memory in the Java heap is perfectly neat, with all used memory on one side and free memory on the other, with a pointer in the middle as an indicator of the dividing point. The allocated memory simply moves the pointer to free space by a distance equal to the size of the object. This allocation method is called “pointer collision”.
- Free list: If memory is not tidy, used memory interacts with free memory, and there is no way to do simple pointer collisions. The VIRTUAL machine maintains a list of memory blocks that are available, finds a block from the list that is large enough to be allocated to instance objects, and updates the list. This is called a “free list.”
The choice of allocation is determined by the integrity of the Java heap, which in turn is determined by the ability of the garbage collector to compress space. Collectors such as.serial.parnew with a collation process use pointer collisions. CMS based cleanup algorithms use free lists
Not thread-safe in concurrent cases:
- CAS + failed retry to ensure atomic operation
- Local thread allocation buffer (TLAB) : Allocating memory in different Spaces according to threads. That is, each thread allocates a small chunk of memory in the Java heap in advance, which is called the local thread allocation buffer. The thread that allocates memory is allocated in the local buffer of that thread.
Object memory layout
In the Hotspot VIRTUAL machine, the storage layout of objects in the heap memory can be divided into three parts: object header instance data alignment padding
The object header contains two parts
- Mark Word
- Hash code (HashCode)
- GC generational age
- Lock information (lock status indicates that the lock held by the thread is biased toward the thread ID)
- Type pointer: A pointer to type metadata to determine which class it is an instance of
- If the object is an array, the object header also contains data that records the length of the array
Instance data is the content of the various types of fields defined in our code
Object access location
Objects are naturally created for later use, and Java programs manipulate specific objects on the stack using reference data on the stack.
The main access methods are handle and direct pointer
- Handle: A block of memory is allocated to the Java heap as a handle pool. Reference stores the address of the handle of the object, and the handle contains the address information of the instance data and the object type data respectively.
- Direct Pointers: Reference stores the address of the object,
The biggest benefit of using handles for access is that reference stores a stable handle address and only changes the instance data pointer in the handle when the object is moved (which is very common in garbage collection), not reference itself.
The advantage of using a direct pointer is that it is faster and it saves time for a pointer location (the HotSpot VIRTUAL machine is mainly used for object access location).
Memory allocation policy
The Java Heap is the primary area managed by the Garbage collector and is therefore also known as the Garbage Collected Heap. From the point of view of garbage collection, the Java heap can be subdivided into: new generation and old generation: Eden space, From Survivor, To Survivor space, etc. The purpose of further partitioning is to better reclaim memory, or to allocate memory faster.
Basic structure of heap space:
Eden zone, From Survivor0(” From “) zone and To Survivor1(” To “) zone belong To Cenozoic era, and Old Memory zone belongs To Old age.
In most cases, the object will be allocated in Eden first. After a Cenozoic garbage collection, if the object is still alive, it will enter S0 or S1 and its age will be increased by 1(the initial age of the object becomes 1 after Eden ->Survivor). When it reaches a certain age (15 by default), it is promoted to the old age.
Big object goes straight to the old age
Large objects are objects that require a large amount of contiguous memory (e.g., strings, arrays).
Why is that? To avoid the loss of efficiency when allocating memory for large objects due to replication caused by the allocation guarantee mechanism.
For the HotSpot VM implementation, there are actually only two types of GC that are accurate:
Partial GC:
- Minor/Young GC: Garbage collection is only for Young generations.
- Major GC/Old GC: Only Old GC is collected. It is important to note that the Major GC is also used in some contexts to refer to a whole heap collection;
- Mixed GC: Garbage collection for the entire New generation and part of the old generation.
Full GC: Collects the entire Java heap and method area.
Is the object dead?
Reference counting algorithm: Adds a reference counter to an object, incrementing the counter by one each time a reference is referenced, and decrement the calculator by one when the reference is invalid. Objects whose calculators are zero at any time can no longer be used.
Reachability analysis algorithm: If an object is not connected to GCRoots by any reference chain, or is unreachable from GCRoots to the object in graph theory, then the object cannot be used again.
Reference counting method
Add a reference counter to the object, incrementing it every time it is referenced; When a reference is invalid, the counter is decayed by 1; Any time an object with a 0 counter is no longer usable.
This method is easy to implement and efficient, but the mainstream virtual machine does not choose this algorithm to manage memory, its main reason is that it is difficult to solve the problem of object reference cycle.