You are on page 1of 7

Memory Allocation

Dynamic Storage Allocation


 Static Allocation (fixed in size)
 Sometimes we create data structures that are “fixed” and
don’t need to grow or shrink.
CS 414: Operating Systems  Dynamic Allocation (change in size)
 At other times, we want to increase and decrease the
Fall 2005 size of our data structures to accommodate changing
needs.
Presented By: Vibha Prasad
 Often, real world problems mean that we don’t
know how much space to declare, as the number
needed will change over time.

Static Allocation Dynamic Allocation


 Done at compile time.  Done at run time.
 Global variables: variables declared “ahead of  Data structures can grow and shrink to fit changing data
time,” such as fixed arrays. requirements.
 We can allocate (create) additional storage whenever we need them.
 Lifetime = entire runtime of program  We can de-allocate (free/delete) dynamic space whenever we are
 Advantage: efficient execution time. done with them.
 Advantage: we can always have exactly the amount of space
 Disadvantage? required - no more, no less.
 If we declare more static data space than we need, we  For example, with references to connect them, we can use
waste space. dynamic data structures to create a chain of data structures
 If we declare less static space than we need, we are out of called a linked list.
luck.

Dynamic Allocation Memory Management

 Local Variables  User-allocated variables  The memory of a process is divided into the
 Lifetime = duration of  Lifetime = until the user deletes
it (or until it is garbage-
following parts
procedure activation (as long
as that method is active) collected)  A space for the code of the program
 Advantage: efficient storage  Advantage: permits creation of  A space for the data (global variables)
use. dynamic structures like trees,
 Stack Allocation (all linked lists etc.  The stack, for the local variables (static allocation)
variables allocated within  Heap Allocation.  The heap, for the dynamic variables (dynamic allocation)
the procedure scope unless  For example, linked lists.
declared static).  The division between 1 and 2 is only logical:
 For example, parameter physically the globals and the code are placed at the
passing during function
calls. base of the stack.

1
Logical Memory Components Memory of a Process
Memory area for Memory area private to Memory area available Logical View Physical Memory Mapping
storing a program each program for to all programs for
(application) and storing local variables dynamic data structures Stack (Static Area) n
Heap (Dynamic Area)
global variables (Local variables go here)
Heap (Dynamic variables
go here)
Stack A Stack B Stack C :
Heap (Dynamic Area) :
Program A Program B Program C
Stack (Static Area) (Dynamic variables :
(Local variables go here) go here) :
Operating System Data (global variables)
Data (global variables)
Module Code Module Code
Driver Driver Driver Driver Driver 0

Why isn’t static allocation


sufficient? Dynamic Memory Management

 Recursive procedures  Dynamic memory allocation is performed by the


 Complex data structures operating system when the program is executing
 If all storage must be reserved in advance (statically), and the program (user) sends a request for
then it will be used inefficiently (enough will be reserved
to handle the worst possible case). additional memory.
OS cannot predict  Three issues need to be addressed by the operating
 how many jobs there will be or system:
 which programs will be run or  allocating variable-length dynamic storage when needed
 when a process will come and ask for storage  freeing up storage when requested
space!!!!!!  managing the storage (e.g. reclaiming freed up memory
for later use)

Dynamic Storage Allocation


methods Stack-Based Allocation

 Stack Allocation  Memory allocation and freeing are partially


 Used to allocate local variables. predictable.
 Grown and shrunk on procedure calls and returns.
 Restricted but simple and efficient.
 Register allocation works best for stack-allocated objects.
 Heap Allocation  Allocation is hierarchical: Memory freed in
 Used to allocate dynamic objects. opposite order of allocation.
 Heap objects are accessed with pointers.  If alloc(A) then alloc(B) then alloc(C), then it
 Never allocated to registers. must be free(C) then free(B) then free(A).

2
Stack-Based Allocation: Example Stack-Based Allocation: Example

 Procedure call:  A stack-based organization keeps all the free


 Program calls Y, which calls X. Each call pushes
space together in one place.
another stack frame on top of the stack. Each
Local
stack frame has space for variable, parameters,
variables
and return addresses. Stack
Frame for Y
 Stacks are also useful for tree traversal, Parameters
expression evaluation, top-down recursive Stack Stack Stack
Return
parsers etc. address
Frame for X Frame for X Frame for X

After call to X after call to Y after exiting Y after exiting X

Heap Organization Heap Organization

 Allocation and release are unpredictable.  Memory consists of


allocated areas and free
 Heaps are used for arbitrary list structures, areas (holes).
complex data organizations.  Goal
 Reuse the spaces in holes.
 More general, less efficient.  Keep the no. of holes small.
 Keep the size of holes large.
 Example: payroll system.  Problem: Fragmentation!! 0 .... n
 Do not know when employees will join and leave the  Holes may be too small to be
useful for any allocation. Memory in use
company.
 Inefficient use of memory.
 Must be able to keep track of all of them using the least Memory not in use

possible amount of storage.

Fragmentation and Compaction Heap Allocation Methods

 External fragmentation  Typically, heap allocation schemes use a free list to keep
track of the storage that is not in use.
 Total memory space exists to satisfy a request, but
it is not contiguous.  Algorithms differ in how they manage the free list
 Best Fit
 Internal fragmentation  Keep linked list of free blocks.
 Allocated memory may be slightly larger than  Search the whole list on each allocation.
requested memory; holes in the memory block  Choose block that comes closest to matching the needs of allocation.
Save excess for later.
allocated. 

 Merge adjacent free blocks during release operations.


 Compaction  First Fit
 Reduce external fragmentation.  Keep linked list of free blocks.
 Scan the list for the first block that comes closest to matching the needs of
 Shuffle memory contents to place all free memory allocation.
together in one large block.  Merge adjacent free blocks during release operations.

3
Example: Best Fit Example: First Fit
Request for Memory Request for Memory

35 35

8 42 8 50 12 16 16 16 48 16 16 40 24 6 18 8 42 8 50 12 16 16 16 48 16 16 40 24 6 18

8 42 8 50 12 16 16 16 48 16 16 35 5 24 6 18 8 35 7 8 50 12 16 16 16 48 16 16 40 24 6 18

Memory in use Memory not in use Memory in use Memory not in use

Other Heap Allocation Methods Example: Worst Fit


Request for Memory
 Worst Fit
 Keep linked list of free blocks. 35
 Search the whole list on each allocation.
 Choose block that worst matches the request.
 Save excess for later.
 Merge adjacent free blocks during release operations. 8 42 8 50 12 16 16 16 48 16 16 40 24 6 18
 Next Fit
 Keep linked list of free blocks.
 Start where the last search left off. 8 42 8 35 15 12 16 16 16 48 16 16 40 24 6 18
 Scan the list for the first block that comes closest to matching the
needs of allocation.
 Merge adjacent free blocks during release operations.
Memory in use Memory not in use

Example: Next Fit Bit Map


Request for Memory
 Bit Map
This is where the last search left off  Used when allocation comes in fixed-size chunks (e. g. disk blocks, or 32-
35 byte chunks).
 Keep a large array of bits, one for every chunk
 If bit is 0, chunk is in use.
 If bit is 1, chunk is free.
 No need to merge later operations.
8 42 8 50 12 16 16 16 48 16 16 40 24 6 18  Problem: Internal Fragmentation!!!
Bit Map:
1 1 0 0 1 1 0 1 0 1 0 0 0 1 0 0 1 0
8 42 8 50 12 16 16 16 35 13 16 16 40 24 6 18 Memory:

Memory in use Memory not in use

4
Pools Reclamation Methods
1 1 0 0 11 0 10 10 011 101 1 0011 00010 00100
 Pools  How do we know when dynamically-allocated
 Keep a separate allocation 32 memory can be freed?
pool for every size.  Easy when chunk only used in one place.
 Allocation fast. 1 1 0 0 1 1 0 1 0 1 1 1 0 1 0 0
 Harder when information is shared (Pointers).
 No fragmentation.
 Any problems?
64  Problems
 Inefficiency if some pools 0 1 0 0 1 1 0 0
 Dangling Pointers
run out of space while  Memory Leaks
others have a lot of free 128  Solution
space.
 Reference Count
 Solution? 0 1 0 0
 Garbage Collection
 Shuffle between pools.
256

Reference Counter Method Reference Counter: Example

 Reference Counts keep track of the number of


Memory
outstanding pointers for each chunk of memory. Process 1
 The reference counts must be managed 2 Process 5
automatically (by the system) so no mistakes are A
made in incrementing and decrementing them. Process 2
 Each time a new pointer refers to the block of memory, 1 Reference Counts
B
the reference count of the block must be increased by 1.
 Each time a pointer that refers the block of memory is Process 3
3 Process 6
changed so it no longer points to that sublist, the
C
reference count of the block must be decreased by 1.
 When reference count becomes zero, free the memory. Process 4
 Example: File descriptors in Unix.

Reference Counter: Example Reference Counter: Example

Memory Memory
Process 1 Process 1
2 Process 5 2 Process 5
A A
Process 2 Process 2
1 Reference Counts 1 Reference Counts
B B
Process 3 Process 3
3 Process 6 2
C C
Process 4 Process 4
Process 6 does not need block C Reference Count Updated!!!
anymore

5
Reference Counter Method Garbage Collection
 Advantages:  At all times, the heap may contain three kinds of
 Unused blocks of memory are returned to the available
list as soon as they are unused. dynamic data structures (or memory blocks):
 Time spent for reclamation is being undertaken  Blocks that are in use.
continually (unlike garbage collection which happens  Blocks that are not in use and are on the current list of
irregularly and takes more execution time when it does
happen). available memory space.
 Disadvantages:  Blocks that are not in use but are not on the list of
 Requires additional overheads (storage of the reference available memory space (garbage).
count for each block, reference count updating algorithm  The management (reclaiming and recycling) of
etc.
 Is still time consuming for reference count updating etc.. garbage nodes is called garbage collection.
(but more dynamic than garbage collection).

Garbage Collection Garbage Collection

 Garbage Collection
Blocks in use
 No explicit free operation by the user.
Blocks not in use and on the list of available space  When system needs storage, it searches through
all the pointers and collects things that aren’t
Blocks not in use but are not on the list of available space (garbage) used.
 Only way in circular structures.
Blocks of type 2 and 3 should be  Incredibly difficult to program and debug.
merged together!!!!  Must be able to find all pointers to objects.
 Must be able to find all objects.

Garbage Collection Mechanism Garbage Collection: Example


Initial State of Memory
 Initialization:
 Initialize all memory blocks to be free.
 Pass 1: Mark
 Go through all statically-allocated and procedure-level Memory after Marking
variables, looking for pointers.
 Mark each object pointed to, and recursively mark all
objects it points to.
Memory after Garbage Collection
 Pass 2: Sweep
 Go through all objects, free up those that aren’t marked.
 Problem: Expensive!!!
Allocated Marked Freed

6
Questions?

You might also like