0% found this document useful (0 votes)
74 views58 pages

Memory Architecture

Uploaded by

narender1818
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views58 pages

Memory Architecture

Uploaded by

narender1818
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 58

Memory Management Case study (JVM & CLR)

 

KIRAN KUMAR V LENIN THUMMALAPALLI

C++ Memory Architecture Specification


HEAP

STACK STATIC Area CODE

JVM Architecture Specification

Method Area
Type information constant pool Field information Method Table

Method information

Class variables

Reference to class loader and class Class

Method Area
 Fully qualified type s
name. Type Information Constant pool Field Informa tion

 Fully qualified direct super


class name.

 Whether class or an
interface Method Information Class variables Ref. to class loader and class Class Method Table

 type's modifiers  list of the fully qualified


names of any direct super interfaces

Method Area
Type Informa tion Constant Pool Field Informa tion

 Ordered set of constants


Method Table string integer floating point final variables

Method Information

Class variables

Ref. to class loader and class Class

 symbolic references to
- types - fields - Methods

Method Area
Type Informa tion Constant pool Field Information  field s name  field s type  field s modifiers (subset ) - public - private - protected - static - final - volatile - transient

Method Information

Class variables

Ref. to class loader and class Class

Method Table

Method Area
Type Informa tion Constant pool Field Informa tion Method Table Method Information Class variables Ref. to class loader and class Class  Method s name  Method s return type  Number and type of parameters  Modifiers (subset) - public - private - protected - static - final - synchronized - native - abstract

Method Area
Type Informa tion Constant pool Field Informa tion Method Table Method Informa tion Class variables Ref. to class loader and class Class

 ordered set of class variables - static variables

Method Area
Type Informa tion Constant pool Field Informa tion Method Table Method Informa tion Class variables Ref. to Class loader And class Class  Reference to class loader is used for dynamic linking.  instance java.lang.Class is created every type for the following info. - getName(); - getSuperClass(); - isInterface(); - getInterfaces(); - getClassLoader();

Method Area
Type Informa tion Constant pool Field Informa tion

 Used for quick ref. to


method.  Contains name and index in symbol ref. array

Method Informa tion

Class variables

Ref. to class loader and class Class

Method Table

Heap Memory
 Objects and arrays are allocated in this
area.  Two different threads of the same application, however, could trample on each other's heap data.

One possible Design of Object Allocation on Heap

Another Design of Object Allocation

Another Design of Object Allocation

Lock on Objects
 object is associated with a lock (or mutex) to
coordinate multi-threaded access to the object. multi 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 to release the lock the same number of times before it is made available to other threads.

Array Allocation on Heap


 The name of an array's class has one
open square bracket for each dimension plus a letter or string representing the array's type.  The class name for an array of ints is "[I .  The class name for three-dimensional threearray of bytes is "[[[B".  The class name for a two-dimensional twoarray of Objects is "[[Ljava.lang.Object".

Design of allocation of array on Heap

Java Stack
 Java stack stores a thread's state in
discrete frames.  Each frame contains - local variables Area. - operand stack - frame data

Local variable Area


 organized as a zero-based array of cells. zero Variables are accessed through their

indices.  Values of type int, float, reference, and return Address occupy one cell.  Values of type byte, short, and char also occupy one cell.  Values of type long and double occupy 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 Stack
cells.  local variables are accessed via array indices, the operand stack is accessed by pushing and popping values.  instructions take their operands from - operand stack - immediately following the opcode - constant pool

 operand stack is also organized as an array of

   

iload_0 iload_1 iadd istore_2

// push the int in local variable 0 // push the int in local variable 1 // pop two ints, add them, push result // pop int, store into local variable 2

Frame data
 Frame data is needed to support
constant pool resolution normal method return exception dispatch debugging.

class Example3c { public static void addAndPrint() { double result = addTwoTypes(1, 88.88); System.out.println(result); } public static double addTwoTypes(int i, double d) { return i + d; } }

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

class Main { public static void main(String[] s) { pqr p=new pqr(20); p.add(); p.print(); } }

class abc { Type info public int a; abc String str; java.lang.Object abc() { Isclass=true a=10; str= string1 ; modifier=4 } public void print{ System.out.print(a+ +str); } }

Constant pool
Symbol ref. array a 10 str <init> print string1

Field info
name a str Type Modifier int String 5 4 index 0 1

Method info
0 0 1 5

name ret.type npar modifier parlist codeptr <init> print void void

Method Table
name index <init> print 2 3

Class variables

null

Class Area of abc in Method area


abc java.lang.Object Isclass=true modifier=4 ptr. to interface list ptr. to symbolic ref. array ptr to field info ptr to method info ptr to class variable list ref. to class loader ref. to Class ptr to method table Method name <init> print index in sym ref. 2 3 name ret.type npar modifier parlist codeptr <init> print void void 0 0 5 5 name a str Type Modifier int int 5 4 index 0 1 Symbolic ref. array

interface def { void add(); } Field info null

Type info
def java.lang.Object Isclass=false modifier=4

Constant pool
Symbol ref. array add

Method info
name ret.type npar modifier parlist codeptr add void 0 4

Class variables null

Method Table
name index add 0

Class Area of def in Method area


def java.lang.Object Isclass=false modifier=4 ptr. to interface list ptr. to symbolic ref. array ptr to field info ptr to method info ptr to class variable list ref. to class loader ref. to Class ptr to method table Method name add index in sym ref. 0 name ret.type npar modifier parlist codeptr add void 0 4 Symbolic ref. array

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; } }

Type info
pqr abc Isclass=true modifier=4

Class variables

b Field info
name b c S Type Modifier int int String 4,6 4,7 4 index 0 1 2

Constant pool
Symbolic ref. array b 50 c s <init> super add add1

Method info
name ret.type npar modifier parlist codeptr <init> add add1 super void void void void 1 0 0 0 4 4 4 4

Method Table
name index <init> 3 add 5 add1 6 super 4

Class Area of pqr in Method area


pqr abc Isclass=true modifier=4 ptr. to interface list ( to def) ptr. to symbolic ref. array ptr to field info ptr to method info ptr to class variable list (b) ref. to class loader ref. to Class ptr to method table Method name <init> add index in sym ref. 3 4 name b c S Type Modifier int int String 4,6 4,7 4 index 0 1 2 Symbolic ref. array

name ret.type npar modifier parlist codeptr <init> add add1 super void void void void 1 0 0 0 4 4 4 4

class Main { public static void main(String[] s) { pqr p=new pqr(20); p.add(); p.print(); } }

Type info
Main java.lang.Object Isclass=true modifier=4

Constant pool
Symbol ref. array main 20

Field info null


main

Method info
name ret.type npar modifier parlist codeptr void 0 4

Class variables null

Method Table
name index main 0

Class Area of Main in Method area


Main java.lang.Object Isclass=true modifier=4 ptr. to interface list ptr. to symbolic ref. array ptr to field info ptr to method info ptr to class variable list ref. to class loader ref. to Class ptr to method table Method name main index in sym ref. 0 name ret.type npar modifier parlist codeptr main void 0 5,6 Symbolic ref. array

Main

Main main

Main p

pqr main

Main p

pqr

abc str s

stack Heap

a,b,c string2 Main pqr Main p main Pqr.<init> p pqr

Main p

pqr

Pqr.<init> abc.<init>

Main p

pqr

abc str s

Main p add

pqr

abc str s

Main p

pqr

abc str s

add string2

a,b,c add1 a,b,c a,b,c Main p pqr abc str s print string2 a,b,c string2 a,b,c Main p string2 pqr abc str s str s string2

Relation between C++ memory structure and JVM memory structure

Constant pool

STATIC Area CODE

Field Informa tion

Class variables

Type Informa tion

Metadata

Method Area
Ref. to class loader and class Class Method Table

Method Informa tion

Garbage collection

JVM Heap

C++ Heap

Frame data

Operand Stack

Threaded JVM Stack

JVM stack
Threads

C++ stack

Responsibilities of Memory Mgr


 Chop the chunk.  Allocate Requested number of bytes.  Provide necessary information to garbage
collector.  Collect the bytes returned by garbage collector.  Defragment the chunks.

Design Choices
 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.

JVM Memory Manager


Algorithms Followed  Noncontiguous Memory Allocation.  First fit to choose approperiate chunk.  Buddy system to chop a chunk.  Buddy system for defragmentation.

How to implement memory manager


 Need of 4 threads
Memory allocator. Memory Defragmenter. Chunk cleaner. Data manager.

When VM requests m bytes


 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

Garbage Collection
starting address+ size requested address, checks the ref. count. If zero returns the index of table entry to Data Manager.  Data Manager deletes the entry from MAT and adds in Defragment Table.  For each entry in Defragment Table, Defragmenter combines the consecutive fragments. Starting Address Size

 For each entry in MA Table GC moves to

Design problem
VM requests an array of bytes - to store data in Method Area. - for stack frame. - for object on Heap. MM doesn t 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 the requests to appropriate MM.

Optimal size of chunk


 For Method Area MM  For Stack
 For

10K 16K

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

Chunk Pool cleaner


 similar to Garbage collector.  For every 5s cleaner returns all chunks
more than 5.

ClR Memory Management

CLR Names to Memory Areas


Method Area Stack Heap as Type Area as Roots as Heap, but two heaps - Managed heap - Unmanaged heap

In JVM entire heap is managed.

Necessity of Unmanaged Heap


M/C 1

Java

JVM
M/C 2

C#

M/C 1

CLR
VB
M/C 2

Some Languages allow pointers. So to support pointers Unmanaged heap is supported

What is the difference


JVM MM - Allocation is complex. - Defragmentation is simple. ClR MM - Allocation is simple. - Defragmentation is complex.

JVM Memory Manager


Algorithms Followed  Contiguous Memory Allocation.  Compaction for defragmentation.  Lazy Defragmenter

Memory Managers
 Type Memory manager.  Roots Memory manager.  Managed heap manager.  Unmanaged heap manager.  GC runs.  No GC.  GC runs.  No GC.

To deallocate Unmanaged Memory Finalizers should be used.

Memory Allocation

Before GC

After GC

Next Ptr Obj 3 Obj 2 Obj 1

Thank You

You might also like