Slide 1

Slide 1 text

Managed Runtime Systems The Java Virtual Machine Foivos Zakkak https://foivos.zakkak.net Except where otherwise noted, this presentation is licensed under the Creative Commons Attribution 4.0 International License. Third party marks and brands are the property of their respective holders.

Slide 2

Slide 2 text

Managed Runtime Systems CC-BY https://Foivos.Zakkak.net 2 The JVM architecture Data Locations Class Loader Class Loader Execution Engine Execution Engine Stack Area Stack Area PC register PC register Native Stack Native Stack Heap Area Heap Area Method Area Method Area Native Methods Interface Native Methods Interface Native Libraries Native Libraries .java .java .class .class javac java

Slide 3

Slide 3 text

Managed Runtime Systems CC-BY https://Foivos.Zakkak.net 3 Class Loading 1) Verification 2) Preparation of JVM internal data structures 3) Resolution of references (might load other classes) 4) Initialization

Slide 4

Slide 4 text

Managed Runtime Systems CC-BY https://Foivos.Zakkak.net 4 Java Class File ClassFile { u4 magic; u2 minor_version; u2 major_version; u2 constant_pool_count; cp_info constant_pool[constant_pool_count-1]; u2 access_flags; u2 this_class; u2 super_class; u2 interfaces_count; u2 interfaces[interfaces_count]; u2 fields_count; field_info fields[fields_count]; u2 methods_count; method_info methods[methods_count]; u2 attributes_count; attribute_info attributes[attributes_count]; } ClassFile { u4 magic; u2 minor_version; u2 major_version; u2 constant_pool_count; cp_info constant_pool[constant_pool_count-1]; u2 access_flags; u2 this_class; u2 super_class; u2 interfaces_count; u2 interfaces[interfaces_count]; u2 fields_count; field_info fields[fields_count]; u2 methods_count; method_info methods[methods_count]; u2 attributes_count; attribute_info attributes[attributes_count]; }

Slide 5

Slide 5 text

Managed Runtime Systems CC-BY https://Foivos.Zakkak.net 5 Field Info field_info { u2 access_flags; // public, private, protected, static, final, // volatile, transient, synthetic, enum u2 name_index; u2 descriptor_index; u2 attributes_count; attribute_info attributes[attributes_count]; // e.g., Deprecated } field_info { u2 access_flags; // public, private, protected, static, final, // volatile, transient, synthetic, enum u2 name_index; u2 descriptor_index; u2 attributes_count; attribute_info attributes[attributes_count]; // e.g., Deprecated }

Slide 6

Slide 6 text

Managed Runtime Systems CC-BY https://Foivos.Zakkak.net 6 Java fields ■ Reference (Size is implementation-dependent) ■ Primitive – 8, 16, 32, or 64-bit Integer (byte, short, int, long, boolean, char) – 32 or 64-bit IEEE 754 floating-point (float, double)

Slide 7

Slide 7 text

Managed Runtime Systems CC-BY https://Foivos.Zakkak.net 7 Field Descriptors Abbreviation Type Example B byte C char D double F float I int J long L ClassName; reference Ljava/lang/String; S short Z boolean [ array [I [[[D [Ljava/lang/Object;

Slide 8

Slide 8 text

Managed Runtime Systems CC-BY https://Foivos.Zakkak.net 8 Method Info Method Descriptors: method_info { u2 access_flags; // public, private, native, var args, synthetic ... u2 name_index; u2 descriptor_index; u2 attributes_count; attribute_info attributes[attributes_count]; // e.g., Code, Exceptions } method_info { u2 access_flags; // public, private, native, var args, synthetic ... u2 name_index; u2 descriptor_index; u2 attributes_count; attribute_info attributes[attributes_count]; // e.g., Code, Exceptions } void foo(int a, Object o) => (ILjava/lang/Object;)V String bar(int[][] i) => ([[I)Ljava/lang/String;

Slide 9

Slide 9 text

Managed Runtime Systems CC-BY https://Foivos.Zakkak.net 9 Code attribute Code_attribute { u2 attribute_name_index; u4 attribute_length; u2 max_stack; u2 max_locals; u4 code_length; u1 code[code_length]; // The actual bytecode u2 exception_table_length; { u2 start_pc; // if catch_type is thrown between start_pc and end_pc, u2 end_pc; // execute handler at handler_pc u2 handler_pc; u2 catch_type } exception_table[exception_table_length]; u2 attributes_count; attribute_info attributes[attributes_count]; // line numbers, stack maps ... } Code_attribute { u2 attribute_name_index; u4 attribute_length; u2 max_stack; u2 max_locals; u4 code_length; u1 code[code_length]; // The actual bytecode u2 exception_table_length; { u2 start_pc; // if catch_type is thrown between start_pc and end_pc, u2 end_pc; // execute handler at handler_pc u2 handler_pc; u2 catch_type } exception_table[exception_table_length]; u2 attributes_count; attribute_info attributes[attributes_count]; // line numbers, stack maps ... }

Slide 10

Slide 10 text

Managed Runtime Systems CC-BY https://Foivos.Zakkak.net 10 Java Stack vs Native Stack Java Stack frame frame frame Frame operand stack 2 operand stack 1 operand stack 0 local variable n . . . local variable 1 local variable 0 return address

Slide 11

Slide 11 text

Managed Runtime Systems CC-BY https://Foivos.Zakkak.net 11 Java Threads ■ A Program Counter – Class – Method – bytecode-offset or native code pointer ■ A Java Stack ■ Native Stack

Slide 12

Slide 12 text

Managed Runtime Systems CC-BY https://Foivos.Zakkak.net 12 Java Objects and Arrays Object Array header field 0 field 1 . . . field n header length element 0 element 1 . . . element n