Upload
joydeepdey4u
View
239
Download
0
Embed Size (px)
Citation preview
7/29/2019 Java Memory Architecture
1/58
Joydeep Dey
7/29/2019 Java Memory Architecture
2/58
CODE
STATIC Area
STACK
HEAP
7/29/2019 Java Memory Architecture
3/58
7/29/2019 Java Memory Architecture
4/58
Typeinformation
constant pool Fieldinformation
Method
information
Class
variables
Method Table
Reference toclass loader
and classClass
7/29/2019 Java Memory Architecture
5/58
TypeInformation
Constantpool
FieldInforma
tion
MethodInforma-
tion
Classvariables
MethodTable
Ref. toclass loaderand class
Class
Fully qualified types
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
7/29/2019 Java Memory Architecture
6/58
Constant PoolType
Information
FieldInforma
tion
MethodInforma-
tion
Classvariables
MethodTable
Ref. toclass loaderand class
Class
Ordered set of constants- string- integer
- floating point- final variables
symbolic referencesto
- types
- fields- Methods
7/29/2019 Java Memory Architecture
7/58
FieldInformation
TypeInforma
tion
Constantpool
MethodInforma-
tion
Classvariables
MethodTable
Ref. toclass loaderand class
Class
fields name fields type fields modifiers (subset )
- public- private- protected- static- final- volatile
- transient
7/29/2019 Java Memory Architecture
8/58
MethodInformation
TypeInforma
tion
Constantpool
FieldInforma
tion
Classvariables
MethodTable
Ref. toclass loaderand class
Class
Methods name Methods return type Number and type of
parameters
Modifiers (subset)- public- private- protected- static- final
- synchronized- native- abstract
7/29/2019 Java Memory Architecture
9/58
Classvariables
TypeInforma
tion
Constantpool
FieldInforma
tion
MethodInforma
tion
MethodTable
Ref. toclass loaderand class
Class
ordered set of classvariables
- static variables
7/29/2019 Java Memory Architecture
10/58
Ref. toClass loader
And class
Class
TypeInforma
tion
Constantpool
FieldInforma
tion
MethodInforma
tion
MethodTable
Classvariables
Reference to class loaderis used for dynamic linking. instance java.lang.Class
is created every type forthe following info.
- getName();- getSuperClass();- isInterface();- getInterfaces();
- getClassLoader();
7/29/2019 Java Memory Architecture
11/58
Method
Table
TypeInforma
tion
Constantpool
FieldInforma
tion
MethodInforma
tion
Classvariables
Used for quick ref. to
method.
Contains name and indexin symbol ref. array
Ref. toclass loaderand class
Class
7/29/2019 Java Memory Architecture
12/58
Objects and arrays are allocated in this area.
Two different threads of the same application,however, could trample on each other's heap
data.
7/29/2019 Java Memory Architecture
13/58
7/29/2019 Java Memory Architecture
14/58
7/29/2019 Java Memory Architecture
15/58
7/29/2019 Java Memory Architecture
16/58
object is associated with a lock (or mutex) tocoordinate multi-threaded access to the object.
Only one thread at a time can "own" an object's
lock. Once a thread owns a lock, it can request the
same lock again multiple times, but then has torelease the lock the same number of timesbefore it is made available to other threads.
7/29/2019 Java Memory Architecture
17/58
The name of an array's class has one opensquare bracket for each dimension plus a letteror string representing the array's type.
The class name for an array of ints is "[I.
The class name for three-dimensional array ofbytes is "[[[B".
The class name for a two-dimensional array of
Objects is "[[Ljava.lang.Object".
7/29/2019 Java Memory Architecture
18/58
7/29/2019 Java Memory Architecture
19/58
Java stack stores a thread's state in discreteframes.
Each frame contains
- local variables Area.- operand stack
- frame data
7/29/2019 Java Memory Architecture
20/58
organized as a zero-based array of cells. Variables are accessed through their indices.
Values of type int, float, reference, and returnAddress occupy one cell.
Values of type byte, short, and char alsooccupy one cell.
Values of type long and double occupy two
consecutive cells in the array.
7/29/2019 Java Memory Architecture
21/58
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; }
}
7/29/2019 Java Memory Architecture
22/58
operand stack is also organized as an array ofcells.
local variables are accessed via array indices, the
operand stack is accessed by pushing andpopping values.
instructions take their operands from
- operand stack
- immediately following the opcode
- constant pool
7/29/2019 Java Memory Architecture
23/58
iload_0 // push the int in local variable 0 iload_1 // push the int in local variable 1 iadd // pop two ints, add them, push result
istore_2 // pop int, store into local variable 2
7/29/2019 Java Memory Architecture
24/58
Frame data is needed to support
- constant pool resolution
- normal method return
- exception dispatch
- debugging.
7/29/2019 Java Memory Architecture
25/58
class Example3c {
public static voidaddAndPrint() {
double result =
addTwoTypes(1, 88.88);System.out.println(result);
}
public static doubleaddTwoTypes(int i, double d)
{return i + d;
}
}
7/29/2019 Java Memory Architecture
26/58
class abc {
public int a;
String str;
abc() {
a=10; atr=string1;}
public void print{ System.out.print(a+ +str); }
}
interface def {
void add();
}
class pqr extends abc implements def {
static int b;
final int c=50;String s;
pqr(int m) { super(); b=m; s= new String(string2); }
void add() { a=a+c; add1(); }
static void add1() { c=b+50; }
}
Example
7/29/2019 Java Memory Architecture
27/58
class Main {public static void main(String[] s) {
pqr p=new pqr(20);
p.add();p.print();
}
}
7/29/2019 Java Memory Architecture
28/58
class abc {public int a;
String str;
abc() {a=10;str=string1;
}public void print{
System.out.print(a+ +str);}
}
abc
java.lang.Object
Isclass=true
modifier=4
Type info
a str print
Symbol 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
void 0 1
print void 0 5
codeptr
Method info
name index
2
print 3
Method Table
null
Class variables
Class A ea of abc in Method a ea
7/29/2019 Java Memory Architecture
29/58
abc
java.lang.ObjectIsclass=true
modifier=4
ptr. to interface list
ptr. to symbolic ref. array
name Type Modifier index
a int 5 0
str int 4 1
ptr to field infoname ret.type npar modifier parlist codeptr
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.
2
print 3
Symbolic ref. array
Class Area of abc in Method area
7/29/2019 Java Memory Architecture
30/58
interface def{
void add();
}
def
java.lang.Object
Isclass=false
modifier=4
Type info
add
Symbol ref. array
Constant pool
Field info
null
Method info
name ret.type npar modifier parlist
add void 0 4
codeptr
Classvariables
null
add 0
Method Table
name index
Class Area of def in Method area
7/29/2019 Java Memory Architecture
31/58
def
java.lang.ObjectIsclass=false
modifier=4
ptr. to interface list
ptr. to symbolic ref. arrayptr to field info
name ret.type npar modifier parlist codeptr
add void 0 4ptr 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
l d b l d f {
7/29/2019 Java Memory Architecture
32/58
class pqr extends abc implements def {static int b;final int c=50;String s;pqr(int m) { super(); b=m;
s= new String(string2); }void add() { a=a+c; add1(); }static void add1() { c=b+50; }}
pqr
abc
Isclass=true
modifier=4
Type info
Symbolic ref. array
b c s super add add1
50
name Type Modifier
b int 4,6 0
c int 4,7 1
S String 4 2
index
Field infoConstant pool
name ret.type npar modifier parlist
void 1 4
add void 0 4
add1 void 0 4
super void 0 4
Method info
codeptr
b
Class variables
3
add 5
Method Tablename index
add1 6
super 4
Class Area of pqr in Method area
7/29/2019 Java Memory Architecture
33/58
pqr
abcIsclass=true
modifier=4
ptr. to interface list ( to def)
ptr. to symbolic ref. array
name Type Modifier index
b int 4,6 0
c int 4,7 1
ptr to field infoname ret.type npar modifier parlist codeptr
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 tableMethod name index in sym ref.
3
add 4
Symbolic ref. array
Class Area of pqr in Method area
S String 4 2
add1 void 0 4super void 0 4
l M i {
7/29/2019 Java Memory Architecture
34/58
class Main {public static void main(String[] s) {
pqr p=new pqr(20);p.add();p.print();
}}
Main
java.lang.Object
Isclass=true
modifier=4
Type info
main
Symbol ref. array
Constant pool
Field info
null
Method info
name ret.type npar modifier parlist
main void 0 4
codeptr
Classvariables
null
main 0
Method Table
name index
20
Class Area of Main in Method area
7/29/2019 Java Memory Architecture
35/58
Main
java.lang.ObjectIsclass=true
modifier=4
ptr. to interface list
ptr. to symbolic ref. arrayptr to field info
name ret.type npar modifier parlist codeptr
main void 0 5,6ptr 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
7/29/2019 Java Memory Architecture
36/58
Main
stack
Heap
Main
main
Main pqr
p
Main pqr
p
main
main
Pqr.
Main pqr
p
Pqr.
abc.
Main pqr
p
Main pqr
p
a,b,c
str
s
string2
abc
7/29/2019 Java Memory Architecture
37/58
Main pqr
p
a,b,c
str
s
string2
add
abc Main pqr
p
a,b,c
str
s
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 str
strs
7/29/2019 Java Memory Architecture
38/58
7/29/2019 Java Memory Architecture
39/58
CODE
STATIC Area
Metadata
MethodArea
Constantpool
Classvariables
TypeInforma
tion
FieldInforma
tion
MethodInforma
tion
MethodTable
Ref. toclass loaderand class
Class
7/29/2019 Java Memory Architecture
40/58
C++ Heap
Garbage collection
JVM Heap
7/29/2019 Java Memory Architecture
41/58
C++ stack
OperandStack
Frame data
ThreadedJVMStack
JVM stackThreads
7/29/2019 Java Memory Architecture
42/58
Chop the chunk.
Allocate Requested number of bytes.
Provide necessary information to garbage
collector. Collect the bytes returned by garbage
collector.
Defragment the chunks.
7/29/2019 Java Memory Architecture
43/58
How to allocate bytes- Contiguous memory allocation.
- Non contiguous memory allocation.
How many bytes to be allocated
- exact requested number of bytes.
- Allocate bytes in terms of 2^n (Buddy System).
How defragmentation to be done
- Compaction.
- Buddy system.
7/29/2019 Java Memory Architecture
44/58
Algorithms Followed
Noncontiguous Memory Allocation.
First fit to choose approperiate chunk.
Buddy system to chop a chunk.
Buddy system for defragmentation.
7/29/2019 Java Memory Architecture
45/58
Need of 4 threads
- Memory allocator.
- Memory Defragmenter.
- Chunk cleaner.
- Data manager.
7/29/2019 Java Memory Architecture
46/58
Allocator
1. Checks for first chunk with size
>=m+4.
2. If available chops it and returns to VM
else requests OS for new chunk. Data Manager inserts an entry in Memory
Allocation Table (for garbage collector ).
Starting address Size requested Size Allocated
7/29/2019 Java Memory Architecture
47/58
For each entry in MA Table GC moves tostarting address+ size requested address,
checks the ref. count. If zero returns the index oftable entry to Data Manager.
Data Manager deletes the entry from MATand adds in Defragment Table.
For each entry in Defragment Table,
Defragmenter combines the consecutive
fragments.
Starting Address Size
7/29/2019 Java Memory Architecture
48/58
VM requests an array of bytes
- to store data in Method Area.- for stack frame.
- for object on Heap.
MM doesnt know the purpose of bytes.How does MM allocates memory in three
different address spaces for different type of
requests.Use Three MMs. Each MM has its own addr.Space. Its responsibility of VM to direct therequests to appropriate MM.
7/29/2019 Java Memory Architecture
49/58
For Method Area MM 10K
For Stack 16K
For Heap 1K, 10K, 32K
Initial State of Chunk pool Trade off between performance and
efficient memory usage. first and top pointers of each pool is set
to null
7/29/2019 Java Memory Architecture
50/58
similar to Garbage collector.
For every 5s cleaner returns all chunks morethan 5.
7/29/2019 Java Memory Architecture
51/58
7/29/2019 Java Memory Architecture
52/58
Method Area as Type Area
Stack as Roots
Heap as Heap, but two heaps- Managed heap
- Unmanaged heap
In JVM entire heap is managed.
7/29/2019 Java Memory Architecture
53/58
Java
M/C 1
JVMM/C 2
CLR
M/C 1
M/C 2
C#
VB
Some Languages allow pointers. So to supportpointers Unmanaged heap is supported
7/29/2019 Java Memory Architecture
54/58
JVM MM- Allocation is complex.
- Defragmentation is simple.
ClR MM
- Allocation is simple.
- Defragmentation is complex.
7/29/2019 Java Memory Architecture
55/58
Algorithms Followed
Contiguous Memory Allocation.
Compaction for defragmentation.
Lazy Defragmenter
7/29/2019 Java Memory Architecture
56/58
Type Memory manager.
Roots Memory manager.
Managed heap manager.
Unmanaged heap manager.
GC runs.
No GC.
GC runs.
No GC.
To deallocate Unmanaged Memory Finalizersshould be used.
7/29/2019 Java Memory Architecture
57/58
Obj 2
Obj 1
Obj 3
Next Ptr
MemoryAllocation
Before GC After GC
7/29/2019 Java Memory Architecture
58/58