Dynamic Storage Allocation in OS: Stack, Heap, Fragmentation, & Garbage Collection - Prof., Study notes of Operating Systems

This document from cs 414 at the university of virginia explores dynamic storage allocation in operating systems, focusing on stack and heap allocation, fragmentation, and garbage collection. Topics include the advantages and disadvantages of stack and heap allocation, memory organization, and reclamation methods. The document also discusses the challenges of managing dynamically-allocated memory, such as dangling pointers and memory leaks, and the solutions of reference counts and garbage collection.

Typology: Study notes

Pre 2010

Uploaded on 07/29/2009

koofers-user-bog-1
koofers-user-bog-1 🇺🇸

4.5

(1)

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 414 : Operating Systems
UNIVERSITY OF VIRGINIA
Department of Computer Science
Fall 2005
Topic 11: Dynamic Storage Allocation
Two aspects of memory allocation:
Static allocation: finding a slot in memory big enough to load a.out
Dynamic allocation: while executing the program, finding space for additional memory
requests
Why isn’t static allocation sufficient for everything?
Recursive procedures: e.g., factorial
Complex data structures: e.g., symbol table
What is the probelm if all storage must be reserved in advance (statically)?
OS cannot predict when a process will come and ask for storage space: Need dynamic memory
allocation both for main memory and for file space on disk.
Two basic operations in dynamic storage management:
Allocate
Free
In general, dynamic allocation can be handled in one of two general ways:
Stack allocation (hierarchical; LIFO): restricted but simple and efficient.
Heap allocation: more general, but less efficient, more difficult to implement.
Stack organization: memory allocation and freeing are partially predictable (we do better
when we can predict the future).
11.1
pf3
pf4

Partial preview of the text

Download Dynamic Storage Allocation in OS: Stack, Heap, Fragmentation, & Garbage Collection - Prof. and more Study notes Operating Systems in PDF only on Docsity!

CS 414 : Operating Systems

UNIVERSITY OF VIRGINIA

Department of Computer Science

Fall 2005

Topic 11: Dynamic Storage Allocation

Two aspects of memory allocation: Static allocation: finding a slot in memory big enough to load a.out Dynamic allocation: while executing the program, finding space for additional memory requests Why isn’t static allocation sufficient for everything? Recursive procedures: e.g., factorial Complex data structures: e.g., symbol table What is the probelm if all storage must be reserved in advance (statically)?

OS cannot predict when a process will come and ask for storage space: Need dynamic memory

allocation both for main memory and for file space on disk.

Two basic operations in dynamic storage management: Allocate Free

In general, dynamic allocation can be handled in one of two general ways: Stack allocation (hierarchical; LIFO): restricted but simple and efficient. Heap allocation: more general, but less efficient, more difficult to implement.

Stack organization: memory allocation and freeing are partially predictable (we do better

when we can predict the future).

Allocation is hierarchical: memory is freed in opposite order from allocation. If alloc(A) then alloc(B) then alloc(C), then it must be free(C) then free(B) then free(A).

Stacks are also useful for lots of other things: tree traversal, expression evaluation, top- down recursive descent parsers, etc. A stack-based organization keeps all the free space together in one place. Why is it im- portant?

Heap organization: allocation and release are unpredictable. Heaps are used for arbitrary list

structures, complex data organizations. Example: payroll system. Don’t know when employees will join and leave the com- pany, must be able to keep track of all them using the least possible amount of storage.

Memory consists of allocated areas and free areas (or holes). Inevitably end up with lots of holes. Goal: reuse the space in holes to keep the number of holes small, their size large.

Fragmentation: inefficient use of memory due to holes that are too small to be useful. In stack allocation, all the holes are together in one big chunk.

Typically, heap allocation schemes use a free list to keep track of the storage that is not in use. Algorithms differ in how they manage the free list.

Best fit: keep linked list of free blocks, search the whole list on each allocation, choose block that comes closest to matching the needs of the allocation, save the excess for later. During release operations, merge adjacent free blocks.

First fit: just scan list for the first hole that is large enough. Merge on releases.

Is best fit better than first fit?

First fit tends to leave ‘‘average’’ size holes, while best fit tends to leave some very large ones, some very small ones. The very small ones can’t be used very easily.

How does garbage collection work? Must be able to find all pointers to objects. Must be able to find all objects.

Pass 1: mark. Go through all statically-allocated and procedure-local variables, looking for pointers. Mark each object pointed to, and recursively mark all objects it points to. The compiler has to cooperate by saving information about where the pointers are within structures. Pass 2: sweep. Go through all objects, free up those that aren’t marked.

Garbage collection is often expensive: 10% or more of CPU time used in systems that use it.