JVM Internal Architechture
-
Upload
nityananda-sahu -
Category
Education
-
view
202 -
download
1
description
Transcript of JVM Internal Architechture
Memory Management Memory Management Case study (JVM & CLR)Case study (JVM & CLR)
KIRAN KUMAR V KIRAN KUMAR V
LENIN THUMMALAPALLILENIN THUMMALAPALLI
C++ Memory Architecture C++ Memory Architecture SpecificationSpecification
CODE
STATIC Area
STACK
HEAP
JVM Architecture SpecificationJVM Architecture Specification
Method AreaMethod Area
Type information constant pool Field
information
Method information
Class
variables
Method Table
Reference to class loader and class Class
Method AreaMethod Area
Type Information
Constantpool
Field Informa
tion
Method Informa-
tion
Classvariables
Method Table
Ref. to class loader
and classClass
Fully qualified type’s
name.
Fully qualified direct super
class name.
Whether class or an
interface
type's modifiers
list of the fully qualified
names of any direct super
interfaces
Method AreaMethod Area
Constant PoolType
Information
Field Informa
tion
Method Informa-
tion
Classvariables
Method Table
Ref. to class loader
and classClass
Ordered set of constants - string - integer - floating point - final variables
symbolic references to
- types
- fields
- Methods
Method AreaMethod Area
Field Information
Type Informa
tion
Constant pool
Method Informa-
tion
Classvariables
Method Table
Ref. to class loader
and classClass
field’s name field’s type field’s modifiers (subset ) - public - private - protected - static - final - volatile - transient
Method AreaMethod Area
MethodInformation
Type Informa
tion
Constant pool
FieldInforma
tion
Classvariables
Method Table
Ref. to class loader
and classClass
Method’s name Method’s return type Number and type of parameters Modifiers (subset) - public - private - protected - static - final - synchronized - native - abstract
Method AreaMethod Area
Class variables
Type Informa
tion
Constant pool
FieldInforma
tion
MethodInforma
tion
Method Table
Ref. to class loader
and classClass
ordered set of class variables
- static variables
Method AreaMethod Area
Ref. toClass loader
And classClass
Type Informa
tion
Constant pool
FieldInforma
tion
MethodInforma
tion
Method Table
Class variables
Reference to class loader is used for dynamic linking. instance java.lang.Class is created every type forthe following info. - getName(); - getSuperClass(); - isInterface(); - getInterfaces(); - getClassLoader();
Method AreaMethod Area
Method Table
Type Informa
tion
Constant pool
FieldInforma
tion
MethodInforma
tion
Class variables
Used for quick ref. to
method.
Contains name and index in symbol ref. array
Ref. to class loader
and classClass
Heap MemoryHeap Memory
Objects and arrays are allocated in this Objects and arrays are allocated in this area.area.
Two different threads of the same Two different threads of the same application, however, could trample on application, however, could trample on each other's heap data.each other's heap data.
One possible Design of Object One possible Design of Object Allocation on Heap Allocation on Heap
Another Design of Object AllocationAnother Design of Object Allocation
Another Design of Object AllocationAnother Design of Object Allocation
Lock on ObjectsLock on Objects
object is associated with a lock (or mutex) to object is associated with a lock (or mutex) to coordinate multi-threaded access to the object.coordinate multi-threaded access to the object.
Only one thread at a time can "own" an object's Only one thread at a time can "own" an object's lock.lock.
Once a thread owns a lock, it can request the Once a thread owns a lock, it can request the same lock again multiple times, but then has to same lock again multiple times, but then has to release the lock the same number of times release the lock the same number of times before it is made available to other threads.before it is made available to other threads.
Array Allocation on HeapArray Allocation on Heap The name of an array's class has one The name of an array's class has one
open square bracket for each dimension open square bracket for each dimension plus a letter or string representing the plus a letter or string representing the array's type. array's type.
The class name for an array of ints is "[I“.The class name for an array of ints is "[I“. The class name for three-dimensional The class name for three-dimensional
array of bytes is "[[[B". array of bytes is "[[[B". The class name for a two-dimensional The class name for a two-dimensional
array of Objects is "[[Ljava.lang.Object". array of Objects is "[[Ljava.lang.Object".
Design of allocation of array on Design of allocation of array on HeapHeap
Java StackJava Stack
Java stack stores a thread's state in Java stack stores a thread's state in discrete frames. discrete frames.
Each frame containsEach frame contains - local variables Area.- local variables Area. - operand stack - operand stack - frame data- frame data
Local variable AreaLocal variable Area organized as a zero-based array of cells.organized as a zero-based array of cells. Variables are accessed through their Variables are accessed through their
indices.indices. Values of type int, float, reference, and Values of type int, float, reference, and
return Address occupy one cell. return Address occupy one cell. Values of type byte, short, and char also Values of type byte, short, and char also
occupy one cell.occupy one cell. Values of type long and double occupy Values of type long and double occupy
two consecutive cells in the array. two consecutive cells in the array.
class Example3a { public static int runClassMethod(int i, long l, float f, double d, Object o, byte b) { return 0; } public int runInstanceMethod(char c, double d, short s, boolean b) { return 0; } }
Operand StackOperand Stack operand stack is also organized as an array of operand stack is also organized as an array of
cells.cells. local variables are accessed via array indices, the local variables are accessed via array indices, the
operand stack is accessed by pushing and operand stack is accessed by pushing and popping values. popping values.
instructions take their operands from instructions take their operands from - operand stack- operand stack - immediately following the opcode - immediately following the opcode - constant pool- constant pool
iload_0 // push the int in local variable 0iload_0 // push the int in local variable 0 iload_1 // push the int in local variable 1iload_1 // push the int in local variable 1 iadd // pop two ints, add them, push resultiadd // pop two ints, add them, push result istore_2 // pop int, store into local variable 2istore_2 // pop int, store into local variable 2
Frame dataFrame data
Frame data is needed to support Frame data is needed to support - constant pool resolution- constant pool resolution - normal method return- normal method return - exception dispatch- exception dispatch - debugging.- debugging.
class Example3c {class Example3c { public static void addAndPrint() public static void addAndPrint()
{{ double result = double result =
addTwoTypes(1, 88.88);addTwoTypes(1, 88.88); System.out.println(result);System.out.println(result); }} public static double public static double
addTwoTypes(int i, double d) {addTwoTypes(int i, double d) { return i + d;return i + d; }}}}
class abc {class abc { public int a;public int a; String str;String str; abc() {abc() { a=10; atr=“string1”;a=10; atr=“string1”; }} public void print{ System.out.print(a+” “+str); }public void print{ System.out.print(a+” “+str); }}}interface def {interface def { void add();void add();} } class pqr extends abc implements def {class pqr extends abc implements def { static int b;static int b; final int c=50;final int c=50; String s;String s; pqr(int m) { super(); b=m; s= new String(”string2”); }pqr(int m) { super(); b=m; s= new String(”string2”); } void add() { a=a+c; add1(); }void add() { a=a+c; add1(); } static void add1() { c=b+50; }static void add1() { c=b+50; }}}
Example
class Main {class Main { public static void main(String[] s) {public static void main(String[] s) { pqr p=new pqr(20);pqr p=new pqr(20); p.add();p.add(); p.print();p.print(); }}}}
class abc {class abc { public int a;public int a; String str;String str; abc() {abc() { a=10; a=10; str=“string1”;str=“string1”; }} public void print{ public void print{ System.out.print(a+” “+str); System.out.print(a+” “+str); }}}}
abc
java.lang.Object
Isclass=true
modifier=4
Type info
a str <init> printSymbol ref. array
10 “string1”
Constant pool
name Type Modifiera int 5 0
str String 4 1
index
Field info
name ret.type npar modifier parlist<init> void 0 1 print void 0 5
codeptr
Method info
name index<init> 2
print 3
Method Table
null
Class variables
abc
java.lang.Object
Isclass=true
modifier=4ptr. to interface list ptr. to symbolic ref. array
name Type Modifier indexa int 5 0
str int 4 1
ptr to field infoname ret.type npar modifier parlist codeptr
<init> void 0 5 print void 0 5
ptr to method info
ptr to class variable list
ref. to class loader
ref. to Class
ptr to method table
Method name index in sym ref.<init> 2
print 3
Symbolic ref. array
Class Area of abc in Method area
interface def interface def {{ void add();void add();}}
def
java.lang.Object
Isclass=false
modifier=4
Type info
addSymbol ref. array
Constant pool
Field info
nullMethod info
name ret.type npar modifier parlist add void 0 4
codeptr
Class variables
null
add 0
Method Table
name index
def
java.lang.Object
Isclass=false
modifier=4ptr. to interface list ptr. to symbolic ref. arrayptr to field info
name ret.type npar modifier parlist codeptradd void 0 4 ptr to method info
ptr to class variable list
ref. to class loader
ref. to Class
ptr to method table
Method name index in sym ref. add 0
Symbolic ref. array
Class Area of def in Method area
class pqr extends abc implements def {class pqr extends abc implements def { static int b;static int b; final int c=50;final int c=50; String s;String s; pqr(int m) { super(); b=m; pqr(int m) { super(); b=m; s= new String(”string2”); }s= new String(”string2”); } void add() { a=a+c; add1(); }void add() { a=a+c; add1(); } static void add1() { c=b+50; }static void add1() { c=b+50; }}}
pqr
abc
Isclass=true
modifier=4
Type info
Symbolic ref. arrayb c s <init> super add add1
50
name Type Modifierb int 4,6 0
c int 4,7 1 S String 4 2
index
Field infoConstant pool
name ret.type npar modifier parlist<init> void 1 4 add void 0 4 add1 void 0 4 super void 0 4
Method infocodeptr
b
Class variables
<init> 3add 5
Method Table
name index
add1 6super 4
pqr
abc
Isclass=true
modifier=4ptr. to interface list ( to def)
ptr. to symbolic ref. array
name Type Modifier indexb int 4,6 0
c int 4,7 1
ptr to field infoname ret.type npar modifier parlist codeptr
<init> void 1 4 add void 0 4
ptr to method info
ptr to class variable list (b)
ref. to class loader
ref. to Class
ptr to method table Method name index in sym ref.<init> 3
add 4
Symbolic ref. array
Class Area of pqr in Method area
S String 4 2
add1 void 0 4 super void 0 4
class Main {class Main { public static void main(String[] s) {public static void main(String[] s) { pqr p=new pqr(20);pqr p=new pqr(20); p.add();p.add(); p.print();p.print(); }}}}
Main
java.lang.Object
Isclass=true
modifier=4
Type info
mainSymbol ref. array
Constant pool
Field info
nullMethod info
name ret.type npar modifier parlist main void 0 4
codeptr
Class variables
null
main 0
Method Table
name index
20
Main
java.lang.Object
Isclass=true
modifier=4ptr. to interface list ptr. to symbolic ref. arrayptr to field info
name ret.type npar modifier parlist codeptrmain void 0 5,6 ptr to method info
ptr to class variable list
ref. to class loader
ref. to Class
ptr to method table
Method name index in sym ref. main 0
Symbolic ref. array
Class Area of Main in Method area
Main
stack
Heap
Main
main
Main pqr
p
Main pqr
p
main
main
Pqr.<init>
Main pqr
p
Pqr.<init>
abc.<init>
Main pqr
p
Main pqr
p
a,b,c
strs
string2
abc
Main pqr
p
a,b,c
strs
string2
add
abc Main pqr
p
a,b,c
strs
string2
add
abc
add1
Main pqr
p
a,b,c
s
string2
abc
Main pqr
p
a,b,c
s
string2
abc Main pqr
p
a,b,c
s
string2
abc
str
str strstr s
Relation between C++ memory Relation between C++ memory structure and JVM memory structure and JVM memory
structurestructure
CODE
STATIC Area
Metadata
Method Area
Method Area
Constant pool
Class variables
Type Informa
tion
FieldInforma
tion
MethodInforma
tion
Method Table
Ref. to class loader
and classClass
C++ Heap
Garbage collection
JVM Heap
C++ stack
Operand Stack
Frame data
Threaded JVM Stack
Threaded JVM Stack
JVM stackThreads
Responsibilities of Memory MgrResponsibilities of Memory Mgr
Chop the chunk. Chop the chunk. Allocate Requested number of bytes.Allocate Requested number of bytes. Provide necessary information to garbageProvide necessary information to garbage collector.collector. Collect the bytes returned by garbageCollect the bytes returned by garbage collector.collector. Defragment the chunks.Defragment the chunks.
Design ChoicesDesign Choices How to allocate bytesHow to allocate bytes - Contiguous memory allocation.- Contiguous memory allocation. - Non contiguous memory allocation.- Non contiguous memory allocation. How many bytes to be allocatedHow many bytes to be allocated - exact requested number of bytes.- exact requested number of bytes. - Allocate bytes in terms of 2^n (Buddy System).- Allocate bytes in terms of 2^n (Buddy System). How defragmentation to be doneHow defragmentation to be done - Compaction.- Compaction. - Buddy system. - Buddy system.
JVM Memory ManagerJVM Memory Manager
Algorithms Followed Algorithms Followed Noncontiguous Memory Allocation.Noncontiguous Memory Allocation. First fit to choose approperiate chunk.First fit to choose approperiate chunk. Buddy system to chop a chunk.Buddy system to chop a chunk. Buddy system for defragmentation.Buddy system for defragmentation.
How to implement memory How to implement memory managermanager
Need of 4 threadsNeed of 4 threads - Memory allocator.- Memory allocator. - Memory Defragmenter.- Memory Defragmenter. - Chunk cleaner.- Chunk cleaner. - Data manager.- Data manager.
When VM requests m bytes When VM requests m bytes AllocatorAllocator 1. Checks for first chunk with size1. Checks for first chunk with size >=m+4.>=m+4. 2. If available chops it and returns to VM2. If available chops it and returns to VM else requests OS for new chunk. else requests OS for new chunk. Data Manager inserts an entry in MemoryData Manager inserts an entry in Memory Allocation Table (for garbage collector ).Allocation Table (for garbage collector ). Starting
addressSize requested Size Allocated
Garbage CollectionGarbage Collection For each entry in MA Table GC moves to For each entry in MA Table GC moves to
starting address+ size requested address,starting address+ size requested address, checks the ref. count. If zero returns the checks the ref. count. If zero returns the
index of table entry to Data Manager.index of table entry to Data Manager. Data Manager deletes the entry from Data Manager deletes the entry from
MATMAT and adds in Defragment Table.and adds in Defragment Table. For each entry in Defragment Table,For each entry in Defragment Table, Defragmenter combines the consecutive Defragmenter combines the consecutive fragments.fragments.
Starting Address Size
Design problemDesign problemVM requests an array of bytesVM requests an array of bytes - to store data in Method Area.- to store data in Method Area. - for stack frame.- for stack frame. - for object on Heap.- for object on Heap.MM doesn’t know the purpose of bytes.MM doesn’t know the purpose of bytes.How does MM allocates memory in threeHow does MM allocates memory in threedifferent address spaces for different type ofdifferent address spaces for different type of requests.requests.
Use Three MMs. Each MM has its own addr. Space. Its responsibility of VM to direct the requests to appropriate MM.
Optimal size of chunkOptimal size of chunk For Method Area MM 10KFor Method Area MM 10K For Stack 16KFor Stack 16K For Heap 1K, 10K,
32KInitial State of Chunk poolInitial State of Chunk pool
Trade off between performance and Trade off between performance and efficient memory usage.efficient memory usage. first and top pointers of each pool is set to first and top pointers of each pool is set to
null null
Chunk Pool cleanerChunk Pool cleaner
similar to Garbage collector.similar to Garbage collector. For every 5s cleaner returns all chunks For every 5s cleaner returns all chunks
more than 5.more than 5.
ClR Memory ManagementClR Memory Management
CLR Names to Memory Areas CLR Names to Memory Areas
Method Area as Type AreaMethod Area as Type Area Stack as RootsStack as Roots Heap as Heap, but two heapsHeap as Heap, but two heaps - Managed heap- Managed heap - Unmanaged heap- Unmanaged heap
In JVM entire heap is managed.
Necessity of Unmanaged HeapNecessity of Unmanaged Heap
JavaM/C 1
JVMM/C 2
CLR
M/C 1
M/C 2
C#
VB
Some Languages allow pointers. So to support pointers Unmanaged heap is supported
What is the differenceWhat is the difference
JVM MMJVM MM - Allocation is complex.- Allocation is complex. - Defragmentation is simple.- Defragmentation is simple.
ClR MMClR MM - Allocation is simple.- Allocation is simple. - Defragmentation is complex.- Defragmentation is complex.
JVM Memory ManagerJVM Memory Manager
Algorithms Followed Algorithms Followed Contiguous Memory Allocation.Contiguous Memory Allocation. Compaction for defragmentation.Compaction for defragmentation. Lazy DefragmenterLazy Defragmenter
Memory ManagersMemory Managers
Type Memory manager.Type Memory manager. Roots Memory manager.Roots Memory manager. Managed heap manager.Managed heap manager. Unmanaged heap Unmanaged heap
manager. manager.
GC runs.GC runs. No GC.No GC. GC runs.GC runs. No GC.No GC.
To deallocate Unmanaged Memory Finalizers should be used.
Obj 2Obj 1
Obj 3Next Ptr
Memory Allocation
Before GC After GC
Thank YouThank You