Cells and Liveness Cell data item in the heap -Cells are"pointed to"by pointers held in registers,stack,global/static memory,or in other heap cells Roots:registers,stack locations,global/static variables A cell is live if its address is held in a root or held by another live cell in the heap slide 6
Cells and Liveness • Cell = data item in the heap – Cells are “pointed to” by pointers held in registers, stack, global/static memory, or in other heap cells • Roots: registers, stack locations, global/static variables • A cell is live if its address is held in a root or held by another live cell in the heap slide 6
Garbage 。 Garbage is a block of heap memory that cannot be accessed by the program An allocated block of heap memory does not have a reference to it (cell is no longer "live") -Another kind of memory error:a reference exists to a block of memory that is no longer allocated 。 Garbage collection(GC)-automatic management of dynamically allocated storage Reclaim unused heap blocks for later use by program slide7
Garbage • Garbage is a block of heap memory that cannot be accessed by the program – An allocated block of heap memory does not have a reference to it (cell is no longer An allocated block of heap memory does not have a reference to it (cell is no longer “live”) – Another kind of memory error: a reference exists to a block of memory that is no longer allocated • Garbage collection (GC) - automatic management of dynamically allocated storage allocated storage – Reclaim unused heap blocks for later use by program slide 7
Example of Garbage class node int value; p=new node(); node next; q new node(); q=p; delete p, node p,q, null q (a) (b) (c) slide 8
Example of Garbage class node { int value; node next; p = new node(); q = new node(); node next; } node p, q; q (); q = p; delete p; p, q; slide 8
OO Languages and heap allocation Objects are a lot like records and instance variables are a lot like fields. =The representation of objects is similar to that of a record. Methods are a lot like procedures. =>Implementation of methods is similar to routines. But...there are differences: Objects have methods as well as instance variables,records only have fields. The methods have to somehow know what object they are associated with (so that methods can access the object's instance variables)
OO Langg p ua es and heap allocation Objects are a lot like records and instance variables are a lot like fields. like fields. => The representation of objects is similar to that of a record. Methods are a lot like procedures. => Implementation of methods is similar to routines. But… there are differences: there are differences: Objects have methods as well as instance variables, records only have fields. Th th d h t h k h t bj t th The methods have to somehow know what object they are associated with (so that methods can access the object’s instance variables) instance variables)
Example:Representation of a simple Java object Example:a simple Java object(no inheritance) class Point int x,y; (1public Point(int x,int y){ this.x=x;this.y=y; (2public void move(int dx,int dy){ x=x+dx;y=y+dy; (3public float area (){... (4public float dist (Point other){
Example: Representation of a simple Java object Example: a simple Java object (no inheritance) class Point { class Point { int x,y; ( 1 )p y ublic Point(int x, int y) { this.x=x; this.y=y; } ( ) public void move(int dx, int dy) { x=x+dx; y=y+dy; (2) x=x+dx; y=y+dy; } public float area() { ...} public float dist(Point other) { ... } } (3) (4)