Jvm internals

1,899
-1

Published on

Brief explanation about JVM, it's bytecode mechanism, its GC and so on. The presentation has lot's of references from the web.

Published in: Technology, Education
1 Comment
8 Likes
Statistics
Notes
No Downloads
Views
Total Views
1,899
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
187
Comments
1
Likes
8
Embeds 0
No embeds

No notes for slide

Jvm internals

  1. 1. JVM InternalsHow does your preferred platform works Luiz Fernando Teston Summa feu.teston@gmail.com / @FeuTeston
  2. 2. Introduction to (J)VMs Mimics a Machine or OS Abstraction Security Isolation ...
  3. 3. Machine/OS mimicsLoads codeExecutes codeTakes care of memoryManage code for security
  4. 4. Machine/OS mimicsLoads code :: ClassloaderExecutes codeTakes care of memory :: GCManage code for security
  5. 5. Execute code. How?Register basedStack based
  6. 6. Register basedLess (byte) codeWorks like your processorMore difficult to write a compilerEasier to execute
  7. 7. Register based getvar R1, "a" getvar R2, "b" c = a + b getvar R3, "c" add R3, R1, R2
  8. 8. Stack based (JVM way)More (byte) codeWorks like your HP Calculator ;-)Easier to write a compilerDifficult to execute
  9. 9. Stack based push a getvar push ‘b c = a + b getvar add push c storevar
  10. 10. Java Binary Class Layout
  11. 11. Java Binary Class Layout 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]; }
  12. 12. Java Binary Class Layout method_info { u2 access_flags; u2 name_index; u2 descriptor_index; u2 attributes_count; attribute_info attributes[attributes_count]; }
  13. 13. Java Binary Class Layout Code_attribute { u2 attribute_name_index; u4 attribute_length; u2 max_stack; u2 max_locals; u4 code_length; u1 code[code_length]; u2 exception_table_length; { u2 start_pc; u2 end_pc; u2 handler_pc; u2 catch_type; } exception_table[exception_table_length]; u2 attributes_count; attribute_info attributes[attributes_count]; }
  14. 14. Sample sourcepublic class Sample { public int sum(int a, int b){ return a + b; }}
  15. 15. Sample bytecode Compiled from "Sample.java" public class Sample extends java.lang.Object{ public Sample();public class Sample { Code: 0: aload_0 1: invokespecial 1; //Method java/lang/ # public int sum(int a, intObject."<init>":()V b){ 4: return return a + -c Sample javap b; } public int sum(int, int); Code: 0: iload_1} 1: iload_2 2: iadd 3: ireturn }
  16. 16. Assembly,baby
  17. 17. Assembly,babyYes, we have low level stuff too!
  18. 18. Manipulating JVM Assembly
  19. 19. Stack bytecode execution?Here we are talking about the method’s code sectionUsually it is a switch statement on the opcodeThe stack grow and sink as necessaryThe opcode instruction are executed consuming andproducing stack entries
  20. 20. Creating JVM bytecodeJava JasminSource ASM JavaJython SourceJRuby ClojureScala Groovyand many others...
  21. 21. Creating JVM bytecodeJava JasminSource ASM JavaJython Source compiles 0xCAFEBABEJRuby Clojure ... (JVM bytecode)Scala Groovyand many others...
  22. 22. Creating JVM bytecodeJava JasminSource ASM JavaJython Source runtime gen 0xCAFEBABEJRuby Clojure ... (JVM bytecode)Scala Groovyand many others...
  23. 23. Many easy ways ofextending bytecodesWithout ‘brushing bytecodes’: CGLib Java Proxies JavaAssist
  24. 24. Many easy ways ofextending bytecodesWithout ‘brushing bytecodes’: CGLib Java Proxies They’re easy! JavaAssist
  25. 25. Bytecodes can be...Generated when compiling a languageGenerated at runtime during class loadingModified at runtime during class loadingExtended at runtime dynamically
  26. 26. Bytecodes can be...Generated when compiling a language }Generated at runtime during class loadingModified at runtime during class loading JVMExtended at runtime dynamically Feature
  27. 27. Loading code: Classloaders
  28. 28. ClassloadersLoads code in a lazy mannerCan do anything while loading code Create new code Manipulate existing codeHierarchalCan be isolated
  29. 29. Javadoc: classloaders are... A class loader is an object that is responsible for loading classes. Given the binary name of a class, a class loader should attempt to locate or generate data that constitutes a definition for the class. A typical strategy is to transform the name into a file name and then read a "class file" of that name from a file system.
  30. 30. How classloading(normally) worksThere’s a classloader treeFor each classloader entry: Search classes inside the classpath directories Search classes inside jar files in the classpathUntil the class definition is find, searches the childclassloaders
  31. 31. Fancy Classloadersclass FancyClassLoader extends ClassLoader { public Class findClass(String name) { byte[] b = compileOnTheFly(name + “.fancy”); return defineClass(name, b, 0, b.length); }}
  32. 32. Fancy Classloaders Easy to create, extend and use!class FancyClassLoader extends ClassLoader { public Class findClass(String name) { byte[] b = compileOnTheFly(name + “.fancy”); return defineClass(name, b, 0, b.length); }}
  33. 33. Great Classloader samplesRemote Classloader (RMI) Think for a minute about the difficulty to build this without the JVM Loads classes using network connection Makes possible to put remote invocation in a next level
  34. 34. Great Classloader samplesAppServer Classloaders per application Can isolate AppServer classes from the hosted application Isolates classes from each application Makes ease to implement hotdeploy
  35. 35. Great Classloader samplesOSGi Plugin/bundle architecture with lifecycle Isolates plugins/bundles classes really well Makes possible to have many versions of the ‘same class’ on different bundles Isolate internal classes from plugin/bundle from the ones who needs to be exported
  36. 36. Great Classloader samplesAOP (runtime) Can implement AOP on the fly (runtime) Many applications: Transaction management Logging Error tracing
  37. 37. Where the garbagecames from?
  38. 38. How to manage memory?C malloc/freeC++ new/delete
  39. 39. How to manage memory? }C malloc/free Almost the same...C++ new/delete
  40. 40. Memory fragmentationMemory as a big byte array...m[0], m[1] ..... m[MAX]
  41. 41. Memory fragmentationMemory as a big byte array...m[0], m[1] ..... m[MAX] x *x = 1, sizeof(x_type) = 2
  42. 42. Memory fragmentationMemory as a big byte array...m[0], m[1] ..... m[MAX] x y *x = 1, sizeof(x_type) = 2 *y = 1, sizeof(y_type) = 1
  43. 43. Memory fragmentationMemory as a big byte array...m[0], m[1] ..... m[MAX] x y z *x = 1, sizeof(x_type) = 2 *y = 1, sizeof(y_type) = 1 *z = 1, sizeof(z_type) = 6
  44. 44. Memory fragmentationMemory as a big byte array...m[0], m[1] ..... m[MAX] x y zEach item is a index + size.‘Mallocked’ by C’s runtime. *x = 1, sizeof(x_type) = 2 *y = 1, sizeof(y_type) = 1 *z = 1, sizeof(z_type) = 6
  45. 45. Memory fragmentationMemory as a big byte array... WARNING!m[0], m[1] ..... m[MAX] Easy to mess with x y z Easy to fragmentEach itemEasy to forgot leaks in long runs is a index + size.‘Mallocked’ by C’s runtime. *x = 1, sizeof(x_type) = 2 *y = 1, sizeof(y_type) = 1 *z = 1, sizeof(z_type) = 6
  46. 46. Garbage Collection Possible solution for memory fragmentation Memory is no longer managed by programmer Runtime takes care of fragmentation during garbage collecting
  47. 47. How GC works? From time to time it searches from unused memory When it find unused memory, it cleans it and it could defragmentize it Behavior defined by JVM runtime options + JVM implementation
  48. 48. How GC works? Parameters includes: Running in parallel Intervals per region Specifying memory sizes for each type of object eden survivor1, survivor 2 PermGen
  49. 49. Sample GC-ed environment eden x y z survivor 1 a b csurvivor 2 w k l
  50. 50. Sample GC-ed environment eden x y z survivor 1 a b csurvivor 2 w k l
  51. 51. Sample GC-ed environment eden x z survivor 1 a bsurvivor 2 k l
  52. 52. Sample GC-ed environment eden x z survivor 1 a bsurvivor 2 k l
  53. 53. GC in practiceMark and sweep
  54. 54. GC in practiceMark and sweep
  55. 55. GC in practiceMark and sweep
  56. 56. GC in practiceMark and sweep
  57. 57. GC in practiceCopying GC
  58. 58. GC in practiceCopying GC
  59. 59. GC in practiceCopying GC
  60. 60. GC in practiceCopying GC Copy
  61. 61. GC in practiceCopying GC
  62. 62. GC in practiceCopying GC
  63. 63. Questions? Thanks! feu.teston@gmail.com / @FeuTeston
  64. 64. References on the webhttp://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ClassLoader.htmlhttp://java.sun.com/developer/technicalArticles/Networking/classloaders/http://blog.osgi.org/2011/05/what-you-should-know-about-class.htmlhttp://www.theserverside.com/news/1364680/Understanding-J2EE-Application-Server-ClassLoading-Architectureshttp://www.sidhe.org/~dan/blog/archives/000189.htmlhttp://www.ibm.com/developerworks/ibm/library/it-haggar_bytecode/http://download.oracle.com/otndocs/jcp/jcfsu-0.1-prc-oth-JSpec/http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html#jvms-2.11http://asm.ow2.org/doc/tutorial-asm-2.0.htmlhttp://stackoverflow.com/questions/2129044/java-heap-terminology-young-old-and-permanent-generationshttp://javarevisited.blogspot.com.br/2011/04/garbage-collection-in-java.htmlhttp://docs.oracle.com/javase/6/docs/technotes/guides/management/jconsole.htmlhttp://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×