5. memory management from: chapter 5, modern compiler design, by dick grunt et al

29
5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al.

Upload: johnathan-roberts

Post on 16-Dec-2015

226 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al

5. Memory Management

From: Chapter 5, Modern Compiler Design, by Dick Grunt et al.

Page 2: 5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al

Memory Management 2

Introduction

• When a program is started, most operating systems allocate at least the following three memory segment for it:– The ‘code segment’

• Read-only or execute only

– The ‘stack segment’ • With overflow and underflow detection mechanism

• Addressed by one or more stack pointers, automatic manipulation by machine instructions;

– The ‘data segment’• A single contiguous stretch of memory locations, at the disposition of program

for the purpose of storing data;

• Also called heap from the aspect of memory management

Page 3: 5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al

Memory Management 3

Introduction

• It is the task of memory management to hand out and collect subsegments of the data segment such that– All subsegments fit inside the data segment and no memory location is

ever part of more than one subsegment.

• Allocating memory in the heap is easy enough:– We keep a pointer to the first free location in the heap,

– Allocate the request block from there, and

– Bump the pointer to the next free location.

• In this chapter– Techniques for data allocation with explicit deallocation by the

programmer, and

– Techniques for data allocation with implicit deallocation.

Page 4: 5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al

Memory Management 4

5.1 Data allocation with explicit allocation

• In most systems, basic memory allocation comes in the form of routine that – finds a block of unused memory of the requested size,

– Marks it as used, and

– Returns a pointer to the block.

• If no such block is available, the results varies– A null pointer may be returned,

– And error routine may be called, or the program may be aborted.

• This is available in C as the routines void *malloc(size_t size) and free(*void *ptr)

Page 5: 5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al

Memory Management 5

5.1 Data allocation with explicit allocation

• Considerations of the allocation and deallocation of two classes of data types are in great demand in compilers.– Linked lists and– Extensible arrays

• Some modern languages (notably the functional and logic languages, and Java) have automatic data allocation and deallocation mechanisms, but – in practice almost all compilers are written in a traditional language, such

as C or C++.

• Data allocation and deallocation in these languages require considerable care, and experience has shown that it is very advantageous in compiler writing to organize memory management properly and systematically.

Page 6: 5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al

Memory Management 6

5.1.1 Basic memory allocation

Page 7: 5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al

Memory Management 7

Page 8: 5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al

Memory Management 8

Page 9: 5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al

Memory Management 9

Page 10: 5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al

Memory Management 10

5.1.1.1 Optimization for basic memory allocation

• Efficiency problem with the above implementation1. Finding a suitable chunk in the free list requires linear search through the

entire memory, which is unacceptable.2. Coalescing is done in a separate phase, performed only when the usual

linear search fails; this makes the performance of the algorithm irregular

• Solving the first problem– Chaining the free chunk in a linked list– Classifying the free chunks according to size and keeping a free list for

each interval

• Solving the second problem– Coalescing can be done one the fly, during each call of Free if we have

easy access to the chunk preceding the one being freed;– The one following it is already within easy reach, using the size of the

chunk being freed.

Page 11: 5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al

Memory Management 11

5.1.2 Linked lists

• Pages 403~404.

Page 12: 5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al

Memory Management 12

5.1.3 Extensible arrays

• Pages 404~407.

Page 13: 5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al

Memory Management 13

5.1 Data allocation with implicit allocation

• Implicit deallocation, or garbage collection, is the automatic reallocation of memory that is no longer in use by the application program.– Relieving the programmer from the error-prone task of reclaiming

memory manually by using an explicit free() primitive.

• Correctly freeing blocks in handwritten code requires a considerable insight into the dynamics of the program.

• Errors hard to find and correct in freeing memory– Problems of dangling pointers

– Hard to reproduce the errors

Page 14: 5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al

Memory Management 14

5.1 Data allocation with implicit allocation

• Garbage collection is considered to be an important feature of modern programming systems that reduces programming efforts considerably.

• Examples of programming systems offering garbage collection– OO languages, like Java and Smalltalk,

– Functional languages, like ML, Kaskell, login language, like Prolog, and

– Scripting languages like awk and perl.

Page 15: 5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al

Memory Management 15

5.2.1 Basic garbage collection algorithms

• The objective of garbage collection is to reclaim automatically the set of memory chunks that will no longer be used by the program, the garbage set.– Unattainable, because no automatic method can determine what the

program is going to do

• Practical approximations for the garbage set– ‘the set of all chunks to which there are no pointers,” and

– ‘the set of all chunks that are not reachable from the non-heap-allocated program data.’

• These approximations are safe because– No chunk in either of these sets can be in use in the program.

Page 16: 5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al

Memory Management 16

5.2.1 Basic garbage collection algorithms

• The ‘no-pointers’ criterion leads to a technique called reference counting, and the ‘not-reachable’ criterion is exemplified by two techniques, mark scan and two-space copying.

• The three techniques are all different:– Reference counting directly identifies garbage chunks.

• Simple and reasonably efficient but requires all pointer actions to be monitored during program execution and may not recover all garbage chunks.

– Mark and scan identifies reachable chunks and concludes that the rest is garbage.• Reasonably efficient and not requires pointer monitoring.

• Quite complicated; the only one that will recover all available memory

– Two-space copying is not concerned with garbage.• Copying the reachable chunks from the ‘from-space’ memory region to ‘to-space’ region;

the remaining space in to-space is a single free chunk.

• Very efficient, no pointer monitoring required, moderately complicated, waste half of the memory

Page 17: 5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al

Memory Management 17

5.2.1 Basic garbage collection algorithms

• Memory fragmentation– Do memory compaction

• Varieties of garbage collection algorithms– One-shot

– On-the-fly (also called incremental)

– Concurrent

Page 18: 5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al

Memory Management 18

Page 19: 5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al

Memory Management 19

Page 20: 5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al

Memory Management 20

Page 21: 5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al

Memory Management 21

Page 22: 5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al

Memory Management 22

Page 23: 5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al

Memory Management 23

Page 24: 5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al

Memory Management 24

Page 25: 5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al

Memory Management 25

Page 26: 5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al

Memory Management 26

Page 27: 5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al

Memory Management 27

Page 28: 5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al

Memory Management 28

Page 29: 5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al

Memory Management 29