Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

JVM Internals (2015)

1,086 views

Published on

A brief explanation on how the JVM loads and execute its code is done here. Register and stack based execution are explained, and different garbage collection algorithms are shown using graphs to make it easy to understand what happens under the hood. This talk uses the Java Virtual Machine as a main example, but most of the concepts extends to any modern virtual machine available today.

Published in: Technology
  • Be the first to comment

JVM Internals (2015)

  1. 1. JVM INTERNALS Introduction toVirtual Machines and the JVM 1 LuizTeston www.fracta.cc
  2. 2. This presentation is available online at fracta.cc/presentations/jvm 2
  3. 3. VIRTUAL MACHINES 3
  4. 4. Mimics a Real Machine 4
  5. 5. Loads and execute code 5
  6. 6. CLASSLOADERS 6
  7. 7. Java classes are loaded on demand 7
  8. 8. How they are loaded is up to the classloader 8
  9. 9. 9
  10. 10. 10
  11. 11. Classloaders are hierarchical 11
  12. 12. 12
  13. 13. 13
  14. 14. boor jars LibraryA.jar LibraryB.jar 14
  15. 15. boor jars LibraryA.jar LibraryB.jar Load class “Main” 15
  16. 16. boor jars LibraryA.jar LibraryB.jar Load class “Main” Not Here 16
  17. 17. boor jars LibraryA.jar LibraryB.jar Load class “Main” Not Here Not Here 17
  18. 18. boor jars LibraryA.jar LibraryB.jar Load class “Main” Not Here Not Here Wait… 18
  19. 19. boor jars LibraryA.jar LibraryB.jar Load class “Main” Not Here Not Here Not Here run time generated code 19
  20. 20. boor jars LibraryA.jar LibraryB.jar Load class “Main” Not Here Not Here Not Here run time generated code Here! 20
  21. 21. HOWTO EXECUTE CODE? 21
  22. 22. INTERPRETING 22
  23. 23. Line of text AST 23
  24. 24. Simple example: sum of two numbers 24
  25. 25. 25
  26. 26. int variable: j 26
  27. 27. int variable: jint variable: i 27
  28. 28. int variable: jint variable: i sum 28
  29. 29. int variable: jint variable: i sum return 29
  30. 30. Errors usually are caught at runtime 30
  31. 31. COMPILING 31
  32. 32. Code compiled to binary prior to the execution. 32
  33. 33. Some errors can be caught at compile time 33
  34. 34. Binary can be: native code,VM bytecode and so on… 34
  35. 35. Bytecode: Byte sized OPCODE 35
  36. 36. REGISTER BASEDVM 36
  37. 37. Works like your processor 37
  38. 38. Simple example: summing two numbers 38
  39. 39. R1 R2 RN… 39
  40. 40. R1 R2 RN… 1 40
  41. 41. R1 R2 RN… 1 +2 41
  42. 42. R1 R2 RN… 3 42
  43. 43. STACK BASEDVM 43
  44. 44. Works like you HP48G calculator 44
  45. 45. 45
  46. 46. 1 46
  47. 47. 1 2 47
  48. 48. 1 2 + 48
  49. 49. 3 49
  50. 50. JVM IS STACK BASED 50
  51. 51. Eventually byte code is compiled to native code on the fly 51
  52. 52. DYNAMIC MEMORY 52
  53. 53. C/C++ Approach: memory as a big array 53
  54. 54. index size variable 54
  55. 55. index size variable 1 1 i 55
  56. 56. index size variable 1 1 i 2 2 l 56
  57. 57. index size variable 1 1 i 2 2 l 3 4 c 57
  58. 58. index size variable 1 1 i 3 4 c 58
  59. 59. Few caveats for this approach 59
  60. 60. Possible memory fragmentation 60
  61. 61. Possible memory fragmentation 4 sized var doesn’t fit 61
  62. 62. Possible memory leak 62
  63. 63. Possible memory leak used vars unused vars 63
  64. 64. Possible invalid pointer 64
  65. 65. Possible invalid pointer variable pointing here 65
  66. 66. JVM based approach: Garbage Collector 66
  67. 67. Eden Survivor 1 Survivor 2 GC 67
  68. 68. Eden Survivor 1 Survivor 2 GC 68
  69. 69. Eden Survivor 1 Survivor 2 Wait…GC 69
  70. 70. Eden Survivor 1 Survivor 2 off you goGC 70
  71. 71. Eden Survivor 1 Survivor 2 GC t 71
  72. 72. Eden Survivor 1 Survivor 2 GC t ? 72
  73. 73. Eden Survivor 1 Survivor 2 GC t ? Wait… 73
  74. 74. Eden Survivor 1 Survivor 2 GC ? t Wait… 74
  75. 75. Eden Survivor 1 Survivor 2 GC ? t off you go 75
  76. 76. Eden Survivor 1 Survivor 2 GC ? t t2 76
  77. 77. Eden Survivor 1 Survivor 2 GC ? t t2 t is still used within t2 77
  78. 78. Eden Survivor 1 Survivor 2 GC ? t t2 ? is not used… 78
  79. 79. Eden Survivor 1 Survivor 2 GC ? t t2 Whatever, I have plenty of memory 79
  80. 80. Few caveats for this approach 80
  81. 81. GC Wait… GC needs to be properly configured 81
  82. 82. GC … GC can be unpredictable 82
  83. 83. GC No control over memory layout 83
  84. 84. GC No control over memory layout Let me do this job! 84
  85. 85. GC Managing memory is hard! 85
  86. 86. GC Having a GC doing the hard work is good. 86
  87. 87. HANDS ON 87
  88. 88. • Create a .java file • Compile it into a .class file • Analyse its binary content
  89. 89. 89
  90. 90. 90
  91. 91. 91
  92. 92. 92 magic number
  93. 93. 93 Source code info (can be removed by compilation args)
  94. 94. 94 Optimisations?
  95. 95. 95 Yes, we appended to a string, but strings are immutable in Java.
  96. 96. 96 the addTimes method
  97. 97. Time to take a look on the JVM Assembly code 97
  98. 98. 98
  99. 99. 99
  100. 100. 100 main method
  101. 101. 101 arg to int into a variable
  102. 102. 102 new SumArg1Arg2Times
  103. 103. 103 StringBuilder optimisation
  104. 104. 104
  105. 105. 105 Constructor
  106. 106. 106 can you see the loop?
  107. 107. 107 can you see the loop?
  108. 108. 108 add method changing a field
  109. 109. Going further, let’s look the native assembly code 109
  110. 110. • Google for java dissablembler plugin. Install it (hdis-i386 or hdis-amd64) • See the native assembly output • Enjoy analysing it
  111. 111. large iteration count, so it can be JIT compiled
  112. 112. native code for java.lang.Object constructor
  113. 113. native code for addTimes
  114. 114. related to the loop within addTimes
  115. 115. native assembly
  116. 116. related bytecode
  117. 117. native code for add method
  118. 118. bytecode for add method
  119. 119. QUESTIONS?
  120. 120. WHO AM I Luiz Teston Principle Engineer, CTO at FRACTA • 15 years on the field, working on non trivial projects using Java, C++ and functional programming • lteston@fracta.cc • http://fracta.cc • http://linkedin.com/in/teston • http://twitter.com/FeuTeston
  121. 121. KEEP INTOUCH Feedback about the presentation appreciated. lteston@fracta.cc www.fracta.cc
  122. 122. REFERENCES • Garbage Collection:Algorithms for Automatic Dynamic Memory Management, by Richard Jones and Rafael Lins. • Virtual Machines:Versatile Platforms for Systems and Processes, by Jim Smith and Ravi Nair. • https://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html • http://www.ibm.com/developerworks/ibm/library/it-haggar_bytecode/ • http://docs.oracle.com/javase/specs/jvms/se8/html/index.html • http://mechanical-sympathy.blogspot.ie/2013/06/printing-generated-assembly-code- from.html • http://www.slideshare.net/CharlesNutter/redev-2011-jvm-jit-for-dummies-what-the-jvm- does-with-your-bytecode-when-youre-not-looking 126

×