22
2 Cons operation calls for the allocation of a new cons-cell, like new in Pascal. How to make it free?

Storage Reclamation Explicit Erasure Reference Count Garbage collection

Embed Size (px)

Citation preview

Page 1: Storage Reclamation Explicit Erasure Reference Count Garbage collection

2

Cons operation calls for the allocation of a new cons-cell, like new in Pascal.

How to make it free?

Page 2: Storage Reclamation Explicit Erasure Reference Count Garbage collection

3

1. Explicit Erasure

Adopted by Pascal (Dispose function)

Releasing each cell by Programmer

Linking the cell on to a Free-List

Page 3: Storage Reclamation Explicit Erasure Reference Count Garbage collection

4

Problems with Explicit Erasure

Programmers must work harder– Who is the “Good” Programmer?

Remember the words against Fortran– Computer Take care of bookkeeping Details

Violating the Security Principle– Dangling Pointers ( pointers that do not point to an allocated cell)

Page 4: Storage Reclamation Explicit Erasure Reference Count Garbage collection

5

Dangling Pointer

A cell still referenced by several other lists

Storage allocator reuses this cell

Corrupt the storage allocator’s Data structures

Free-List

C

Dangling Ref.

Page 5: Storage Reclamation Explicit Erasure Reference Count Garbage collection

Garbage Collection with no explicit allocation/de-allocation: Responsible

Design Principle

Page 6: Storage Reclamation Explicit Erasure Reference Count Garbage collection

7

Reference count

Explicit Erasure is Low level And Error-prone– Return a cell to free storage when it is no longer

needed (no longer accessible from the program)

Keep track of the accessibility of each cell– A cell is accessible only if it is referenced by the

other accessible cells.– The cells that are used by the interpreter are

Directly Accessible

Page 7: Storage Reclamation Explicit Erasure Reference Count Garbage collection

8

Reference count

Maintain correctly– Increment with additional reference– Decrement when a reference destroyed

Overwrite the pointer( rplaca , rplacd) The cell containing the pointer become inaccessible

(reference count becomes zero)

Free-list become exhausted no more free-cell available the program aborted– 95% of reference counts are 1

Page 8: Storage Reclamation Explicit Erasure Reference Count Garbage collection

9

Reference count

decrement (C) : reference count (C) := reference count (C) -1;if reference count (C) = 0 then

decrement (C^ left); decrement (C^ right); return C to free-list;

end if.

Page 9: Storage Reclamation Explicit Erasure Reference Count Garbage collection

10

Cyclic structures

Are not reclaimed. One solution: Disallow cyclic structures

– Error prone & difficult to understand But some say:

– Cyclic structures are necessary for some problems. Another solution

Page 10: Storage Reclamation Explicit Erasure Reference Count Garbage collection

11

3. Garbage Collection

Garbage : inaccessible cells which are not available for reuse

Inaccessible cells are abandoned After the exhaustion of the free space the system

enters a Garbage Collection Phase in which it identifies all of the unused cells and return them to free storage

– Ignores the storage reclamation until a crisis develops then deals with that

Mark-Sweep garbage collector– Mark phase : marks all of the accessible cells– Sweep phase : places all the inaccessible cells on the free

list

Page 11: Storage Reclamation Explicit Erasure Reference Count Garbage collection

12

Mark phase

Mark phase:

for each root R, mark(R)

mark (R) :

if R is not marked then:

set mark bit of R;

mark (R^. left);

mark (R^. right);

end if.

Page 12: Storage Reclamation Explicit Erasure Reference Count Garbage collection

13

Mark phase

There is a problem:– Mark phase is recursive

Requires space for it’s activation records.

– Garbage Collector is called only in crisis (no free storage available)

So how does this work???– Invoke G.C before the last cell is allocated and there is

enough space for G.C ‘s stack – Encode stack in a clever way (reversing link in the marked

nodes)

Page 13: Storage Reclamation Explicit Erasure Reference Count Garbage collection

14

sweep phase

Unmarked cell:– Inaccessible & can be linked on to the Free-list

Marked cell:– Accessible & we reset its mark bit for the next

garbage collection.

Sweep phase:for each cell c: if c is marked then reset c’s mark bit, else link c onto the free-list.

Page 14: Storage Reclamation Explicit Erasure Reference Count Garbage collection

15

Problems with Garbage Collection

Expensive in a large address space – It should trace down all of the lists– Visit every cell in the memory

There is a high-speed execution of the program until a garbage collection take place.

– The system will stop for minutes while a garbage collection is in the progress.

– This is apparent in an interactive system– A serious problem in real time situations (the program most

be guaranteed to respond at a certain amount of time ) solutions

– Parallel garbage collection: G.C takes place in parallel with normal program execution

Page 15: Storage Reclamation Explicit Erasure Reference Count Garbage collection

16

LISP Evaluation

Successful in Artificial Intelligence – Ability to represent and manipulate complex interrelationships

among symbolic data. Suited to iII-Specified Problems

– (In artificial intelligence) the problem will not well understood – Specification of Abstract Data Types:

1st decide what data structures and data types 2nd necessary operation in terms of their input and output Finally the representation of the data structures and operators fixed

– On ill-defined problems this methodology does not work well: What operations on a data type will be required is unknown. It is difficult to fix the input-output when ultimate requirements that the

data structure must satisfy are not clear. Lisp with few restriction on invocation of procedures and passing

parameters is well suited.

Page 16: Storage Reclamation Explicit Erasure Reference Count Garbage collection

17

Easy to extend, preprocess, generate

Lisp has simple structure syntax. The representation of the Lisp programs as Lisp lists. It has simplified writing programs that process other Lisp

programs such as compilers and optimizer. – It is very easy to manage Lisp programs using other Lisp

programs. ( As we saw in the eval interpreter )– Lisp programmers write many programming tools in Lisp

It has encouraged special–purpose extensions to Lisp for pattern matching, text processing, editing, type checking …

– This tools are provided by conventional languages– So why?

In conventional languages they are complicated because Pascal and … have character-oriented syntax

Page 17: Storage Reclamation Explicit Erasure Reference Count Garbage collection

18

Lisp programming environments developed

Programming Environment– A system that supports all phases of programming including

design, program entry, debugging, documentation, maintenance Lisp programs can manipulate other Lisp programs has led to

the development of a wide variety of Lisp programming tools. The Interlisp programming environment

– 1966: BBN( Bolt Beraneck Newman)– 1972: a joint project with Xerox PARC (Palo Alto Research Center)

that was renamed Interlisp.– Grew during 1970s in response to the needs and ideas of

implementers and users.– The combination of Lisp language and an experimental approach

to tool developmentproduced a highly integrated but loosely coupled and flexible programming environment.

Page 18: Storage Reclamation Explicit Erasure Reference Count Garbage collection

19

Lisp’s inefficiency discouraged it’s use

Why doesn’t every one use it? Because it is interpreted and often runs two orders of

magnitude slower than the code produced by the compiler

( 2*o(n) n-> compiler) recursion was inefficient on most machines

(we don’t have loop in Lisp) Dynamic storage allocation (and reclamation) is one

of the more expensive aspects but it is also one of the most valuable (it was a slow process)

Page 19: Storage Reclamation Explicit Erasure Reference Count Garbage collection

20

Languages

Imperative languages :– Dependent heavily on assignment statements and

a changeable memory for accomplishing a programming task.

Applicative languages:– The central idea is function application that is

applying a function to it’s arguments.

Page 20: Storage Reclamation Explicit Erasure Reference Count Garbage collection

21

Lisp shows what can be done with an applicative language

Lisp is the closest thing to an applicative language in widespread use.

The Lisp experience is evidence in favor of the practicality of functional programming languages.

Page 21: Storage Reclamation Explicit Erasure Reference Count Garbage collection

22

Function-oriented languages

There is an emphasis on the use of pure functions Syntactic structure:

– Prefix notation

Data structure:– List is the principle data structure

Control structure:– Conditional expression and recursion are basic control

structures.

Page 22: Storage Reclamation Explicit Erasure Reference Count Garbage collection

23

Function-oriented language properties

High level of abstraction which removes details and committing many classes of errors. (abstraction principle)

Independent of assignment statements, therefore allowing functions to be evaluated in many different orders.

– Suitable for parallel programming.

It is easier to prove the correctness mathematically and more suitable for analysis.