Example:Representation of a simple Java object Example:a simple Java object (no inheritance) Point p new Point (2,3); new allocates an object Point g new Point (0,0); in the heap class X 2 Y 3 class X 0 Point class Y Point constructor(1) 0 move method(2) area method(3) dist method(4)
Example: Representation of a simple Java object Example: a simple Java object (no inheritance) Point p = new Point(2 3); Point p = new Point(2 ,3); new allocates an object Point q = new Point(0,0); new allocates an object in the heap p class x y 2 3 q l Point class Point constructor(1) c lass x y 0 0 move area method(2) method(3) ( ) y 0 dist method ( 4 )
Objects can become garbage Point p new Point (2,3); Point g new Point (0,0); p=qi
Objects can become garbage Point p Point p = new Point(2,3); new Point(2,3); Point q = new Point(0,0); p = q;
Automatic Storage Deallocation (Garbage Collection) Everybody probably knows what a garbage collector is. But here are two "one liners"to make you think again about what a garbage collector really is! 1)Garbage collection provides the "illusion of infinite memory" 2)A garbage collector predicts the future! It's a kind of magic!:- Let us look at how this magic is done!
Automatic Storage Deallocation (Garbage Collection) Everybody probably knows what a garbage collector is. But here are two “one liners” to make you think again about what a garbage collector really is! 1) Garbage collection provides the “illusion of infinite memory ” 2) A garbage collector predicts the future! I ’ ki d f i ! I t’s a ki n d o f magi c! :- ) L t l k t h thi i i d ! L e t us loo k a t how this magic is done!
Stacks and dynamic allocations are incompatible Why can't we just do dynamic allocation within the stack? Proc P Act.Rec.P ptr X Proc Q ptr Y allocate (Y) Act.rec.Q X=Y ptr X points to storage that no longer exists (i.e.,is live)
Stacks and dynamic allocations are incompatible Why can’t we just do dynamic allocation within the stack?
Where to put the heap? The heap is an area of memory which is dynamically allocated Like a stack,it may grow and shrink during runtime. Unlike a stack it is not a LIFO =more complicated to manage o In a typical programming language implementation we will have both heap-allocated and stack allocated memory coexisting Q:How do we allocate memory for both
Where to put the heap? • The heap is an area of memory which is dynamically allocated. • Like a stack, it may grow and shrink during runtime. • Unlike a stack it is not a LIFO => more complicated to manage • In a typical programming language implementation we will have both heap-allocated and stack allocated memory coexisting. Q: How do we allocate memory for both