Professional Documents
Culture Documents
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
2
Stack-Based Allocation: Example Stack-Based Allocation: Example
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.
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
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
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
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.
6
Questions?