Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
'new' in JVM -- What happen when 'new' is called? 1st
1. 'new' in JVM (1/2)
What happen when 'new' is called?
Kengo TODA 2012/May/3
12年5月2日水曜日
2. On condition that:
You already know
what is reference
what is memory
what is ‘class file’
12年5月2日水曜日
3. This code becomes...
public class Test {
! public static void main(String[] args) {
! ! Test test = new Test();
! }
}
12年5月2日水曜日
4. this byte code.
public static void main(java.lang.String[]);
Code:
0:! new!#1; //class Test
3:! dup
4:! invokespecial!#16; //Method "<init>":()V
7:! astore_1
8:! return
12年5月2日水曜日
5. new => 3 opcodes
new
new javac dup
invokespecial
12年5月2日水曜日
6. opcode ‘new’
new
‘new’ allocates memory
(Java heap) to store instance data.
Allocated memory isn’t initialized.
You get a reference to allocated instance.
12年5月2日水曜日
7. opcode ‘dup’
‘dup’ copies a reference dup
created by ‘new’.
12年5月2日水曜日
8. opcode ‘invokespecial’
‘invokespecial’ invokes <init> method of
specified class. <init> is name of constructor.
This opcode consumes a reference, so JVM
has to call ‘dup’ at first.
Finally instance is fully invokespecial
initialized!
12年5月2日水曜日
10. key point
Opcode ‘new’ doesn’t call constructor.
We can get an reference to uninitialized
instance.
Data size is fixed at first (by opcode ‘new’).
JVM can decide size from class definition.
We have to create new instance to expand
data size. @see ArrayList#grow(int).
12年5月2日水曜日
11. References
newするとJVM内で何が起こるのかという話
前編 / 後編
The Java™ Virtual Machine Specification
Java theory and practice: Fixing the Java
Memory Model, Part 1
12年5月2日水曜日