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. Java Binary Class Layout
method_info {
u2 access_flags;
u2 name_index;
u2 descriptor_index;
u2 attributes_count;
attribute_info attributes[attributes_count];
}
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];
}
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
}
19. Stack bytecode execution?
Here we are talking about the method’s code section
Usually it is a switch statement on the opcode
The stack grow and sink as necessary
The opcode instruction are executed consuming and
producing stack entries
20. Creating JVM bytecode
Java Jasmin
Source ASM
Java
Jython
Source
JRuby Clojure
Scala Groovy
and many others...
22. Creating JVM bytecode
Java Jasmin
Source ASM
Java
Jython
Source
runtime gen 0xCAFEBABE
JRuby Clojure ...
(JVM bytecode)
Scala Groovy
and many others...
23. Many easy ways of
extending bytecodes
Without ‘brushing bytecodes’:
CGLib
Java Proxies
JavaAssist
24. Many easy ways of
extending bytecodes
Without ‘brushing bytecodes’:
CGLib
Java Proxies They’re easy!
JavaAssist
25. Bytecodes can be...
Generated when compiling a language
Generated at runtime during class loading
Modified at runtime during class loading
Extended at runtime dynamically
26. Bytecodes can be...
Generated when compiling a language
}
Generated at runtime during class loading
Modified at runtime during class loading
JVM
Extended at runtime dynamically
Feature
28. Classloaders
Loads code in a lazy manner
Can do anything while loading code
Create new code
Manipulate existing code
Hierarchal
Can be isolated
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. How classloading
(normally) works
There’s a classloader tree
For each classloader entry:
Search classes inside the classpath directories
Search classes inside jar files in the classpath
Until the class definition is find, searches the child
classloaders
31. Fancy Classloaders
class FancyClassLoader extends ClassLoader {
public Class findClass(String name) {
byte[] b = compileOnTheFly(name + “.fancy”);
return defineClass(name, b, 0, b.length);
}
}
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. Great Classloader samples
Remote 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. Great Classloader samples
AppServer Classloaders per application
Can isolate AppServer classes from the hosted
application
Isolates classes from each application
Makes ease to implement hotdeploy
35. Great Classloader samples
OSGi
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. Great Classloader samples
AOP (runtime)
Can implement AOP on the fly (runtime)
Many applications:
Transaction management
Logging
Error tracing
42. Memory fragmentation
Memory 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. Memory fragmentation
Memory 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. Memory fragmentation
Memory as a big byte array...
m[0], m[1] ..... m[MAX]
x y z
Each 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. Memory fragmentation
Memory as a big byte array...
WARNING!
m[0], m[1] ..... m[MAX]
Easy to mess with
x y z
Easy to fragment
Each 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. Garbage Collection
Possible solution for memory
fragmentation
Memory is no longer managed by
programmer
Runtime takes care of fragmentation
during garbage collecting
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. How GC works?
Parameters includes:
Running in parallel
Intervals per region
Specifying memory sizes for each type of
object
eden
survivor1, survivor 2
PermGen