Storage Management

30
COMPARATIVE STUDY OF PROGRAMMING LANGUAGES STORAGE MANAGEMENT Lecture 8 continued

Transcript of Storage Management

Page 1: Storage Management

COMPARATIVE STUDY OF PROGRAMMING LANGUAGES

STORAGE MANAGEMENT

Lecture 8 continued

Page 2: Storage Management

5.4. STORAGE MANAGEMENT

Different features in a language causes different storage management techniques to be used. FORTRAN: no recursive calls, no dynamic storage

management. Pascal: stack-based storage management. LISP: garbage collection. Language implementers decide about the details. Programmers don’t know about it.

2

Page 3: Storage Management

MAJOR RUN-TIME ELEMENTS REQUIRING STORAGE

Data operations

3

Page 4: Storage Management

DATA AND PROGRAM REQUIRING STORAGE

Code segments for translated user programs. System run-time programs.

Supporting user programs. Like library routines, software interpreters or

translator, storage management routines. User-defined data structures and constants. Subprogram return points.

4

Page 5: Storage Management

Referencing environments. Identifier associations (LISP A-list)

Temporaries in expression evaluation. Recursive function calls make it a lot.

Temporaries in parameter transmission. Resulting values for evaluation of actual

parameters are stored in temporaries until the total evaluation is completed.

5

Page 6: Storage Management

Input-output buffers. Temporary storage areas used between the time

of the actual physical transfer of the data to or from external storage and the program-initiated input / output operation.

Miscellaneous system data. System data like tables, status information for

input-output, ...

6

Page 7: Storage Management

MAJOR OPERATIONS REQUIRING STORAGE

Subprogram call and return operations. Activation record, local referencing environment, …

Data structure creation and destruction operations. new - dispose in Pascal. malloc - free in C

Component insertion and deletion operations. ML and Lisp list operations, inserting a

component into a list.7

Page 8: Storage Management

PROGRAMMER- AND SYSTEM- CONTROLLED STORAGE MANAGEMENT

Programmer control of storage management place a large and often undesirable burden on

the programmer, may interfere with the necessary system-

controlled storage management. Programmer can cause dangling references

and garbage.

8

Page 9: Storage Management

What language? Protection for the programmer by using a

language with strong typing and effective storage management features,

decrease in performanceOR performance more risk in having errors and fail during

execution.

9

Page 10: Storage Management

STORAGE MANAGEMENT PHASES

Initial allocation Recovery Compaction and reuse

10

Page 11: Storage Management

STATIC STORAGE MANAGEMENT

Simplest static allocation no run-time storage management no concern for recovery and reuse efficient in COBOL and FORTRAN

11

Page 12: Storage Management

In FORTRAN each subprogram is compiled separately, the code segment includes an activation record

compiled program, its data areas, return point location, miscellaneous items of system data.

12

Page 13: Storage Management

STACK-BASED STORAGE MANAGEMENT

Simplest run-time storage management technique.

Based on the nested last in first out structure in subprograms calls and returns.

Automatic compaction. In Pascal : a single central stack of activation

records, and a statically allocated area for subprogram code segments and system programs.

P.222, fig. 5.5

13

Page 14: Storage Management

HEAP STORAGE MANAGEMENT: FIXED-SIZE ELEMENTS

A heap is a block of storage within which pieces are allocated and freed in some relatively unstructured manner.

Need for heap , when a language permits storage to be allocated and freed at execution time.

Fixed size elements allocated => no need for compaction.

14

Page 15: Storage Management

RECOVERY

The problem: identification of reusable element, solutions:

Explicit return by programmer or system. Natural, but cause garbage and dangling

reference. P.226 Reference counts.

Cost of maintaining. P.227 popular with parallel processing systems.

Garbage collection.

15

Page 16: Storage Management

GARBAGE COLLECTION Dangling references more dangorous Two stages

Mark garbage collection bit, set off if it is active.

Sweep links the “on” elements to the free list.

When is a heap element active? There is a pointer to it from

outside the heap another active heap element

16

Page 17: Storage Management

Three critical assumptions any active element must be reachable by a chain

of pointers beginning outside the heap. It must be possible to identify every pointer

outside the heap that points to an element inside the heap.

It must be possible to identify within any active heap element the fields that contain pointers to other heap elements.

P. 231

17

Page 18: Storage Management

HEAP STORAGE MANAGEMENT:VARIABLE-SIZE ELEMENTS

More difficult if space for programmer defined data

structures is sequential, like arrays or activation records.

Major difficulty : reuse of recovered space.

18

Page 19: Storage Management

Initial allocation and reuse. reuse directly from a free-space list.

First-fit method best-fit methodkeeping free-space list in size order.

Recovery with variable-size blocks. In first word of each block: a length indicator.

Compaction and memory fragmentation problem.

19

Page 20: Storage Management

Compaction approaches: Partial compaction

only adjacent free blocks Full compaction

active blocks may be shifted

20

Page 21: Storage Management

STORAGE MANAGEMENT Elements requiring storage

Programmer and system controlled storage

Static Storage Management

Heap Storage Management

Page 22: Storage Management

ELEMENTS REQUIRING STORAGE

Code segments for translated user programs

System run-time programs - e.g. libraries

User defined data structures and constants

Subprogram return points

Referencing environments

Page 23: Storage Management

ELEMENTS REQUIRING STORAGE

Temporaries in expression evaluation

Temporaries in parameter transmission

Input-Output buffers

Miscellaneous system data - tables etc

Page 24: Storage Management

OPERATIONS THAT REQUIRE STORAGE ALLOCATION

Subprogram call and return operations - creation and deletion of activation records

Explicit data structure creation and destruction operations

Component insertion and deletion operations

Page 25: Storage Management

PROGRAMMER AND SYSTEM CONTROLLED STORAGE

Pros and cons:Programmer knows when to allocate / free storage.

Programmer may interfere with the system-controlled storage management

Storage-Management PhasesInitial allocationRecoveryCompaction and reuse

Page 26: Storage Management

STATIC STORAGE MANAGEMENT

Static allocation :

allocation during translation that

remains fixed throughout

execution.

Does not allow recursive

subprograms

Page 27: Storage Management

DYNAMIC ALLOCATION:HEAP STORAGE MANAGEMENT

Memory used for dynamic allocation of data objects in somewhat unstructured manner is called heap storage.

OS

HEAP

STACK

Page 28: Storage Management

HEAP STORAGE MANAGEMENT

Tasks: allocation, recovery,

dangling referencesgarbage collection

compaction, reuse

Fixed size elementsVariable size elements

Page 29: Storage Management

HEAP COMPACTION

used used used

used

used used

used

Page 30: Storage Management

HEAP COMPACTION

used used used

used

used usedused