Java Virtual Machine, Call stack, Java Byte Code

Download Java Virtual Machine, Call stack, Java Byte Code

Post on 22-Apr-2015

1.276 views

Category:

Software

6 download

Embed Size (px)

DESCRIPTION

Java Virtual Machine , method call stack, java byte code opcode .

TRANSCRIPT

<ul><li> 1. JVM Call Stack </li> <li> 2. Java Virtual Machine </li> <li> 3. JVM </li> <li> 4. Java Virtual Machine Class Loader System Operating System Java Threads Execution Engine Run-time Data Area Garbage Collector Java Application Java API Native Method Libraries </li> <li> 5. ByteCode Link Verify Prepare Class Load Class Loader Resolve Initialize ByteCode Class File Memory </li> <li> 6. Java API Execution Class Loader Engine Java Class System Runtime Data Areas PC Registers JVM Stacks Files Native Method Libraries Method Area Heap Native Method Stacks JVM Class Loader Byte Code Runtime Data Areas Method Area Load. </li> <li> 7. Execution Engine Class Loader System Operating System Java Threads Execution Engine Run-time Data Area Garbage Collector Java Application Java API Native Method Libraries </li> <li> 8. Java API Execution Class Loader Engine Java Class System Runtime Data Areas PC Registers JVM Stacks Files Native Method Libraries Method Area Heap Native Method Stacks JVM Execution Engine Method Area Load Byte Code . Execution Engine ByteCode (interpreting ). </li> <li> 9. Method Area Thread . Class(Type), Method, Field . . </li> <li> 10. Method Area Thread . Method Area Type Information : Constant Pool : , Symbolic Reference Field Information : Field , Data Type, Modifier Method Information : Method , DataType, Modifier Class Variables : static Reference to Class Class Loader : Type Load ClassLoader Reference to Class class Method Table : Class Method Direct Reference . </li> <li> 11. Java API Execution Class Loader Engine Java Class System Runtime Data Areas PC Registers JVM Stacks Files Native Method Libraries Method Area Heap Native Method Stacks Method Area Byte Code JVM Stacks, Native Method Stacks . </li> <li> 12. JVM Stacks, Native Method Stacks Thread . Thread . Thread method . </li> <li> 13. Java API Execution Class Loader Engine Java Class System Runtime Data Areas PC Registers JVM Stacks Files Native Method Libraries Method Area Heap Native Method Stacks JVM Heap Array . </li> <li> 14. Method Area Thread A ( A) JVM Stacks main() Stack frame Operand Stack Local Variable 0 args constant pool JVM Stack . JVM Stack Stack Frame . JVM Stacks main() Stack frame Operand Stack Local Variable 0 args JVM Heap Thread B ( B) </li> <li> 15. JVM Heap Method Area Thread . Method Area constant pool JVM Heap Thread A ( A) Thread B ( B) </li> <li> 16. Method Area ByteCode . Thread JVM Heap . Method Area constant pool JVM Heap Thread A ( A) Thread B ( B) </li> <li> 17. Heap Thread . JVM . JVM Memory , (Garbage Collection) Heap . </li> <li> 18. package net.slipp; public class Adder { int add(int i, int j) { return i + j; } public static void main(String[] args) { Adder adder = new Adder(); int result = adder.add(5,2); System.out.println(result); } } </li> <li> 19. package net.slipp; public class Adder { int add(int i, int j) { return i + j; } public static void main(String[] args) { Adder adder = new Adder(); int result = adder.add(5,2); System.out.println(result); } } JVM Stacks main() Stack frame Operand Stack Local Variable 0 args 1. main method stack frame </li> <li> 20. package net.slipp; public class Adder { int add(int i, int j) { return i + j; } public static void main(String[] args) { Adder adder = new Adder(); int result = adder.add(5,2); System.out.println(result); } } JVM Stacks main() Stack frame Operand Stack Local Variable 0 args 1. main method stack frame 2. Adder stack frame Adder Stack frame Operand Stack Local Variable 0 this </li> <li> 21. package net.slipp; public class Adder { int add(int i, int j) { return i + j; } public static void main(String[] args) { Adder adder = new Adder(); int result = adder.add(5,2); System.out.println(result); } } JVM Stacks main() Stack frame Operand Stack Local Variable 0 args 1. main method stack frame 2. Adder stack frame 3. Adder stack frame </li> <li> 22. JVM Stacks main() Stack frame Operand Stack Local Variable 0 args package net.slipp; public class Adder { int add(int i, int j) { return i + j; } public static void main(String[] args) { Adder adder = new Adder(); int result = adder.add(5,2); System.out.println(result); } 1. main method stack frame 2. Adder stack frame 3. Adder stack frame 4. add method stack frame } add method Stack frame Operand Stack Local Variable 2 2 1 5 0 this </li> <li> 23. JVM Stacks main() Stack frame Operand Stack Local Variable 0 args package net.slipp; public class Adder { int add(int i, int j) { return i + j; } public static void main(String[] args) { Adder adder = new Adder(); int result = adder.add(5,2); System.out.println(result); } 1. main method stack frame 2. Adder stack frame 3. Adder stack frame 4. add method stack frame 5. add method stack frame } </li> <li> 24. JVM Stacks package net.slipp; public class Adder { int add(int i, int j) { return i + j; } public static void main(String[] args) { Adder adder = new Adder(); int result = adder.add(5,2); System.out.println(result); } 1. main method stack frame 2. Adder stack frame 3. Adder stack frame 4. add method stack frame 5. add method stack frame 6. main method stack frame } </li> <li> 25. </li> <li> 26. Execution Engine . </li> <li> 27. public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } javap c HelloWorld.class } D:next-workspaceworkspacejavabin&gt;javap -c HelloWorld.class Compiled from "HelloWorld.java" public class HelloWorld { public HelloWorld(); Code: 0: aload_0 1: invokespecial #8 // Method java/lang/Object."":()V 4: return public static void main(java.lang.String[]); Code: 0: getstatic #16 // Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc #22 // String Hello World! 5: invokevirtual #24 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 8: return } </li> <li> 28. D:next-workspaceworkspacejavabin&gt;javap -verbose HelloWorld.class Classfile /D:/next-workspace/workspace/java/bin/HelloWorld.class Last modified 2013. 3. 20; size 534 bytes MD5 checksum 77129a8bac38a3ac8dc870dd7362668c Compiled from "HelloWorld.java" public class HelloWorld SourceFile: "HelloWorld.java" minor version: 0 major version: 51 flags: ACC_PUBLIC, ACC_SUPER Constant pool: javap verbose HelloWorld.class #1 = Class #2 // HelloWorld #2 = Utf8 HelloWorld #3 = Class #4 // java/lang/Object #4 = Utf8 java/lang/Object #5 = Utf8 #6 = Utf8 ()V #7 = Utf8 Code #8 = Methodref #3.#9 // java/lang/Object."":()V #9 = NameAndType #5:#6 // "":()V #10 = Utf8 LineNumberTable #11 = Utf8 LocalVariableTable #12 = Utf8 this #13 = Utf8 LHelloWorld; #14 = Utf8 main #15 = Utf8 ([Ljava/lang/String;)V #16 = Fieldref #17.#19 // java/lang/System.out:Ljava/io/PrintStream; #17 = Class #18 // java/lang/System #18 = Utf8 java/lang/System #19 = NameAndType #20:#21 // out:Ljava/io/PrintStream; #20 = Utf8 out #21 = Utf8 Ljava/io/PrintStream; #22 = String #23 // Hello World! #23 = Utf8 Hello World! #24 = Methodref #25.#27 // java/io/PrintStream.println:(Ljava/lang/String;)V #25 = Class #26 // java/io/PrintStream #26 = Utf8 java/io/PrintStream #27 = NameAndType #28:#29 // println:(Ljava/lang/String;)V #28 = Utf8 println #29 = Utf8 (Ljava/lang/String;)V #30 = Utf8 args #31 = Utf8 [Ljava/lang/String; #32 = Utf8 SourceFile #33 = Utf8 HelloWorld.java </li> <li> 29. { public HelloWorld(); flags: ACC_PUBLIC Code: stack=1, locals=1, args_size=1 0: aload_0 1: invokespecial #8 // Method java/lang/Object."":()V 4: return LineNumberTable: line 1: 0 LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LHelloWorld; public static void main(java.lang.String[]); flags: ACC_PUBLIC, ACC_STATIC Code: stack=2, locals=1, args_size=1 0: getstatic #16 // Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc #22 // String Hello World! 5: invokevirtual #24 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 8: return LineNumberTable: line 3: 0 line 4: 8 LocalVariableTable: Start Length Slot Name Signature 0 9 0 args [Ljava/lang/String; } </li> <li> 30. Bytecode Visualizer Eclipse (http://www.slipp.net/questions/292) </li> <li> 31. Java opcode(bytecode instruction) http://en.wikipedia.org/wiki/Java_bytecode_instruction_listings </li> <li> 32. Prefix/ Suffix Operand Type i integer l long s short b byte c character f float d double z boolean a reference </li> <li> 33. JVM Stacks add method Stack frame Operand Stack Local Variable 2 2 1 5 0 this load opcode Local Variable Operand Stack (instruction) JVM Stacks add method Stack frame Operand Stack Local Variable 2 2 1 5 0 this iload_1 0 5 </li> <li> 34. JVM Stacks main method Stack frame Operand Stack Local Variable 1 adder 0 this store opcode Operand Stack Local Variable (instruction) istore_2 0 7 JVM Stacks main method Stack frame Operand Stack Local Variable 2 7 1 adder 0 this </li> <li> 35. invoke* opcode : , method opcode invokestatic : (static) method invokeinterface : interface method invokespecial : method( X) Invokevirtual : method ( O) </li> <li> 36. Case 1 public class StringConcatenations { public String concat1(String start, String end) { return start + end; } public void concat2(StringBuffer start, String end) { start.append(end); } } </li> <li> 37. Case 2 public class StringLiterals { public static void main(String[] args) { String one = "Test"; String two = "Test"; String three = "T" + "e" + "s" + "t"; String four = new String("Test"); } } </li> <li> 38....</li></ul>