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 - CorkJUG

312 views

Published on

Presentation file used for the CorkJUG JVM Internals talk, done on 14/10/2015

Published in: Technology
  • Be the first to comment

Jvm internals 2015 - CorkJUG

  1. 1. JVM INTERNALS Introduction toVirtual Machines and the JVM 1 LuizTeston www.fracta.cc www.corkjug.com
  2. 2. 1st meetup in14/10 corkjug.com
  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. boot jars LibraryA.jar LibraryB.jar 14
  15. 15. boot jars LibraryA.jar LibraryB.jar Load class “Main” 15
  16. 16. boot jars LibraryA.jar LibraryB.jar Load class “Main” Not Here 16
  17. 17. boot jars LibraryA.jar LibraryB.jar Load class “Main” Not Here Not Here 17
  18. 18. boot jars LibraryA.jar LibraryB.jar Load class “Main” Not Here Not Here Wait… 18
  19. 19. boot jars LibraryA.jar LibraryB.jar Load class “Main” Not Here Not Here Not Here run time generated code 19
  20. 20. boot 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. SIMPLE GC ALGORITHMS 67
  68. 68. MARK AND SWEEP 68
  69. 69. 69 VISIBLE REF
  70. 70. 70
  71. 71. 71
  72. 72. 72
  73. 73. COPY 73
  74. 74. 74 VISIBLE REF
  75. 75. 75
  76. 76. 76
  77. 77. 77
  78. 78. 78
  79. 79. 79
  80. 80. IN PRACTICE 80
  81. 81. Eden Survivor 1 Survivor 2 GC 81
  82. 82. Eden Survivor 1 Survivor 2 GC 82
  83. 83. Eden Survivor 1 Survivor 2 Wait…GC 83
  84. 84. Eden Survivor 1 Survivor 2 off you goGC 84
  85. 85. Eden Survivor 1 Survivor 2 GC t 85
  86. 86. Eden Survivor 1 Survivor 2 GC t ? 86
  87. 87. Eden Survivor 1 Survivor 2 GC t ? Wait… 87
  88. 88. Eden Survivor 1 Survivor 2 GC ? t Wait… 88
  89. 89. Eden Survivor 1 Survivor 2 GC ? t off you go 89
  90. 90. Eden Survivor 1 Survivor 2 GC ? t t2 90
  91. 91. Eden Survivor 1 Survivor 2 GC ? t t2 t is still used within t2 91
  92. 92. Eden Survivor 1 Survivor 2 GC ? t t2 ? is not used… 92
  93. 93. Eden Survivor 1 Survivor 2 GC ? t t2 Whatever, I have plenty of memory 93
  94. 94. Few caveats for this approach 94
  95. 95. GC Wait… GC needs to be properly configured 95
  96. 96. GC … GC can be unpredictable 96
  97. 97. GC No control over memory layout 97
  98. 98. GC No control over memory layout Let me do this job! 98
  99. 99. GC Managing memory is hard! 99
  100. 100. GC Having a GC doing the hard work is good. 100
  101. 101. WHAT ABOUT PERM GEN? 101
  102. 102. 102 @Deprecated
  103. 103. HANDS ON 103
  104. 104. • Create a .java file • Compile it into a .class file • Analyse its binary content
  105. 105. 105
  106. 106. 106
  107. 107. 107
  108. 108. 108 magic number
  109. 109. 109 Source code info (can be removed by compilation args)
  110. 110. 110 Optimisations?
  111. 111. 111 Yes, we appended to a string, but strings are immutable in Java.
  112. 112. 112 the addTimes method
  113. 113. Time to take a look on the JVM Assembly code 113
  114. 114. 114
  115. 115. 115
  116. 116. 116 main method
  117. 117. 117 arg to int into a variable
  118. 118. 118 new SumArg1Arg2Times
  119. 119. 119 StringBuilder optimisation
  120. 120. 120
  121. 121. 121 Constructor
  122. 122. 122 can you see the loop?
  123. 123. 123 can you see the loop?
  124. 124. 124 add method changing a field
  125. 125. Going further, let’s look the native assembly code 125
  126. 126. • Google for java dissablembler plugin. Install it (hdis-i386 or hdis-amd64) • See the native assembly output • Enjoy analysing it
  127. 127. large iteration count, so it can be JIT compiled
  128. 128. native code for java.lang.Object constructor
  129. 129. native code for addTimes
  130. 130. related to the loop within addTimes
  131. 131. native assembly
  132. 132. related bytecode
  133. 133. native code for add method
  134. 134. bytecode for add method
  135. 135. QUESTIONS?
  136. 136. WHO AM I Luiz Teston • 15 years on the field mostly as a consultant, 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
  137. 137. KEEP INTOUCH Feedback about the presentation appreciated. lteston@fracta.cc www.fracta.cc
  138. 138. 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 142

×