Java - JVM: 读书笔记 Chapter 05 The Java Virtual Machine ( Part II Method Area )

Tutorial Materials: Inside Java Machine

5.6. The Method Area

 

  How Information loaded / What information is kept in Method area?

 

  1). Information about loaded types is stored in a logical area of memory called the method area.  When the Java Virtual Machine loads a type, it uses a class loader to locate the appropriate class file.
  2). The class loader reads in the class file--a linear stream of binary data--and passes it to the virtual machine. The virtual machine extracts information about the type from the binary data and stores the information in the method area.

  3). Memory for class (static) variables declared in the class is also taken from the method area.

 

  Key responsibilies,

 

  * The virtual machine will search through and use the type information stored in the method area as it executes the application it is hosting. 

  * All threads share the same method area, so access to the method areaís data structures must be designed to be thread-safe. 
  * The size of the method area need not be fixed. As the Java application runs, the virtual machine can expand and contract the method area to fit the applicationís needs.  Implementations may allow users or programmers to specify an initial size for the method area, as well as a maximum or minimum size.
  * The method area can also be garbage collected. Because Java programs can be dynamically extended via class loader objects, classes can become "unreferenced" by the application. If a class becomes unreferenced, a Java Virtual Machine can unload the class (garbage collect it) to keep the memory occupied by the method area at a minimum. ( Chapter 7 will describe how the Class Loader Objects becames "un-referenced" )

 

5.6.1 Type Information

 

  * The fully qualified name of the type

  * The fully qualified name of the typeís direct superclass (unless the type is an interface or class java.lang.Object, neither of which have a superclass)

  * Whether or not the type is a class or an interface

  * The typeís modifiers ( some subset of` public, abstract, final)

  * An ordered list of the fully qualified names of any direct superinterfaces

 

  Inside the Java class file and Java Virtual Machine, type names are always stored as fully qualified names. ( Like java/lang/Object )

 

 In addition to the basic type information listed above, the virtual machine must also store for each loaded type:

   

  * The constant pool for the type

  * Field information

  * Method information

  * All class (static) variables declared in the type, except constants

  * A reference to class ClassLoader

  * A reference to class Class

 

5.6.2 The Constant Pool

 

  * For each type it loads, a Java Virtual Machine must store a constant pool for it.
  A constant pool is an ordered set of constants used by the type, including literals (string, integer, and floating point constants) and symbolic references to types, fields, and methods.

 

5.6.3 Field Information

 

  For each field declared in the type, the following information must be stored in the method area.

  

  * The fieldís name

  * The fieldís type

  * The fieldís modifiers (some subset of public, private, protected, static, final, volatile, transient)

 

  In addition to the information for each field, the order in which the fields are declared by the class or interface must also be recorded.

 

5.6.4 Method Information

 

  For each method declared in the type, the following information must be stored in the method area.

  * The methodís name

  * The methodís return type (or void)

  * The number and types (in order) of the methodís parameters

  * The methodís modifiers (some subset of public, private, protected, static, final, synchronized, native, abstract)

 

  the order in which the methods are declared by the class or interface must be recorded as well as the data.

  

  In addition to the items listed above, the following information must also be stored with each method that is not abstract or native:

  * The methodís bytecodes

  * The sizes of the operand stack and local variables sections of the methodís stack frame (these are described in a later section of this chapter)

  * An exception table (this is described in Chapter 17, "Exceptions")

 

5.6.5 Class Variables

 

  Class variables are shared among all instances of a class and can be accessed even in the absence of any instance. (Actually it's the static variables without final modifier and the value can be changed during runtime ).These variables are associated with the class -- not with instances of the class -- so they are logically part of the class data in the method area. Before a Java Virtual Machine uses a class, it must allocate memory from the method area for each non-final class variable declared in the class.

 

  Constants(class variables declared final), Every type that uses a final class variable gets a copy of the constant value in its own Constant Pool.  As part of the constant pool, final class variables are stored in the method area--just like non-final class variables. But whereas non-final class variables are stored as part of the data for the type that declares them, final class variables are stored as part of the data for any type that uses them. ( that means the non-final class variables just plays as a reference name to link the value, the value can be changed; the final class variables just plays as a reference name to use the value from Constant Pool, the value can not be changed. )

 

5.6.6  A Reference to Class Loader

 

  For each type it loads, a Java Virtual Machine must keep track of whether or not the type was loaded via the primordial class loader or a class loader object. For those types loaded via a class loader object, the virtual machine must store a reference to the class loader object. This information is stored as part of the typeís data in the method area.

 

5.6.7 A Reference to Class class

 

  An instance of class java.lang.Class is created by the Java Virtual Machine for every type it loads. The virtual machine must in some way associate a reference to the Class instance for a type with the type's data in the method area.
  Comment: Class instance means Class Object, in simple name class. Class instance holds all Type information that JVM needs.
 
  There are 2 ways to get the Class Instance/Object/class.

  1)  public static Class forName(String className)
  2)  public final Class getClass()

 

  The type information can be retrieved by

  1)  public String getName();

  2)  public Class getSuperClass();

  3)  public boolean isInterface();

  4)  public Class[] getInterfaces();

  5)  public ClassLoader getClassLoader()

 

5.6.8 Method Table
 

  The type information stored in the method area must be organized to be quickly accessible.  A method table allows a virtual machine to quickly locate an instance method invoked on an object. Method tables are described in detail in Chapter 8, "The Linking Model."

 

5.6.9 An Example of Area Use

---------------------------------------------------------------------------  

  class Lava {

 

      private int speed = 5; // 5 kilometers per hour

 

      void flow() {

 

      }

  }

 

  class Volcano{

 

      public static void main(String[] args){

 

           Lava lava = new Lava();

 

           lava.flow();

 

      }

  }

--------------------------------------------------------------------------------------------

  1) Executes the first instruction of Volcano's main method ( execute main() )

      a) JVM finds and reads the binary data from the Volcano.class

      b) Extracts the definition of class Volcano from the binary data and places those information(Types) into the method area.

      c) JVM invokes the main() method by interpretting the bytecodes stored in the method area.

      d) As the virtual machie executes main(), it maintains a pointer to the Constant Pool for itself(Volcono) -- "this" pointer

 

  2) Loading Lava Class - ( execute main() )

      If we have the Java Virtual Machine in an implementation-dependent manner. That's means the JVM loads classes only as it needs them.

      a) Uses its pointer Volcono's pointer into Valcono's Constant Pool to look up entry one and finds a symbolic reference to class Lava to see if Lava has already been loaded or not.

          (The Symbolic reference is just a string giving the class's full qulified name "Lava" - This same mechanism can be sed by the static forName() method of class Class. which returns a Class reference given a fully qulified name)

      b) JVM discovers that it hasn't yet loaded a class named "Lava". It processed to find and read its binary data from Lava.class, extract the binary data and store them into the Method Area

      c.) Replaces the Symbolic reference in Volcano's Constant Pool , which is just the string "Lava" with a pointer to the really class data for Lava. his process of replacing symbolic references with direct references (in this case, a native pointer) is called constant pool resolution. ( Commments: At the beginning, in the Method area Volcano only records a reference named as string "Lava".  when it needs to invoke Lava, then it needs to do constant pool resolution to really reference to the Lava Class in the Method area. Be caution, this time, only Lava class is located and next step we need to initialize a new Lava Object )

      d) Allocate the memroy for new a Lava object.  It uses the pointer to Lava class of Volcano  to find out how much heap space is required by a Lava object.

      e) JVM allocates the space on the heap and initializes the instance. in this example, it initializes the vairable speed of Lava to be zero, its default initialize value.  If class Lavaís superclass, Object, has any instance variables, those are also initialized to default initial values.
 

   3) Execute the new Lava() method

      a) Initializes the speed  value to its proper initial value, 5 .  

 

   4) Invoke lava.flow()

 

   Conclusion: The first instruction of main() completes by pushing a reference to the new Lava object onto the stack. new Lava() will use the reference to invoke Java code that initializes the speed variable to its proper initial value, five.  lava.flow() will use the reference to invoke the flow() method on the referenced Lava object.

 

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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值