28 05 04 09 50
play

28.05.04 09:50 Memory Management The computer memory is a limited - PDF document

28.05.04 09:50 Memory Management The computer memory is a limited resource so the Memory Management memory use of programs has to be managed in some way. Memory Management The memory management is usually performed by a runtime system


  1. 28.05.04 09:50 Memory Management ♦ The computer memory is a limited resource so the Memory Management memory use of programs has to be managed in some way. Memory Management ♦ The memory management is usually performed by a runtime system with help from the compiler. ♦ The runtime system is a set of system procedures linked to the program. Advanced Compiler Techniques ♦ For C programs it can be as simple as a small library for 2004 interacting with the operating system. ♦ For Erlang programs the runtime system implements almost all Erik Stenman the functionality normally provided by the OS. EPFL 2 Advanced Compiler Techniques 28.05.04 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / Memory Management Memory Organization ♦ In a language such as C there are three ♦ A typical layout of the Memory Management Memory Management Stack Stack ways to allocate memory: memory of a C 1. Static allocation. The size of memory needed program looks like: by global variables (and code) is decided at compile time. Heap (dynami Heap (dynamic) c) Stack allocation. Activation records are 2. Uniniti Un initialized alized stat static da ic data ta (Global (G lobal varia variables) bles) allocated on the stack at function calls. Constan Co nstant stat t static da ic data ta Heap allocation. Dynamically allocated by the 3. programmer by the use of malloc . Code Code 3 Advanced Compiler Techniques 28.05.04 4 Advanced Compiler Techniques 28.05.04 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / Fragmentation Dynamic Memory Management Memory Management: Fragmentation Dynamic Memory Management ♦ Heap allocation is necessary for data that lives longer than ♦ The memory management system should try to avoid the function which created it, and which is passed by fragmentation , i.e. when the free memory is broken up into reference, e.g., lists in misc. several small blocks instead of few large blocks. ♦ Two design questions for the heap: ♦ In a fragmented system memory allocation may fail because there is no free block that is large enough even ♦ How is space for data allocated on the heap? though the total free memory would be large enough. ♦ How and when is the space deallocated? ♦ Considerations in memory management design: ♦ We distinguish between: ♦ Internal fragmentation – the allocated block is larger than the ♦ Space leaks & dangling pointers. requested size (the waste is in the allocated data). ♦ The cost for allocation and deallocation. ♦ External fragmentation – all free blocks are too small (the waste is ♦ Space overhead of the memory manager. in the layout of the free data). ♦ Fragmentation. Advanced Compiler Techniques 28.05.04 Advanced Compiler Techniques 28.05.04 5 6 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / 1

  2. 28.05.04 09:50 Memory Allocation Free-list In use Free Memory Management: Allocation Memory Management: Allocation ♦ The use of a free-list is a common scheme. ♦ The free-list can be stored in the ♦ The system keeps a list of unused memory blocks. free memory since it is not used for ♦ To allocate memory the free-list is searched to find a block anything else. (We assume, or ensure, which is large enough. that each memory block is at least two 4 4 ♦ The block is removed from the free-list and used to store words). the data. If the block is larger than the need, it is split and 2 2 the unused part is returned to the free-list ( to avoid internal Free list: fragmentation ). ♦ When the memory is freed it is returned to the free-list. This can be 3 3 Adjacent memory blocks can be merged (or coalesced) stored as a into larger blocks ( to avoid external fragmentation ). static global variable. 7 Advanced Compiler Techniques 28.05.04 8 Advanced Compiler Techniques 28.05.04 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / Free-list Free-list Memory Management: Allocation Memory Management: Allocation ♦ Note that we need to know the size of a block ♦ There are many different ways to when it is deallocated. This means that even implement the details of the free-list allocated blocks need to have a size field in them. algorithm: ♦ Thus the space overhead will be at least one word ♦ Search method: first-fit, best-fit, next-fit. per allocated data object. (It might also be ♦ Links: single, double. advantageous to keep the link.) ♦ Layout: one list, one list per block size, tree, ♦ The cost (time) of allocation/deallocation is buddy. proportional to the search through the free-list. 9 Advanced Compiler Techniques 28.05.04 10 Advanced Compiler Techniques 28.05.04 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / Deallocation Explicit Deallocation Memory Management: Deallocation Memory Management: Deallocation ♦ Explicit deallocation has a number of ♦ Deallocation can either be explicit or problems: implicit . ♦ If done too soon it leads to dangling pointers. ♦ Explicit deallocation is used in e.g., Pascal ♦ If done too late (or not at all) it leads to space ( new/dispose ), C ( malloc/free ), and C++ leaks. ( new/delete ). ♦ In some cases it is almost impossible to do it at ♦ Implicit deallocation is used in e.g., Lisp, the right time. Consider a library routine to append two mutable lists: Prolog, Erlang, ML, and Java. c = append(a,b); Advanced Compiler Techniques 28.05.04 Advanced Compiler Techniques 28.05.04 11 12 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / 2

  3. 28.05.04 09:50 Explicit Deallocation Explicit Deallocation Memory Management: Deallocation Memory Management: Deallocation ♦ The programmer list a = new List(1,2,3); list a = List(1,2,3); now has to ensure list b = new List(4,5,6); that a , b , and c are list b = List(4,5,6); NIL NIL NIL NIL all deallocated at the list c = append(a,b); 6 6 list c = append(a,b); same time. A mistake printList(c); would lead to printList(c); 5 5 doLotsOfStuff(); dangling pointers. doLotsOfStuff(); 4 4 printList(b); ♦ If b is in use long printList(b); free(c); after a , and c , then 3 3 we will keep a live too long. A space 2 2 leak. 1 1 13 Advanced Compiler Techniques 28.05.04 14 Advanced Compiler Techniques 28.05.04 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / Implicit Deallocation Garbage Collection (GC) Memory Management: Deallocation ♦ With implicit deallocation the programmer does not ♦ Garbage collection is a common name for a set of have to worry about when to deallocate memory. Garbage Collection techniques to deallocate heap memory that is ♦ The runtime system will dynamically decide when unreachable by the program. it is safe to do this. ♦ There are several different base algorithms: ♦ In some cases, and systems, the compiler can also add static dealloctions to the program. reference counting , mark & sweep , copying . ♦ The most commonly used automatic deallocation ♦ We can also distinguish between how the GC method is called garbage collection (GC). interferes or interacts with the program: ♦ There are other methods such as region based disruptive , incremental , real-time , concurrent . allocation and deallocation. 15 Advanced Compiler Techniques 28.05.04 16 Advanced Compiler Techniques 28.05.04 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / The Reachability Graph The Reachability Graph Garbage Collection: The reachability graph Garbage Collection: The reachability graph ♦ The data reachable by the program form a list a = List(1,2,3); directed graph, where the edges are pointers. list b = List(4,5,6); NIL NIL NIL NIL ♦ The roots of this graph can be in: 6 6 list c = append(a,b); global variables, 1. printList(c); 5 5 2. registers, local variables & formal parameters on the stack. 3. doLotsOfStuff(); 4 4 ♦ Objects are reachable iff there is a path of edges return b; 3 3 that leads to them from some root. Hence, the 2 2 compiler must tell the GC where the roots are. 1 1 Advanced Compiler Techniques 28.05.04 Advanced Compiler Techniques 28.05.04 17 18 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / 3

Recommend


More recommend