Jvm internals
Upcoming SlideShare
Loading in...5
×
 

Jvm internals

on

  • 1,695 views

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

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

Statistics

Views

Total Views
1,695
Views on SlideShare
1,688
Embed Views
7

Actions

Likes
6
Downloads
150
Comments
1

2 Embeds 7

http://www.linkedin.com 6
http://us-w1.rockmelt.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • Eu vi o link pelo TDC 2012. Parabéns!
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Jvm internals Jvm internals Presentation Transcript

  • JVM InternalsHow does your preferred platform works Luiz Fernando Teston Summa feu.teston@gmail.com / @FeuTeston
  • Introduction to (J)VMs Mimics a Machine or OS Abstraction Security Isolation ...
  • Machine/OS mimicsLoads codeExecutes codeTakes care of memoryManage code for security
  • Machine/OS mimicsLoads code :: ClassloaderExecutes codeTakes care of memory :: GCManage code for security
  • Execute code. How?Register basedStack based
  • Register basedLess (byte) codeWorks like your processorMore difficult to write a compilerEasier to execute
  • Register based getvar R1, "a" getvar R2, "b" c = a + b getvar R3, "c" add R3, R1, R2
  • Stack based (JVM way)More (byte) codeWorks like your HP Calculator ;-)Easier to write a compilerDifficult to execute
  • Stack based push a getvar push ‘b c = a + b getvar add push c storevar
  • Java Binary Class Layout
  • 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]; }
  • Java Binary Class Layout method_info { u2 access_flags; u2 name_index; u2 descriptor_index; u2 attributes_count; attribute_info attributes[attributes_count]; }
  • 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]; }
  • Sample sourcepublic class Sample { public int sum(int a, int b){ return a + b; }}
  • 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 }
  • Assembly,baby
  • Assembly,babyYes, we have low level stuff too!
  • Manipulating JVM Assembly
  • 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
  • Creating JVM bytecodeJava JasminSource ASM JavaJython SourceJRuby ClojureScala Groovyand many others...
  • Creating JVM bytecodeJava JasminSource ASM JavaJython Source compiles 0xCAFEBABEJRuby Clojure ... (JVM bytecode)Scala Groovyand many others...
  • Creating JVM bytecodeJava JasminSource ASM JavaJython Source runtime gen 0xCAFEBABEJRuby Clojure ... (JVM bytecode)Scala Groovyand many others...
  • Many easy ways ofextending bytecodesWithout ‘brushing bytecodes’: CGLib Java Proxies JavaAssist
  • Many easy ways ofextending bytecodesWithout ‘brushing bytecodes’: CGLib Java Proxies They’re easy! JavaAssist
  • Bytecodes can be...Generated when compiling a languageGenerated at runtime during class loadingModified at runtime during class loadingExtended at runtime dynamically
  • Bytecodes can be...Generated when compiling a language }Generated at runtime during class loadingModified at runtime during class loading JVMExtended at runtime dynamically Feature
  • Loading code: Classloaders
  • ClassloadersLoads code in a lazy mannerCan do anything while loading code Create new code Manipulate existing codeHierarchalCan be isolated
  • 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.
  • 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
  • Fancy Classloadersclass FancyClassLoader extends ClassLoader { public Class findClass(String name) { byte[] b = compileOnTheFly(name + “.fancy”); return defineClass(name, b, 0, b.length); }}
  • 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); }}
  • 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
  • Great Classloader samplesAppServer Classloaders per application Can isolate AppServer classes from the hosted application Isolates classes from each application Makes ease to implement hotdeploy
  • 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
  • Great Classloader samplesAOP (runtime) Can implement AOP on the fly (runtime) Many applications: Transaction management Logging Error tracing
  • Where the garbagecames from?
  • How to manage memory?C malloc/freeC++ new/delete
  • How to manage memory? }C malloc/free Almost the same...C++ new/delete
  • Memory fragmentationMemory as a big byte array...m[0], m[1] ..... m[MAX]
  • Memory fragmentationMemory as a big byte array...m[0], m[1] ..... m[MAX] x *x = 1, sizeof(x_type) = 2
  • 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
  • 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
  • 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
  • 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
  • Garbage Collection Possible solution for memory fragmentation Memory is no longer managed by programmer Runtime takes care of fragmentation during garbage collecting
  • 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
  • How GC works? Parameters includes: Running in parallel Intervals per region Specifying memory sizes for each type of object eden survivor1, survivor 2 PermGen
  • Sample GC-ed environment eden x y z survivor 1 a b csurvivor 2 w k l
  • Sample GC-ed environment eden x y z survivor 1 a b csurvivor 2 w k l
  • Sample GC-ed environment eden x z survivor 1 a bsurvivor 2 k l
  • Sample GC-ed environment eden x z survivor 1 a bsurvivor 2 k l
  • GC in practiceMark and sweep
  • GC in practiceMark and sweep
  • GC in practiceMark and sweep
  • GC in practiceMark and sweep
  • GC in practiceCopying GC
  • GC in practiceCopying GC
  • GC in practiceCopying GC
  • GC in practiceCopying GC Copy
  • GC in practiceCopying GC
  • GC in practiceCopying GC
  • Questions? Thanks! feu.teston@gmail.com / @FeuTeston
  • 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