源碼分析:Java對象的記憶體配置(1)
Java對象的分配,根據其過程,將其分為快速分配和慢速分配兩種形式,其中快速分配使用無鎖的指標碰撞技術在新生代的Eden區上進行分配,而慢速分配根據堆的實現方式、GC的實現方式、代的實現方式不同而具有不同的分配調用層次。
下面就以bytecodeInterpreter解譯器對於new指令的解釋出發,分析執行個體對象的記憶體配置過程:
一、快速分配
1.執行個體的建立首先需要知道該類型是否被載入和正確解析,根據位元組碼所指定的CONSTANT_Class_info常量池索引,擷取對象的類型資訊並調 用is_unresovled_klass()驗證該類是否被解析過,在建立類的執行個體之前,必須確保該類型已經被正確載入和解析。
- CASE(_new): {
- u2 index = Bytes::get_Java_u2(pc+1);
- constantPoolOop constants = istate->method()->constants();
- if (!constants->tag_at(index).is_unresolved_klass()) {
2.接下來擷取該類型在虛擬機器中的表示instanceKlass(具體可以參考前文執行個體探索Java對象的組織圖)
- oop entry = constants->slot_at(index).get_oop();
- assert(entry->is_klass(), "Should be resolved klass");
- klassOop k_entry = (klassOop) entry;
- assert(k_entry->klass_part()->oop_is_instance(), "Should be instanceKlass");
- instanceKlass* ik = (instanceKlass*) k_entry->klass_part();
3.當類型已經被初始化並且可以被快速分配時,那麼將根據UseTLAB來決定是否使用TLAB技術(Thread-Local Allocation Buffers,線程局部分配緩衝技術)來將分配工作交由線程自行完成。TLAB是每個線程在Java堆中預先分配了一小塊記憶體,當有對象建立請求記憶體分 配時,就會在該塊記憶體上進行分配,而不需要在Eden區通過同步控制進行記憶體配置。
- if ( ik->is_initialized() && ik->can_be_fastpath_allocated() ) {
- size_t obj_size = ik->size_helper();
- oop result = NULL;
- // If the TLAB isn't pre-zeroed then we'll have to do it
- bool need_zero = !ZeroTLAB;
- if (UseTLAB) {
- result = (oop) THREAD->tlab().allocate(obj_size);
- }
- if (result == NULL) {
- need_zero = true;
4.如果不使用TLAB或在TLAB上分配失敗,則會嘗試在堆的Eden區上進行分配。Universe::heap()返回虛擬機器記憶體體系所 使用的CollectedHeap,其top_addr()返回的是Eden區空閑塊的起始地址變數_top的地址,end_addr()是Eden區空 閑塊的結束位址變數_end的地址。故這裡compare_to是Eden區空閑塊的起始地 址,new_top為使用該塊空閑塊進行分配後新的空閑塊起始地址。這裡使用CAS操作進行空閑塊的同步操作,即觀察_top的預期值,若與 compare_to相同,即沒有其他線程操作該變數,則將new_top賦給_top真正成為新的空閑塊起始地址值,這種分配技術叫做bump- the-pointer(指標碰撞技術)。
- retry:
- HeapWord* compare_to = *Universe::heap()->top_addr();
- HeapWord* new_top = compare_to + obj_size;
- if (new_top <= *Universe::heap()->end_addr()) {
- if (Atomic::cmpxchg_ptr(new_top, Universe::heap()->top_addr(), compare_to) != compare_to) {
- goto retry;
- }
- result = (oop) compare_to;
- }
- }
5.根據是否需要填0選項,對分配空間的對象資料區進行填0
- if (result != NULL) {
- // Initialize object (if nonzero size and need) and then the header
- if (need_zero ) {
- HeapWord* to_zero = (HeapWord*) result + sizeof(oopDesc) / oopSize;
- obj_size -= sizeof(oopDesc) / oopSize;
- if (obj_size > 0 ) {
- memset(to_zero, 0, obj_size * HeapWordSize);
- }
- }
6.根據是否使用偏向鎖,設定對象頭資訊,然後設定對象的klassOop引用(這樣對象本身就擷取了擷取類型資料的途徑)
- if (UseBiasedLocking) {
- result->set_mark(ik->prototype_header());
- } else {
- result->set_mark(markOopDesc::prototype());
- }
- result->set_klass_gap(0);
- result->set_klass(k_entry);
7.把對象地址引入棧,並繼續執行下一個位元組碼
- SET_STACK_OBJECT(result, 0);
- UPDATE_PC_AND_TOS_AND_CONTINUE(3, 1);
8.若該類型沒有被解析,就會調用InterpreterRuntime的_new函數完成慢速分配
- // Slow case allocation
- CALL_VM(InterpreterRuntime::_new(THREAD, METHOD->constants(), index),
- handle_exception);
- SET_STACK_OBJECT(THREAD->vm_result(), 0);
- THREAD->set_vm_result(NULL);
- UPDATE_PC_AND_TOS_AND_CONTINUE(3, 1);
以上就是快速分配的過程,其流程圖如下,關鍵在於快速分配在Eden區所使用的無鎖指標碰撞技術