1 Languages and Compilers (SProg og Oversættere) Heap allocation and Garbage Collection.

11
1 Languages and Compilers (SProg og Oversættere) Heap allocation and Garbage Collection

Transcript of 1 Languages and Compilers (SProg og Oversættere) Heap allocation and Garbage Collection.

Page 1: 1 Languages and Compilers (SProg og Oversættere) Heap allocation and Garbage Collection.

1

Languages and Compilers(SProg og Oversættere)

Heap allocation and Garbage Collection

Page 2: 1 Languages and Compilers (SProg og Oversættere) Heap allocation and Garbage Collection.

2

Heap allocation and Garbage Collection

– Why may we need heap allocation?

– Garbage collection strategies

Page 3: 1 Languages and Compilers (SProg og Oversættere) Heap allocation and Garbage Collection.

3

Heap Storage

• Memory allocation under explicit programmatic control– C malloc, C++ / Pascal / Java new operation.

• Memory allocation implicit in language constructs– Lisp, Scheme, Haskel, SML, … most functional languages

• Deallocation under explicit programmatic control– C, C++, Pascal

• Deallocation implicit– Java, Lisp, …

Page 4: 1 Languages and Compilers (SProg og Oversættere) Heap allocation and Garbage Collection.

4

Stacks and dynamic allocations are incompatible

Why can’t we just do dynamic allocation within the stack?

Programming Language design and Implementation -4th EditionCopyright©Prentice Hall, 2000

Page 5: 1 Languages and Compilers (SProg og Oversættere) Heap allocation and Garbage Collection.

5

How does things become garbage?

int *p, *q;…p = malloc(sizeof(int));p = q;

for(int i=0;i<10000;i++){ SomeClass obj= new SomeClass(i); System.out.println(obj);}

Newly created space becomes garbage

Creates 10000 objects, which becomes garbage just after the print

Page 6: 1 Languages and Compilers (SProg og Oversættere) Heap allocation and Garbage Collection.

6

Heap Storage Deallocation

Explicit versus Implicit Deallocation

Examples: • Implicit: Java, Scheme • Explicit: Pascal and C

To free heap memory a specific operation must be called.Pascal ==> dispose C ==> free

In explicit memory management, the program must explicitly call an operation to release memory back to the memory management system.

In implicit memory management, heap memory is reclaimed automatically by a “garbage collector”.

Page 7: 1 Languages and Compilers (SProg og Oversættere) Heap allocation and Garbage Collection.

7

Where to put the heap?

ST

SB

HB

HT

Stack memory area

Heap memory area

Stack grows downward

Heap can expand upward

Page 8: 1 Languages and Compilers (SProg og Oversættere) Heap allocation and Garbage Collection.

8

How to keep track of free memory?

Stack is LIFO allocation => ST moves up/down everything above ST is in use/allocated. Below is free memory. This is easy! But …Heap is not LIFO, how to manage free space in the “middle” of the heap?

HB

HTAllocated

ST

SB

Free

Free

Mixed:AllocatedandFree

reuse?

Page 9: 1 Languages and Compilers (SProg og Oversættere) Heap allocation and Garbage Collection.

9

Heap Compaction

To fight fragmentation, some memory management algorithms perform “heap compaction” once in a while.

HB

HT

HFa

b

c

HB

HTa

b

ddc

before after

Page 10: 1 Languages and Compilers (SProg og Oversættere) Heap allocation and Garbage Collection.

10

Classes of Garbage Collection Algorithms

• Direct Garbage Collectors: a record is associated with each node in the heap. The record for node N indicates how many other nodes or roots point to N.

• Indirect/Tracing Garbage Collectors: usually invoked when a user’s request for memory fails because the free list is exhausted. The garbage collector visits all live nodes, and returns all other memory to the free list. If sufficient memory has been recovered from this process, the user’s request for memory is satisfied.

Page 11: 1 Languages and Compilers (SProg og Oversættere) Heap allocation and Garbage Collection.

11

Types of garbage collectors

• The “Classic” algorithms– Reference counting

– Mark and sweep

• Copying garbage collection

• Incremental Tracing garbage collection

• Generational garbage collection