1 / 13

# Garbage Collection and High-Level Languages

Garbage Collection and High-Level Languages. Programming Languages Fall 2003. The Notion of Garbage Collection. In a language like Python, we are treat high level data structures like integers. Consider a = [1,2,3,6,8,9]; … a = [6.9]; What happened to the old list?

Télécharger la présentation

## Garbage Collection and High-Level Languages

E N D

### Presentation Transcript

1. Garbage Collectionand High-Level Languages Programming Languages Fall 2003

2. The Notion of Garbage Collection • In a language like Python, we are treat high level data structures like integers. • Consider a = [1,2,3,6,8,9]; … a = [6.9]; • What happened to the old list? • Answer from Python programmer: Huh?

3. More on the Basic Notion • What do you mean Huh? • Sorry I don’t understand, if I write a = 10; … a = 23; • I don’t worry about what happens to the 10, it is just overwritten by the 23 and the 10 is gone.

4. More on the Basic Notion • Yes, but there is a big difference between integers. Integers fit in one word, but lists require dynamic allocation and you have to make sure the dynamic memory is released and reclaimed or you have a memory leak. • Sorry I don’t know what you are talking about, to me the two cases are identical.

5. The Point of This • If we want genuine high level languages, we do indeed what to treat the two cases as the same • And it is not acceptable to have the programmer have to worry about low level implementation details like releasing memory.

6. The Solution: Garbage Collection • So, at the implementation level we do indeed have to release and reclaim the memory, but it has to be done automatically (garbage collected). • Very easy to describe at the abstract level, since garbage collection is semantic free, it’s just a memory optimization. • But implementation concepts are not so clear.

7. Some Approaches • Try to release memory as soon as it is garbage. • But then we have to be able to determine this efficiently. • Clever idea, reference counts keep track of how many people are referencing a chunk of memory. • If count is zero, memory is garbage • But what about cycles 

8. Type Accurate Garbage Collection • Figure out all the “roots” in the program, these are values accessible directly by the program which can reference memory blocks. • These blocks are not garbage, also any blocks referenced by these blocks (recursively to all levels) are not garbage • Everything else is garbage.

9. Requirements for TA Garbage Collection • At run time, when garbage collection occurs, we have to be able to trace all references. • This means that we need information at run-time on the exact data layouts of all data structures in use. • Probably we have this in Python and other dynamic languages, but less likely to be true in static languages.

10. Conservative Garbage Collection • Assume that if a block is not garbage then there is somewhere in memory a word containing the address of this block. • Scan in use memory blocks for all addresses of memory blocks, assume all these blocks are not garbage. All other blocks are garbage • But what if an integer value happens to look like the address of a block.

11. Worry Spots for Garbage Collection • What if some global variables are logically dead, and will be clobbered, but have not been reassigned yet. May hold on to garbage (even with TA Gcol) • What if cycles and using reference counts • What if conservative gcol, and large blocks retained by accident.

12. Garbage Collection and Real-Time • If we use stop-and-collect approach (wait till we run out of memory, then do a TC or conservative garbage collection), this may take a hard to analyze amount of time. • This hard to analyze amount of time may occur at an embarrassing moment: • WARNING: MISSILE THREAD DETECTED! • (system msg): please wait, garbage collection in progress, normal execution will be resumed as soon as possile

13. Garbage Collection and Safety-Critical Programs • How can we analyze memory use • Hard anyway in a language like Python • And made harder by garbage collection • Usually we simply forbid dynamic allocation • Does that mean no Python in the next Boeing plane – probably!

More Related