Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations

Presentation on theme: "5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al."— Presentation transcript:

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

2 Memory Management2 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

3 Memory Management3 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.

4 Memory Management4 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)

5 Memory Management5 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.

6 Memory Management6 5.1.1 Basic memory allocation

7 Memory Management7

8 8

9 9

10 10 Optimization for basic memory allocation Efficiency problem with the above implementation 1.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.

11 Memory Management11 5.1.2 Linked lists Pages 403~404.

12 Memory Management12 5.1.3 Extensible arrays Pages 404~407.

13 Memory Management13 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

14 Memory Management14 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.

15 Memory Management15 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.

16 Memory Management16 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

17 Memory Management17 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

18 Memory Management18

19 Memory Management19

20 Memory Management20

21 Memory Management21

22 Memory Management22

23 Memory Management23

24 Memory Management24

25 Memory Management25

26 Memory Management26

27 Memory Management27

28 Memory Management28

29 Memory Management29

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

Similar presentations

Ads by Google