memory management
play

Memory management programs use a lot of memory over their lifetime, - PDF document

Memory management The memory of a computer is a finite resource. Typical Memory management programs use a lot of memory over their lifetime, but not all of it at the same time. The aim of memory management is to use that finite resource as


  1. Memory management The memory of a computer is a finite resource. Typical Memory management programs use a lot of memory over their lifetime, but not all of it at the same time. The aim of memory management is to use that finite resource as efficiently as possible, according to some Michel Schinz – based on Erik Stenman’s slides criterion. Advanced compiler construction, 2008-03-07 2 Memory areas Memory organisation The three areas just described can be organised as follows in Every piece of memory used by a program is allocated from the address space of a running program: one of three different areas: • A static area, which is laid out at compilation time and high addresses Stack allocated when the program starts. The static area is (grows downward) used to store global variables and constants. • A stack , from which memory is allocated and freed dynamically, in LIFO order. The stack is used to store the arguments and local variables of functions, since in most languages function calls happen in LIFO order. Heap (grows upward) • A heap , from which memory is allocated and freed dynamically, in any order. The heap is used to store Static area and code objects that outlives the function that created them. (does not grow) low addresses 3 4 The memory manager Managing the static area and the stack is trivial. Managing the heap is much more difficult because of the irregular lifetimes of the blocks it contains. The memory manager is the part of the run time system in charge of managing heap memory. The memory manager Its job consists in answering to two kinds of requests: 1. allocation requests, which consist in finding a free block of memory big enough to satisfy the request, remove it from the set of free blocks, and return it to the program, 2. deallocation requests, which consist in returning a previously-allocated block to the set of free blocks, to make it available for further allocation requests. 6

  2. Free list Free list storage Since the blocks stored in the free list are by definition not used by the program, the memory manager can store information in them! The memory manager must keep track of which parts of the heap are free, and which are allocated. For example, if the free list is represented as a singly linked list, then the pointer to the next block can be stored in the For that purpose, free memory blocks are stored in a data- blocks themselves: structure called the free list . Notice that the term free list is heap used even when the data-structure used to track free memory is not a list. head of There is no need to keep a list of allocated blocks, as it can free list be computed using the free list – all blocks that are not in the free list are allocated. 7 8 Block header Splitting and coalescing Apart from the link to their successor and/or to their predecessor, free blocks must contain their size. Allocated blocks do not require links to other blocks, but When the memory manager has found a free block big must also contain their size. enough to satisfy an allocation request, it is possible for that This information is stored in the block’s header , situated just block to be bigger than the size requested. In that case, the before the area used by the client, and invisible to it. block must be split in two parts: one part is returned to the client, while the other is put back into the free list. free block allocated block The opposite must be done during deallocation: if the block pointer size size header being freed is adjacent to one or two other free blocks, then returned previous they all should be coalesced to form a bigger free block. to client next area used by the client (unused area) 9 10 Fragmentation External fragmentation The following two heaps have the same amount of free memory, but the first suffers from external fragmentation while the second does not. As a consequence, some requests can be fulfilled by the second but not by the first. The term fragmentation is used to designate two different but similar problems associated with memory management: 1. external fragmentation refers to the fragmentation of fragmented f a f a f a free memory in many small blocks, not fragmented a f 2. internal fragmentation refers to the waste of memory due to the use of a free block larger than required to satisfy an allocation request. a allocated block f free block 11 12

  3. Internal fragmentation For various reasons – e.g. alignment constraints – the memory manager sometimes allocates slightly more memory than requested by the client. This results in small amounts of wasted memory scattered in the heap. This phenomenon is called internal fragmentation . Memory allocation allocated size requested size memory block wasted memory 13 Allocation policies First fit, next fit When a block of memory is requested, there are in general First fit chooses the first block in the free list big enough to many free blocks big enough to satisfy the request. satisfy the request, and splits it if necessary. An allocation policy must therefore be used to decide which Next fit is like first fit, except that the search for a fitting of those candidates to choose. A good allocation policy block starts where the last one ended, instead of at the should minimise fragmentation while being fast to beginning of the free list. implement. It appears that next fit results in significantly more There are several such policies: first fit, next fit, best fit, worst fragmentation than first fit, as it mixes blocks allocated at fit, etc. very different times. 15 16 Best fit, worst fit Segregated free lists Best fit chooses the smallest block big enough to satisfy the Instead of having a single free list, it is possible to have request. several of them, each holding free blocks of (approximately) Worst fit chooses the biggest, with the aim of avoiding the the same size. creation of too many small fragments. It doesn’t work well in These segregated free lists are organised in an array, to practice. quickly find the appropriate free list given a block size. The major problem of these techniques is that they require When a given free list is empty, bigger blocks taken from an exhaustive search of the free list, unless segregation adjacent lists are split in order to repopulate it. techniques are used. 17 18

  4. Buddy systems Allocation in a buddy system This example illustrates how a 10 bytes block is allocated in a binary buddy system with a heap of 256 bytes, initially Buddy systems are a variant of segregated free lists. free. The heap is initially viewed as one large block that can be split in two smaller blocks – called buddies – of a given size. 256 Those smaller blocks can again be split in two smaller buddies, and so on. 128 In a binary buddy system , a block is split in two buddies of 64 the same size. In a Fibonacci buddy system , a block is split 32 in two buddies whose size is given by a Fibonacci sequence ( s n = s n-1 + s n-2 ). 16 Coalescing is fast in buddy systems, since a block can only 8 be coalesced with its buddy, provided it is free too. allocated block (wastes 6 bytes) 4 19 20 Memory deallocation In a programming language, deallocation of heap memory can be either explicit or implicit . It is explicit when the language offers a way to declare a Memory deallocation memory block as being free – e.g. using delete in C++ or free() in C. It is implicit when the run time system infers that information itself, usually by finding which allocated blocks are not reachable anymore. 22 Explicit deallocation Implicit deallocation Explicit memory deallocation presents several problems: Implicit memory deallocation is based on the following 1. memory can be freed too early, which leads to conservative assumption: dangling pointers – and then to data corruption, If a block of memory is reachable, then it will be used crashes, security issues, etc. again in the future, and therefore it cannot be freed. Only 2. memory can be freed too late – or never – which leads unreachable memory blocks can be freed. to space leaks . Since this assumption is conservative, it is possible to have Due to these problems, most recent programming languages space leaks even with implicit memory deallocation. This are designed to provide implicit deallocation , also called happens whenever a reference to a memory block is kept, automatic memory management – or garbage collection , but the block is not accessed anymore. even though garbage collection refers to a specific kind of However, implicit deallocation prevents dangling pointers. automatic memory management. 23 24

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend