introduction
I prefer to study topics in the following order. First, I like to learn about memory structure, understanding what happens form the moment an object is created untill is deleted. Once i grasp the basics of memory, I naturally become curious about how objects are created and destroyed. After that, it leads to an exploration of how to garbage collection works to handle unused object.
create object
When the new
keyword is encountered
- The system chekcs if the parameter references a class that is a symbolic instance in the constant pool.
- it verifies whether the class has been loaded, resolved and initailized. if not, the system peforms these steps.
Once the class is loaded, the memory size required is determined. there are two ways to handle memory alloation:
- Usign a pointer that points to the boundary between used and available memory.
- Managing a free list of memory blocks to allocate space.
In a multi-threaded environment, concurrency problems can occur during object creation. To handle this, there are two approaches:
1. Synchronize memory allocation using atomic operations
2. Use Thread-Local Allocation Buffers (TLAB), where each thread manages its own private memory space.
After memory is allocated, the follwing steps occur:
- The allocated memory is initialized by filling it with zeros.
- The object header is set.
- the constructor is called.
object layout
An object consists of three parts: the header, instance data, and alignment padding.
the header includes the mark word and klass word:
- mark word
- Its size is 32 or 64 bits.
- The data structure can change dynamically to reuse space depending on the object's stae
- It stores the object's hash code in 25 bits.
- 4 bits are used to indicate the object's generation
- 1 bit is just reserved.
- The last 2 bits represent the lock status flag.
- klass word:
- A pointer that refers to the class's metadata.
- The JVM uses this class pointer to recognize the object's class and determine its size from the metadata
- For arrays, it stores the array length, which helps in calculating the memory size.
instance data
The instance data contains the fields of the object, including fields inherited from the parent class.The order of fields typically follows this pattern
1. long, double
2. int
3. short, char
4. byte, boolean
5. pointer
If the lengths of the fields are the same, the fields of the parent class are placed first.
alignment padding
The size of every object must be a multiple of 8 bytes.
access to object
Objects can be accessed either through a handle or a direct pointer.
handle
- A reference in the stack points to a handle in the handle pool.
- The handle pool exists in the heap.
- The handle contains references to both the instacne data and the type data.
- The reference in the stack remains the same, even after garbagte collection, as the handle adderess does not change.
- However, the references in the handle pool may change after GC.
pointers
- Similar to a v-table in C++, the direct pointer holds a reference to the type data and the instace data.
Next, I'll study garbage collection