'new' in JVM -- What happen when 'new' is called? 1st
-
Upload
kengo-toda -
Category
Technology
-
view
595 -
download
1
Transcript of 'new' in JVM -- What happen when 'new' is called? 1st
'new' in JVM (1/2)What happen when 'new' is called?
Kengo TODA 2012/May/3
12年5月2日水曜日
On condition that:
You already know
what is reference
what is memory
what is ‘class file’
12年5月2日水曜日
This code becomes...
public class Test {! public static void main(String[] args) {! ! Test test = new Test();! }}
12年5月2日水曜日
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日水曜日
new => 3 opcodes
new dup
new
invokespecial
javac
12年5月2日水曜日
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日水曜日
opcode ‘dup’
‘dup’ copies a referencecreated by ‘new’.
dup
12年5月2日水曜日
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 fullyinitialized!
invokespecial
12年5月2日水曜日
Summary
Java heap
instance
1.‘new’
reference3.‘invokespecial’
4.retu
rn
reference2.‘dup’
12年5月2日水曜日
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日水曜日
References
newするとJVM内で何が起こるのかという話
前編 / 後編
The Java™ Virtual Machine Specification
Java theory and practice: Fixing the Java Memory Model, Part 1
12年5月2日水曜日