Presentation is loading. Please wait.

Presentation is loading. Please wait.

Concepts of programming languages

Similar presentations


Presentation on theme: "Concepts of programming languages"— Presentation transcript:

1 Concepts of programming languages
Chapter 6 Data types Lec. 14 Lecturer: Dr. Emad Nabil

2 Problems with Pointers
1-Dangling pointers (dangerous) A pointer points to a heap-dynamic variable that has been deallocated 2- Lost heap-dynamic variable memory leakage dangling-pointer Solutions to the Dangling-Pointer Problem Tombstone: extra heap cell that is a pointer to the heap-dynamic variable Locks-and-keys: Pointer values are represented as (key, address) pairs No explicit deallocation of heap-dynamic variables out of the hands of programmers lost heap-dynamic

3 Heap Management

4 (eager approach): reclamation is gradual
Heap Management A very complex run-time process Single-size cells vs. variable-size cells Two approaches to reclaim garbage reclaim garbage Single-size cells Reference counters (eager approach): reclamation is gradual Mark-sweep (lazy approach) reclamation occurs when the list of available space becomes empty

5 Reference Counter : Single-size cells
Reference counters: maintain a counter in every cell that store the number of pointers currently pointing at the cell. Embedded in the decrement operation for the reference counters, which occurs when a pointer is disconnected from the cell, is a check for a zero value. If the reference counter reaches zero, it means that no program pointers are pointing at the cell, and it has thus become garbage and can be returned to the list of available space. Disadvantages: space required, execution time required. Advantage: efficiency- it is pure incremental, so significant delays in the application execution are avoided

6 1-Reference Counter : count char * p1 = new char (111); char * p2=p1;
P1 Dynamic heap variable P2 count P1 P1= new char (222); count P2 count P1 count P2= new char (333); P2 count Will be moved to avail_List

7 1- Reference Counter : count 2 111 char * p1 = new char (111); P1
char * p2=p1; P1 Dynamic heap variable P2 count P1 Dynamic heap variable delete p1; P2 Will be moved to avail_List If the reference counter reaches zero, it means that no program pointers are pointing at the cell, and it has thus become garbage and can be returned to the list of available space.

8 1-Reference Counter // STEP 1 Object a = new Integer (100); Object b = new Integer (99); // Step2 a=b; Possible implementation for reference counter code for a=b; is if (a != b) { if (a != null) if (--a.refCount == 0) heap.release (a); a = b; ++a.refCount; } if (a != b) { if (a != null) --a.refCount; a = b; ++a.refCount; }

9 1-Reference Counter – problem
public void buidDog() { Dog newDog = new Dog(); Tail newTail = new Tail(); newDog.tail = newTail; newTail.dog = newDog; } Main() { buidDog(); …. After completing execution of buildDog method the heap will be like this Reference counting does not detect garbage with cyclic references. One of the solution is to use mark and sweep alg.

10 1-Reference Counter – problem
Reference Counting fails to reclaim circular structures Python uses reference counting and offers cycle detection as well.

11 (eager approach): reclamation is gradual
Heap Management reclaim garbage Reference counters (eager approach): reclamation is gradual Mark-sweep (lazy approach) reclamation occurs when the list of available space becomes empty

12 2-Mark-And-Sweep (Garbage Collection Algorithm)
The mark-and-sweep algorithm is called a tracing garbage collector because is traces out the entire collection of objects that are directly or indirectly accessible by the program. The mark-and-sweep algorithm is divided into two phases: Mark phase:  The garbage collector traverses the graph of references from the root nodes and marks each heap object it encounters. Each object has an extra bit: the mark bit – initially the mark bit is 0. It is set to 1 for the reachable objects in the mark phase. Sweep phase: The GC exhaustively scans the heap looking for objects with mark bit 0 – these objects have not been visited in the mark phase – they are garbage. Any such object is added to the free list of objects that can be reallocated. The objects with a mark bit 1 have their mark bit reset to 0.

13 2-Mark-And-Sweep (Garbage Collection Algorithm)

14 In java : GC roots are objects that are themselves referenced by the JVM and thus keep every other object from being garbage-collected.

15 Once all live objects are marked, memory is exhaustively examined to find all of the unmarked (garbage) objects and reclaim their space

16 2-The Mark-And-Sweep algorithm can be expressed as follows:
for each root variable a mark (a); sweep ();  disadvantage: The program must be halted while garbage collection is being performed. void mark (Object a) if (!a.marked) a.marked = true; for each Object b referenced by a mark (b); void sweep () for each Object a in the heap if (a.marked) a.marked = false else heap.release (a); Once all live objects are marked, memory is exhaustively examined to find all of the unmarked (garbage) objects and reclaim their space

17 Mark-Sweep: Single-size cells
The run-time system allocates storage cells as requested and disconnects pointers from cells as necessary; mark-sweep then begins Every heap cell has an extra bit used by collection algorithm All cells initially set to garbage All pointers traced into heap, and reachable cells marked as not garbage All garbage cells returned to list of available cells Disadvantages: in its original form, it was done too infrequently. When done, it caused significant delays in application execution. Modern mark-sweep algorithms avoid this by doing it more often—called incremental mark-sweep Reading slide

18 Heap Management variable size cells
reclaim garbage variable size cells Mark-sweep (lazy approach) reclamation occurs when the list of variable space becomes empty

19 Variable-Size Cells Required by most programming languages (different data types and structured data types) In case of fixed size cells, we know the size of the garbage, but in case of the variable cell size(ex: char vs double).. How do we know the size of the cell to be reclaimed.. -Solution: each cell have a field to indicate its size. 2. cells with no pointers. How can you follow a chain? Solution: adding internal pointers to every cell. 3. maintaining a list of available space. (how to choose a slot for an object) Solution: sorting the list according to block size, Ascending. Marked (0|1)? Cell size Cell data pointer


Download ppt "Concepts of programming languages"

Similar presentations


Ads by Google