Dynamic Memory Management - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Dynamic Memory Management

Description:

Memory Maps ... blank (1 page) blank (thousands of pages) stack. The Heap ... The memory map (defined by the OS) for the program locates the heap memory out ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 22
Provided by: craigm6
Category:

less

Transcript and Presenter's Notes

Title: Dynamic Memory Management


1
Dynamic Memory Management
2
Memory Maps
  • When the OS loads a program, it defines a memory
    map for the program using virtual memory.
  • The OS decides which pages in the address space
    are available for code, stack, data, and heap.
  • Not all of these pages will be in physical
    memory.
  • Accesses to locations outside of the map result
    in
  • GP Fault (Windows)
  • Segmentation fault (Unix)

3
The a.out Map
  • a.out was the original map used in Unix. Its
    very simple, but provides limited support for
    dynamic linking (and is not used any longer in
    the major operating systems).

4
The Heap
  • The heap is used for all dynamic memory
    allocations.
  • C new/delete
  • C malloc/free
  • Java new/(garbage collection)
  • At its most simple, a heap is a very large array
    allocated by the operating system and managed by
    the program.
  • malloc/free are just subroutines.

5
Slicing Chunks
  • The memory map (defined by the OS) for the
    program locates the heap memory out of the way of
    all other variables and machine code.
  • The new subroutine slices a chunk off this array
    and returns a pointer to the chunk.
  • The delete subroutine takes back a chunk youd
    been using before.

6
Dynamic Memory Syntax
  • Operator new returns a pointer and takes a type
    as a pseudo-argument (the type is important as it
    specifies how many bytes long the chunk must be).
  • int p new int // allocates 1 int variable
  • You can also allocate an array
  • int my_array new int20
  • The size of a dynamically allocated array can be
    any arbitrary positive integer (i.e., its an
    expression).

7
Semantics of New/Delete
  • We allocate and deallocate chunks, not pointers!
  • delete p // deletes the chunk pointed to by p
  • The new operator requires two steps.
  • allocate a chunk of memory in the heap.
  • operator new (can be redefined in C)
  • invoke the constructor function.
  • The operator delete also requires two steps.
  • invoke the destructor function.
  • deallocate the chunk
  • operator delete (can be redifined in C)

8
But, How does it Work?
  • There are gazillions of different memory
    allocation strategies. Donald Knuth is credited
    with one of the most straightforward,
    general-purpose algorithms.
  • First, well make a few preliminaries.
  • assume heap is an array (of int) we can use.
  • a chunk will consist of one or more elements of
    the heap array.
  • initially, the heap has just one junk (a really
    big one).

9
Chunks and Signatures
  • Each chunk begins with signature.
  • the signature is an int (obviously)
  • positive indicates the chunk is available
  • negative indicates the chunk is in use.
  • the magnitude of the signature indicates the size
    of the chunk.
  • Each chunk ends with a copy of its signature.
  • Whats the smallest possible chunk?
  • Three elements (two signatures and at least 1
    element to hold the data).

10
Allocation Give the Customer more than they ask
for
  • When someone calls new the system must identify a
    chunk thats at least as large as the amount
    requested. Using an extra-big chunk is OK.
  • The user is supposed to only use the amount of
    space they request. In fact, they really
    shouldnt ever notice whether we gave them
    exactly that amount, or if we gave them more.

11
Splitting Chunks
  • We will try to keep as many big chunks as we can
    (well, while still keeping things simple).
    Frequently, when someone allocates memory, well
    find a big chunk and carve a small piece off for
    them.
  • Note that we wont split a chunk unless both of
    the pieces are bigger than some minimum size.
    The minimum size is at least 1 word of useful
    space plus two words to hold the signatures
    (total of 3).

12
Example
p malloc(2 sizeof (int))
  • Initially, one big chunk
  • Allocating 2 words splits chunk makes sigs
    negative

13
Example Continued
p malloc(2 sizeof (int)) q
malloc(3sizeof(int))
  • The remaining chunk is not big enough to split.
  • q gets all four words (programmer only asked for
    3).

14
Deallocating Memory
  • When our user is done with the memory they asked
    for, they should return the memory to us by
    calling delete.
  • Our first task is identifying which chunk theyre
    returning.
  • We can find the signature by subtracting 1 from
    the address they were using!
  • This is because the space inside a chunk begins
    immediately after the signature, i.e., the
    signature can be found immediately before the
    first address of the memory the user was using.

15
Delete Example
p malloc(8) q malloc(12)
delete p
  • When a chunk is deleted, only signatures are
    changed!
  • Pointer still points to memory.

16
Marking and Merging Chunks
  • Once weve identified the chunk and have found
    the signature, we change the sign (making it
    positive) of both signatures.
  • Our last step is to combine any adjacent,
    available, small chunks into an available big
    chunk.
  • In the worst case, there is one available chunk
    immediately before this chunk, and also one
    available chunk immediately after this chunk.
  • why cant there be more? cause we always combine
    chunks if its possible, so if there were two
    chunks in a row that are available, wed have
    already combined them!

17
How to Merge
  • To merge a chunk with its successor, simply
    recalculate what the combined size is (its the
    sum of the sizes of the two chunks plus two).
  • Write this new size into the bottom signature on
    the bottom chunk and replace the top signature on
    the top chunk with the same size.
  • We dont need to erase the old signatures (which
    are now in the middle of the chunk). Since the
    signatures are not at the beginning or end of any
    of our chunks, no one will ever see them there!
  • I suppose, if you want to be neat, you can reset
    these signatures to zero, no harm done.

18
Unix and sbrk
  • The memory for the heap is (usually) a contiguous
    set of virtual addresses (i.e., pages) granted by
    the operating system.
  • In the old a.out systems, unix provided the
    sbrk() system call (set break) that would move
    the boundary at the end of the heap (i.e.,
    increasing the maximum allowed virtual address
    (outside the stack)).
  • malloc and free could call sbrk when they ran out
    of memory. The new memory is ready to use as
    soon as malloc writes two new signatures into the
    newly allocated pages.

19
Common Memory Management Errors and Their Symptoms
  • Error 1. Writing past the end (or before the
    beginning) of an object.
  • allocating an array of 10 words and writing 11
  • typecasting (incorrectly) a pointer to type T
    into a pointer of type S (sizeof(S) larger than
    sizeof(T).
  • Symptom program crashes during malloc
  • (or much much much worse!)

20
Errors Cont.
  • Error 2 Using an object after it has been
    deleted.
  • delete p p 42
  • Bad copy constructor (copies pointers) when
    destructor calls delete.
  • Symptom unrelated variables/objects change at
    random for no apparent reason.
  • this is really ugly.

21
Errors Cont.
  • Error 3 Memory Leak
  • failing to delete an object (bad destructor)
  • assigning a pointer to a new object before
    deleting the old object (bad assignment
    operator).
  • Symptom program runs fine (for a short period of
    time), but uses more and more memory as the
    program runs. Eventually, we run out of memory.
Write a Comment
User Comments (0)
About PowerShow.com