源碼分析:Java對象的記憶體配置(1)

來源:互聯網
上載者:User

源碼分析:Java對象的記憶體配置(1)

Java對象的分配,根據其過程,將其分為快速分配和慢速分配兩種形式,其中快速分配使用無鎖的指標碰撞技術在新生代的Eden區上進行分配,而慢速分配根據堆的實現方式、GC的實現方式、代的實現方式不同而具有不同的分配調用層次。 
下面就以bytecodeInterpreter解譯器對於new指令的解釋出發,分析執行個體對象的記憶體配置過程:

一、快速分配

1.執行個體的建立首先需要知道該類型是否被載入和正確解析,根據位元組碼所指定的CONSTANT_Class_info常量池索引,擷取對象的類型資訊並調 用is_unresovled_klass()驗證該類是否被解析過,在建立類的執行個體之前,必須確保該類型已經被正確載入和解析。

  1. CASE(_new): { 
  2.         u2 index = Bytes::get_Java_u2(pc+1); 
  3.         constantPoolOop constants = istate->method()->constants(); 
  4.         if (!constants->tag_at(index).is_unresolved_klass()) { 

2.接下來擷取該類型在虛擬機器中的表示instanceKlass(具體可以參考前文執行個體探索Java對象的組織圖) 

  1. oop entry = constants->slot_at(index).get_oop(); 
  2.           assert(entry->is_klass(), "Should be resolved klass"); 
  3.           klassOop k_entry = (klassOop) entry; 
  4.           assert(k_entry->klass_part()->oop_is_instance(), "Should be instanceKlass"); 
  5.           instanceKlass* ik = (instanceKlass*) k_entry->klass_part(); 

3.當類型已經被初始化並且可以被快速分配時,那麼將根據UseTLAB來決定是否使用TLAB技術(Thread-Local Allocation Buffers,線程局部分配緩衝技術)來將分配工作交由線程自行完成。TLAB是每個線程在Java堆中預先分配了一小塊記憶體,當有對象建立請求記憶體分 配時,就會在該塊記憶體上進行分配,而不需要在Eden區通過同步控制進行記憶體配置。

  1. if ( ik->is_initialized() && ik->can_be_fastpath_allocated() ) { 
  2.             size_t obj_size = ik->size_helper(); 
  3.             oop result = NULL; 
  4.             // If the TLAB isn't pre-zeroed then we'll have to do it 
  5.             bool need_zero = !ZeroTLAB; 
  6.             if (UseTLAB) { 
  7.               result = (oop) THREAD->tlab().allocate(obj_size); 
  8.             } 
  9.             if (result == NULL) { 
  10.               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(指標碰撞技術)。

  1. retry: 
  2.               HeapWord* compare_to = *Universe::heap()->top_addr(); 
  3.               HeapWord* new_top = compare_to + obj_size; 
  4.               if (new_top <= *Universe::heap()->end_addr()) { 
  5.                 if (Atomic::cmpxchg_ptr(new_top, Universe::heap()->top_addr(), compare_to) != compare_to) { 
  6.                   goto retry; 
  7.                 } 
  8.                 result = (oop) compare_to; 
  9.               } 
  10.             } 

5.根據是否需要填0選項,對分配空間的對象資料區進行填0

  1. if (result != NULL) { 
  2.               // Initialize object (if nonzero size and need) and then the header 
  3.               if (need_zero ) { 
  4.                 HeapWord* to_zero = (HeapWord*) result + sizeof(oopDesc) / oopSize; 
  5.                 obj_size -= sizeof(oopDesc) / oopSize; 
  6.                 if (obj_size > 0 ) { 
  7.                   memset(to_zero, 0, obj_size * HeapWordSize); 
  8.                 } 
  9.               } 

6.根據是否使用偏向鎖,設定對象頭資訊,然後設定對象的klassOop引用(這樣對象本身就擷取了擷取類型資料的途徑)

  1. if (UseBiasedLocking) { 
  2.                 result->set_mark(ik->prototype_header()); 
  3.               } else { 
  4.                 result->set_mark(markOopDesc::prototype()); 
  5.               } 
  6.               result->set_klass_gap(0); 
  7.               result->set_klass(k_entry); 

7.把對象地址引入棧,並繼續執行下一個位元組碼

  1. SET_STACK_OBJECT(result, 0); 
  2.               UPDATE_PC_AND_TOS_AND_CONTINUE(3, 1); 

8.若該類型沒有被解析,就會調用InterpreterRuntime的_new函數完成慢速分配

  1. // Slow case allocation 
  2.         CALL_VM(InterpreterRuntime::_new(THREAD, METHOD->constants(), index), 
  3.                 handle_exception); 
  4.         SET_STACK_OBJECT(THREAD->vm_result(), 0); 
  5.         THREAD->set_vm_result(NULL); 
  6.         UPDATE_PC_AND_TOS_AND_CONTINUE(3, 1); 

以上就是快速分配的過程,其流程圖如下,關鍵在於快速分配在Eden區所使用的無鎖指標碰撞技術 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.