Programming Languages

2nd edition Tucker and Noonan

Chapter 11 Memory Management
C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off. B. Stroustrup

Copyright © 2006 The McGraw-Hill Companies, Inc.

11.1 The Heap
The major areas of memory:

Static area: fixed size, fixed content allocated at compile time Run-time stack: variable size, variable content center of control for function call and return Heap: fixed size, variable content dynamically allocated objects and data structures

Copyright © 2006 The McGraw-Hill Companies, Inc.

The Structure of Run-Time Memory Fig 11. .1 Copyright © 2006 The McGraw-Hill Companies. Inc.

. .Allocating Heap Blocks The function new allocates a block of heap space to the program. Inc.g. new(5) returns the address of the next block of 5 words available in the heap: Copyright © 2006 The McGraw-Hill Companies. E.

Stack and Heap Overflow Stack overflow occurs when the top of stack. would exceed its (fixed) limit. Copyright © 2006 The McGraw-Hill Companies. . Inc. h. Heap overflow occurs when a call to new occurs and the heap does not have a large enough block available to satisfy the call. a.

4. Copyright © 2006 The McGraw-Hill Companies. Inc. Push addr(A[0]) onto the stack.1) is: 1. Steps 2-4 create the dope vector for A in the stack.2 Implementation of Dynamic Arrays Consider the declaration int A[n]. . Step 1 creates a heap block for A.11. Push int onto the stack. Push n onto the stack. Its meaning (Meaning Rule 11. 2. 3. Compute addr(A[0]) = new(n).

Inc.3 Copyright © 2006 The McGraw-Hill Companies. Fig 11. .Stack and Heap Allocation for int A[10].

E.size.g. If addr(ad[0])addr(ad[ar.2 The meaning of an ArrayRef ar for an array declaration ad is: 1. return the value at addr(ad[ar.index])<addr(ad[0]) + ad. Note: this definition includes run-time range checking..Array References Meaning Rule 11. Compute addr(ad[ar. signal an index-out-of-range error. Copyright © 2006 The McGraw-Hill Companies. Inc. Otherwise. consider the ArrayRef A[5].index]) = addr(ad[0]) + ar.index]) 3. . The value of A[5] is addressed by addr(A[0])+4.index-1 2.

Array Assignments Meaning Rule 11. The assignment A[5]=3 changes the value at heap address addr(A[0])+4 to 3.g. signal an index-out-of-range error.3 The meaning of an Assignment as is: 1. Inc. 3. If addr(ad[0])addr(ad[ar.index]).index=5 and addr(A[5])=addr(A[0])+4.index-1 2.source to addr(ad[ar. E. Copyright © 2006 The McGraw-Hill Companies.index])<addr(ad[0]) + ad. ..size then assign the value of as. since ar. Otherwise. Compute addr(ad[ar.index])=addr(ad[0]) + ar.

.3 Garbage Collection Garbage is a block of heap memory that cannot be accessed by the program. Garbage can occur when either: 1. An allocated block of heap memory has no reference to it (an “orphan”). or 2. Copyright © 2006 The McGraw-Hill Companies. A reference exists to a block of memory that is no longer allocated (a “widow”).11. Inc.

q = new node(). } node p. Inc. . node next.Garbage Example (Fig 11. // creates orphan node delete p. q= p. // creates a widow of q q is a widow orphan node Copyright © 2006 The McGraw-Hill Companies. q. p = new node().4) class node { int value.

Occurs only on heap overflow. but reduces the size of the heap space.Faster than mark-sweep. Inc. detects all garbage. but makes two passes on the heap. .Garbage Collection Algorithms Three classical garbage collection strategies: – Reference Counting . but doesn’t detect all garbage. – Copy Collection . Copyright © 2006 The McGraw-Hill Companies.occurs whenever a heap block is allocated. – Mark-Sweep .

11. three are active orphan is to be removed. Each node has a reference count (RC) # of pointers referencing that node. Inc.1 Reference Counting The heap is a chain of nodes (the free_list). RC = 0 Copyright © 2006 The McGraw-Hill Companies. Of four nodes. .3.

the RC for each of its descendents is reduced by 1. and this process repeats for its descendents. leaving: Note the orphan chain on the right.Problem occurs with the circular chains such as the execution of p. Inc.next = null Since q’s node has RC=0. it is returned to the free list. Copyright © 2006 The McGraw-Hill Companies. .

2 Mark-Sweep Each node in the free_list has a mark bit (MB) initially 0. . Called only when heap overflow occurs: Pass I: Mark all nodes that are (directly or indirectly) accessible from the stack by setting their MB=1. Note: all orphans are detected and returned to the free list. Pass II: Sweep through the entire heap and return all unmarked (MB=0) nodes to the free list.3. Inc.11. Copyright © 2006 The McGraw-Hill Companies.

All accessible nodes are marked 1. . Inc. Copyright © 2006 The McGraw-Hill Companies.Heap after Pass I of Mark-Sweep Triggered by q=new node() and free_list = null.

.wikipedia.org/wiki/Garbage_collection_%28computer_science%29#Copying_v s.27t-sweep Copyright © 2006 The McGraw-Hill Companies.Heap after Pass II of Mark-Sweep Now free_list is restored and the assignment q=new node() can proceed. See: http://en._mark-and-don._mark-and-sweep_vs. Inc.

only one is active.3 Copy Collection Heap partitioned into two halves.11. .3. Triggered by q=new node() and free outside the active half: Copyright © 2006 The McGraw-Hill Companies. Inc.

orphans are returned to the free list. and the two halves reverse roles. . Copyright © 2006 The McGraw-Hill Companies.Accessible nodes are copied to other half Note: The accessible nodes are packed. Inc.

– Also. – Most are hybrids/refinements of the above three. garbage collection is built-in. System.gc may be called by the program. Inc. . – runs as a low-priority thread. • In Java.Garbage Collection Summary • Modern algorithms are more elaborate. • Functional languages have garbage collection built-in. Copyright © 2006 The McGraw-Hill Companies. • C/C++ default garbage collection to the programmer.