Java - JVM: 读书笔记 Chapter 05 The Java Virtual Machine ( Part III The Heap )

5.7 The Heap

 

  * There is only one heap inside a Java Virtual Machine instance, all threads share it. And a Java application has its own exclusive Java Virtual Machine instance, so there is a separate heap for every individual running application. And there is no way two different Java applications could trample on each other's heap data.

 

  * The Java Virtual Machine has the instruction that allocates memory on the heap for a new object, but has no instruction for freeing that memory. Just that you can not explicity free an Object in Java Source code. and Java Virtual Machine itself is responsible for deciding whether and when to free the memory.

 

5.7.1 Garbage Collection

  1. The primary function is to automatically reclaim the memory used by objects that are no longer referenced by the running application. It may also move objects as the application runs to reduce heap fragmentation.

  2. A garbage collector is not strictly required by the Java Virtual Machine specification. The specification only requires that an implementation manage its own heap in some manner.  It does not say how an implementation must manage its heap. It says to implementation designers only that the program will be allocating memory from the heap, but not freeing it. It is up to designers to figure out how they want to deal with that fact. So the typical solution if the Memory space has filled up is to throw out the OutOfMemory exception, so this may not win many prizes.

 

5.7.2 Object Representation

 

  The Goal :

 

  The primary data that must in some way be represented for each object is the instance variables declared in the objectís class and all its superclasses. So given an object reference, there must be some to achieve the following

  a. The virtual machine must be able to quickly locate the instance data for the object.

  b. The virtual machine must be able to access the object's class data (stored in the method area)

  For this reason, the memory allocated for an object usually includes some kind of pointer into the method area  

  

  How to design Heap : 

  #1 Heap design: divides the heap into two parts: a handle pool and an object pool.

 

  * An object reference is a native pointer to a handle pool entry.

  * The handle pool contains two components: a pointer to instance data in the object pool, and a pointer to class data in the method area.

  - The advantage as below,

  a. It makes it easy for the virtual machine to combat heap fragmentation.  

  b. When the virtual machine moves an object in the object pool, it need only update one pointer with the object's new address: the relevant pointer in the handle pool.

  - The disadvantage is that every access to an objectís instance data requires dereferencing two pointers.

  ( Comments: Object is represent the Class class or a Class instance data - Suppose that if we have define a Car class and also if we have new an Car instance 'Bmw1' from the Car class, the both defined are called Object. )

 

  #2 Heap design: an object reference a native pointer to a bundle of data that contains the object's instance data and a pointer to the objectís class data.   

  This approach requires dereferencing only one pointer to access an object's instance data, but makes moving objects more complicated. When the virtual machine moves an object to combat fragmentation of this kind of heap, it must update every reference to that object anywhere in the runtime data areas.   

 

  Why an Object reference must reference to its object class data? reason below,

 

  - Cast an object: When a running program attempts to cast an object reference to another type, the virtual machine must check to see if the type being cast to is the actual class of the referenced object or one of its supertypes

  - instanceof operation

  - Invoke an instance method: when a programs invokes an instance method, the virtual machine must perform dynamic binding: it must choose the method to invoke based not on the type of the reference but on the class of the object. To do this, it must once again have access to the class data given only a reference to the object.

   

  Method Table

 

  1) speed up the invocation of instance methods

  2) play an important role in achieving good overall performance for a virtual machine implementation

  3) Method tables are not required by the Java Virtual Machine specification and may not exist in all implementations

  If an implementation does use method tables, however, an object's method table will likely be quickly accessible given just a reference to the object.

 

  One way an implementation could connect a method table to an object reference as below

 
 
  The pointer associated with the instance data for each object points to a special structure, and the special structure has two components:

 

  A pointer to the full class data for the object

  * The method table for the object ( the table contains prt to method data )

 

  The method table is an array of pointers to the data for each instance method that can be invoked on objects of that class;

  The method data pointed to by method table include:

 

  * The sizes of the operand stack and local variables sections of the method's stack

  * The method's bytecodes

  * An exception table

  

  This gives the virtual machine enough information to invoke the method. The method table include pointers to data for methods declared explicitly in the objectís class or inherited from superclasses. In other words, the pointers in the method table may point to methods defined in the objectís class or any of its superclasses

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值