Understanding Java Virtual/ Machine Sachin Seth Alpha Science International Ltd. Oxford, U.K.
Contents Preface v WEM PLATFORM DEPENDENCY AND VISUALIZATION VA_ 1.1 Introduction 1.1 1.2 How Programs Are Platform Dependent 1.1 1.2.1 Windows Segmented Executable File Format 1.2 1.2.2 A.out (File Format) 1.3 1.2.3 Executable and Linking Format (ELF) 1.3 1.3 How Platform Independence is Achieved 1.5 1.3.1 Hardware Virtual Machines 1.5 1.3.2 Application Virtual Machines 1.6 1.3.3 Machine Aggregation 1.6 1.4 Java Virtual Machine 1.6 1.5 Why Byte Codes 1.7 flf^h CLASS FILE FORMAT 2.1 Introduction 2.1 2.2 Signature or Magic Byte 2.2 2.3 Major and Minor Version Number 2.2 2.4 Constant Pool Count 2.2 2.5 Constant Pool Table 2.3 2.5.1 Constant_Utf8 2.3 2.5.2 Constant lnteger and Constant_Float 2.4 2.5.3 Constant Long and Constant_Double 2.5 2.5.4 Constant_Class 2.5 2.5.5 Constant_String 2.6
VIII Contents 2.5.6 Constant_Fieldref, Constant_Methodref, Constant_InterfaceMethodref 2.6 2.5.7 ConstantNameAndType 2.6 Access Flags 2.7 This_class 2.8 superclass 2.8 2.9 Interface_count 2.10 Interfaces table 2.9 2.11 Fields Count 2.9 2.12 Field table 2.9 2.12.1 Access_Flags 2.9 2.12.2 Name Index 2.10 2.12.3 Descriptorlndex 2.10 2.12.4 Attributes_Count 2.10 2.12.5 Attributes_Table 2.10 2.13 Methods count 2.10 2.14 Method table 2.10 2.15 Attributes Count 2.11 2.16 Attributesjable 2.11 2.16.1 Attribute Information Structure 2.11 2.17 Code Attribute 2.11 2.18 Exceptions Attribute 2.13 2.19 C data structure to represent the class file 2.13 2.20 Internal Representation of descriptor 2.14 2.20.1 Field Descriptors 2.15 2.20.2 Method Descriptors 2.15 2.7 2.8 2.8 2.9 MEM DESIGNING PLATFORM INDEPENDENT LANGUAGE 3.1 3.1 Designing Your Own Platform Independent Language 3.1 mjjh IVM INTERNAL DATA STRUCTURE 4.1 Introduction 4.1 4.2 Java Virtual Machine Data Types 4.1 4.2.1 Integral Types 4.1 4.2.2 Floating Point Types 4.2 4.2.3 Return Address Type 4.2 4.2.4 Boolean Type 4.2
Contents IX 4.2.5 Reference Type 4.2 4.3 JVM Data Types Storage Area 4.2 4.4 JVM Internal Data Structures 4.3 4.4.1 Thepc Register 4.3 4.4.2 Class Heap 4.3 4.4.3 Object Heap 4.4 4.4.4 Array Heap 4.5 4.4.5 Stack Frame 4.6 4.4.6 Method Area 4.6 4.4.7 Operand Stack 4.7 IVM INSTRUCTION SET 5.1 Introduction 5.1 5.2 Virtual Machine Error 5.1 5.3 Operations Involved in Executing Instructions 5.1 5.3.1 Push and Pop on Operand Stack 5.1 5.3.2 Push and Pop on Parameter Stack 5.3 5.3.3 Creating Function Stack Frame 5.3 5.3.4 Retrieving and Storing Values in Function Stack Frame 5.3 5.3.5 Creating an Object of Class 5.5 5.3.6 Storing Field Value 5.6 5.3.7 Getting Field Value 5.9 5.3.8 Allocating Memory for Static Fields 5.10 5.3.9 Storing Static Field Values 5.11 5.3.10 Get Static Field Values 5.14 5.3.11 Creating Array 5.14 5.3.12 Creating the Muti-dimensional Array 5.15 5.3.13 Storing and Retrieving Value from Array 5.17 5.3.14 Executing a Method 5.19 5.3.15 Throw an Exception 5.24 5.3.16 Acquiring Monitor 5.26 5.3.17 Releasing Monitor 5.27 5.3.18 Constant. utf8 to String 5.27 5.4 Instruction Set 5-28 CT GENERAL ALGORITHM OF JAVA VIRTUAL MACHINES 6.1 6.1 What Happens Behind the Scene 6.1 6.1.1 Loading of Class 61
X Contents 6.1.2 Verification of Class 6.3 6.1.3 Preparation of Class 6.3 6.1.4 Resolution of Symbols 6.4 6.1.5 Initialization 6.4 6.1.6 Initialize System Class Function 6.4 6.1.7 Load and Execute Test Class Class 6.4 6.2 Changes in JVM's Internal Data Structure 6.4 UNDERSTANDING JAVA NATIVE INTERFACE 7.1 7.1 WhyJNI 7.1 7.2 JNI Interface Functions 7.1 7.3 Loading and Linking Native Library 7.2 7.4 Referencing Java Objects 7.3 7.5 JNI Types and Data Structures 7.4 7.6 JNI by Example 7.4 7.7 JNI behind the Scene 7.5 7.7.1 Calling Native Function from Java 7.5 7.7.2 Calling Java Function from Native Language 7.10 IMPLEMENTING JNI FUNCTIONS 8.1 JNI Function Table 8.1 8.2 Initializing JNI Environment 8.2 8.3 JNI Helper Functions 8.2 8.3.1 Call Method 8.2 8.3.2 Call Method AO 8.3 8.3.3 Call Static Method 0 8.3 8.3.4 Call Static Method AO 8.4 8.3.5 Release Array Elements 0 8.4 8.3.6 Add to Local Ref List 8.4 8.4 JNI Functions 8.5 8.4.1 GetVersion 8.5 8.4.2 DefineClass 8.5 8.4.3 Find Class 8.7 8.4.4 GetSuperclass 8.8 8.4.5 Is Assignable From 8.9 8.4.6 Throw 8.10 8.4.7 ThrowNew 8.11 8.4.8 Throws 8.12
Contents xi 8.4.9 Exception Describe 8.12 8.4.10 Exception Clear 8.13 8.4.11 Fatal Error 8.13 8.4.12 New Local Ref 8.13 8.4.13 New Global Ref 8.14 8.4.14 New Weak Global Ref 8.14 8.4.15 Delete Global Ref 8.15 8.4.16 Delete Local Ref 8.15 8.4.17 Delete Weak Local Ref 8.16 8.4.18 AllocObject 8.16 8.4.19 New Object, New ObjectA & New ObjectV 8.17 8.4.20 Get Object Class 8.18 8.4.21 Is Instance Of 8.19 8.4.22 Is Same Object 8.19 8.4.23 Get Field ID 8.20 8.4.24 Get<type>Field 8.21 8.4.25 Set<type>Field 8.22 8.4.26 Get Method ID 8.23 8.4.27 Call<type>Method 8.24 8.4.28 Call Nonvirtual<type>Method 8.24 8.4.29 Get Static Field ID 8.25 8.4.30 Get Static<type>Field Routines 8.26 8.4.31 Set Static<type>Field Routines 8.26 8.4.32 Get Static Method ID 8.27 8.4.33 Call Static<type>Method 8.28 8.4.34 New String 8.28 8.4.35 Get String Length 8.29 8.4.36 Get String Region 8.30 8.4.37 Get String Chars 8.31 8.4.38 Get String Critical 8.31 8.4.39 Release String Chars 8.31 8.4.40 Release String Critical 8.32 8.4.41 New String UTF 8.32 8.4.42 Get String UTF Chars 8.33 8.4.43 Get String UTF Region 8.33 8.4.44 Get String UTF Length 8.34 8.4.45 Release String UTF Chars 8.34 8.4.46 Get Array Length 8.34 8.4.47 New Object Array 8.35 8.4.48 Get Object Array Element 8.36
xii Contents 8.4.49 Set Object Array Element 8 37 8.4.50 New<PrimitiveType>Array 8.37 8.4.51 Get<PrimitiveType>ArrayElements Routines 8.38 8.4.52 Release<PrimitiveType>ArrayElements Routines 8.39 8.4.53 Get<PrimitiveType>ArrayRegion Routines 8.40 8.4.54 Set<PrimitiveType>ArrayRegion Routines 8.41 8.4.55 Register Natives 8.42 8.4.56 Unregister Natives 8.42 8.4.57 Push Local Frame 8.43 8.4.58 Pop Local Frame 8.43 8.4.59 Monitor Enter 8.44 8.4.60 Monitor Exit 8.45 8.4.61 Get Java VM 8.45 MULTITHREADING IN JAVA 9J. 9.1 Introduction 91 9.2 Multithreading in Java 9.1 9.3 Memory Model for Thread 9.1 9.4 Creating Thread in Java 9.2 9.5 Why Multithreading in Java is Platform Dependent? 9.6 9.5.1 SleepO 9.6 9.5.2 StopO 9.6 9.5.3 SuspendO 9.5.4 ResumeO 9.5.5 isalive() 9.5.6 Interrupt () 9.5.7 Join() 9.8 9.5.8 Yield() 9.8 9.5.9 SetPriorityO 9.8 9.6 Thread local storage 9.9 Volatile keyword 9.9 9.8 Thread Synchronization in Java 9.9 111 GARBAGE COLLECTION 10.1 10.1 Introduction 10.1 10.2 Drawbacks of Legacy Languages 10.1 10.3 Solution to Memory Leak in Java 10.2 10.4 How JVM Recognizes that an Object is not Being Referenced 10.3 10.5 Garbage Collection Thread 10.5