Memory Management Case study (JVM & CLR) KIRAN KUMAR V KIRAN KUMAR V LENIN THUMMALAPALLI LENIN THUMMALAPALLI.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode.
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Dynamic Memory Allocation I Topics Basic representation and alignment (mainly for static memory allocation, main concepts carry over to dynamic memory.
BA1 JVM arkitektur BA2 Specifikation I The Java Virtual Machine Specification specificeres bl.a.: –De grundlæggende datatyper.
Java Virtual Machine (JVM). Lecture Objectives Learn about the Java Virtual Machine (JVM) Understand the functionalities of the class loader subsystem.
INF 212 ANALYSIS OF PROG. LANGS Virtual Machines Instructors: Crista Lopes Copyright © Instructors.
1 1 Lecture 14 Java Virtual Machine Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Informática II Prof. Dr. Gustavo Patiño MJ
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Memory Management. History Run-time management of dynamic memory is a necessary activity for modern programming languages Lisp of the 1960’s was one of.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
JETT 2003 Java.compareTo(C++). JAVA Java Platform consists of 4 parts: –Java Language –Java API –Java class format –Java Virtual Machine.
CS 104 Introduction to Computer Science and Graphics Problems
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 32: The Java Virtual Machine COMP 144 Programming Language Concepts Spring 2002.
Run time vs. Compile time
JVM-1 Introduction to Java Virtual Machine. JVM-2 Outline Java Language, Java Virtual Machine and Java Platform Organization of Java Virtual Machine Garbage.
Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Unit 061 Java Virtual Machine (JVM) What is Java Virtual Machine? The Class Loader Subsystem Linking oVerification oPreparation oResolution Class Initialization.
Java Data Types  Everything is an Object  Except Primitive Data Types  For efficiency  Platform independent  Portable  “slow”  Objects are often.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Intro to Java The Java Virtual Machine. What is the JVM  a software emulation of a hypothetical computing machine that runs Java bytecodes (Java compiler.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 18: 0xCAFEBABE (Java Byte Codes)
1 Java Virtual Machine Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University.
Real-Time Concepts for Embedded Systems Author: Qing Li with Caroline Yao ISBN: CMPBooks.
Compiler Construction Lecture 17 Mapping Variables to Memory.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
1 The Java Virtual Machine Yearly Programming Project.
Implement High-level Program Language on JVM CSCE 531 ZHONGHAO LIU ZHONGHAO LIU XIAO LIN.
Java Bytecode What is a.class file anyway? Dan Fleck George Mason University Fall 2007.
Lecture 10 : Introduction to Java Virtual Machine
Runtime Environments Compiler Construction Chapter 7.
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Copyright Curt Hill Variables What are they? Why do we need them?
Lecture by: Prof. Pooja Vaishnav.  Language Processor implementations are highly influenced by the kind of storage structure used for program variables.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
Programming Languages and Paradigms Activation Records in Java.
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
More Examples of Abstract Interpretation Register Allocation.
RealTimeSystems Lab Jong-Koo, Lim
CS 432: Compiler Construction Lecture 15
Java Memory Management
The Java Virtual Machine (JVM)
Java Memory Management
CS216: Program and Data Representation
University of Central Florida COP 3330 Object Oriented Programming
Concepts of programming languages
Java Virtual Machine Complete subject details are available at:
null, true, and false are also reserved.
Introduction to Java Programming
Pointers, Dynamic Data, and Reference Types
Memory Allocation CS 217.
Units with – James tedder
Topic 3-b Run-Time Environment
Java Implementation – JVMs, JITs &c Hal Perkins Summer 2004
Created By: Asst. Prof. Ashish Shah, J.M.Patel College, Goregoan West
Chap 2. Identifiers, Keywords, and Types
Pointers, Dynamic Data, and Reference Types
CSc 453 Interpreters & Interpretation
Presentation transcript:

Memory Management Case study (JVM & CLR) KIRAN KUMAR V KIRAN KUMAR V LENIN THUMMALAPALLI LENIN THUMMALAPALLI

C++ Memory Architecture Specification CODE STATIC Area STACK HEAP

JVM Architecture Specification

Method Area Type information constant pool Field information Method information Class variables Method Table Reference to class loader and class Class

Method Area Type Information Constant pool Field Informa tion Method Informa- tion Class variables Method Table Ref. to class loader and class Class  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 Area Constant Pool Type Informa tion Field Informa tion Method Informa- tion Class variables Method Table Ref. to class loader and class Class  Ordered set of constants - string - integer - floating point - final variables  symbolic references to - types - fields - Methods

Method Area Field Information Type Informa tion Constant pool Method Informa- tion Class variables Method Table Ref. to class loader and class Class  field’s name  field’s type  field’s modifiers (subset ) - public - private - protected - static - final - volatile - transient

Method Area Method Information Type Informa tion Constant pool Field Informa tion Class variables Method Table 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 Class variables Type Informa tion Constant pool Field Informa tion Method Informa tion Method Table Ref. to class loader and class Class  ordered set of class variables - static variables

Method Area Ref. to Class loader And class Class Type Informa tion Constant pool Field Informa tion Method Informa tion Method Table Class variables  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 Method Table Type Informa tion Constant pool Field Informa tion Method Informa tion Class variables  Used for quick ref. to method.  Contains name and index in symbol ref. array Ref. to class loader and class Class

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

Lock on Objects  object is associated with a lock (or mutex) to coordinate 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 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 array of bytes is "[[[B".  The class name for a two-dimensional array 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. - local variables Area. - operand stack - operand stack - frame data - frame data

Local variable Area  organized as a zero-based array of cells.  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  operand stack is also organized as an array of 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 - operand stack - immediately following the opcode - immediately following the opcode - constant pool - constant pool

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

Frame data  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 { public static void addAndPrint() { public static void addAndPrint() { double result = addTwoTypes(1, 88.88); double result = addTwoTypes(1, 88.88); System.out.println(result); System.out.println(result); } public static double addTwoTypes(int i, double d) { public static double addTwoTypes(int i, double d) { return i + d; return i + d; }}

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 { void add(); void add();} 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 { 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 { 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 astr print Symbol ref. array 10 “string1” Constant pool nameTypeModifier a int 5 0 str String 4 1 index Field info nameret.typenparmodifierparlist void 0 1 print void 0 5 codeptr Method info name index 2 print3 Method Table null Class variables

abc java.lang.Object Isclass=true modifier=4 ptr. to interface list ptr. to symbolic ref. array nameTypeModifierindex a int 5 0 str int 4 1 ptr to field info nameret.typenparmodifierparlistcodeptr 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 print3 Symbolic ref. array Class Area of abc in Method area

interface def { void add(); void add();} def java.lang.Object Isclass=false modifier=4 Type info add Symbol ref. array Constant pool Field info null Method info nameret.typenparmodifierparlist add void 0 4 codeptr Class variables null add0 Method Table name index

def java.lang.Object Isclass=false modifier=4 ptr. to interface list ptr. to symbolic ref. array ptr to field info nameret.typenparmodifierparlistcodeptr add 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. add0 Symbolic ref. array Class Area of def in Method area

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. array bcs superaddadd1 50 nameTypeModifier b int 4,6 0 c int 4,7 1 S String 4 2 index Field info Constant pool nameret.typenparmodifierparlist void 1 4 add void 0 4 add1 void 0 4 super void 0 4 Method info codeptr b Class variables 3 add5 Method Table name index add16 super4

pqr abc Isclass=true modifier=4 ptr. to interface list ( to def) ptr. to symbolic ref. array nameTypeModifierindex b int 4,6 0 c int 4,7 1 ptr to field info nameret.typenparmodifierparlistcodeptr 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. 3 add4 Symbolic ref. array Class Area of pqr in Method area S String 4 2 add1 void 0 4 super void 0 4

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 main Symbol ref. array Constant pool Field info null Method info nameret.typenparmodifierparlist main void 0 4 codeptr Class variables null main0 Method Table name index 20

Main java.lang.Object Isclass=true modifier=4 ptr. to interface list ptr. to symbolic ref. array ptr to field info nameret.typenparmodifierparlistcodeptr main 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. main0 Symbolic ref. array Class Area of Main in Method area

Main stack Heap Main main Main pqr p Main pqr p main Pqr. Mainpqr p Pqr. abc. Main pqr p Main pqr p a,b,c str s string2 abc

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 print abc Main pqr p a,b,c s string2 abc str s

Relation between C++ memory structure and JVM memory structure

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

C++ Heap Garbage collection JVM Heap

C++ stack Operand Stack Frame data Threaded JVM Stack Threaded JVM Stack JVM stack Threads

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

Design Choices  How to allocate bytes - Contiguous memory allocation. - Contiguous memory allocation. - Non contiguous memory allocation. - Non contiguous memory allocation.  How 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 done - Compaction. - Compaction. - Buddy system. - 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 allocator. - Memory Defragmenter. - Memory Defragmenter. - Chunk cleaner. - Chunk cleaner. - Data manager. - Data manager.

When VM requests m bytes  Allocator 1. Checks for first chunk with size 1. Checks for first chunk with size >=m+4. >=m If available chops it and returns to VM 2. 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 Memory Allocation Table (for garbage collector ). Allocation Table (for garbage collector ). Starting addressSize requestedSize Allocated

Garbage Collection  For each entry in MA Table GC moves to starting address+ size requested address, checks the ref. count. If zero returns the index of table entry to Data Manager. 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. and adds in Defragment Table.  For each entry in Defragment Table, Defragmenter combines the consecutive Defragmenter combines the consecutive fragments. fragments. Starting AddressSize

Design problem VM 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. How does MM allocates memory in three different 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 chunk  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. 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 as Type Area Method Area as Type Area Stack as Roots Stack as Roots Heap as Heap, but two heaps Heap as Heap, but two heaps - Managed heap - Managed heap - Unmanaged heap - Unmanaged heap In JVM entire heap is managed.

Necessity of Unmanaged Heap Java M/C 1 JVM M/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 difference JVM MM - Allocation is complex. - Allocation is complex. - Defragmentation is simple. - Defragmentation is simple. ClR MM - Allocation is simple. - Allocation is simple. - Defragmentation is complex. - 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.

Obj 2 Obj 1 Obj 3 Next Ptr Memory Allocation Before GC After GC

Thank You