"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
Module 4 memory management
1. Operating System
MCA II Semester
SWETA BARNWAL Page 1
Module: 4
MEMORY MANAGEMENT
Memory Management Strategies:
Memory consists of a large array of words or bytes, each with its own address. The CPU fetches
instructions from memory according to the value of the program counter. These instructions may
cause additional loading from and storing to specific memory addresses.
A typical instruction-execution cycle, for example, first fetches an instruction from memory. The
instruction is then decoded and may cause operands to be fetched from memory. After the
instruction has been executed on the operands, results may be stored back in memory. The
memory unit sees only a stream of memory addresses; it does not know how they are generated
(by the instruction counter, indexing, indirection, literal addresses, and so on) or what they are
for (instructions or data). Accordingly, we can ignore how a program generates a memory
address. We are interested only in the sequence of memory addresses generated by the running
program.
We begin our discussion by covering several issues that are pertinent to the various techniques
for managing memory. This coverage includes an overview of basic hardware issues, the binding
of symbolic memory addresses to actual physical addresses, and the distinction between logical
and physical addresses. We conclude the section with a discussion of dynamically loading and
linking code and shared libraries.
Swapping:
A process must be in memory to be executed. A process, however, can be swapped temporarily
out of memory to a backing store and then brought back into memory for continued execution.
For example, assume a multiprogramming environment with a round-robin CPU-scheduling
algorithm. When a quantum expires, the memory manager will start to swap out the process that
just finished and to swap another process into the memory space that has been freed (Figure 8.5).
In the meantime, the CPU scheduler will allocate a time slice to some other process in memory.
When each process finishes its quantum, it will be swapped with another process. Ideally, the
memory manager can swap processes fast enough that some processes will be in memory, ready
to execute, when the CPU scheduler wants to reschedule the CPU. In addition, the quantum must
be large enough to allow reasonable amounts of computing to be done between swaps.
A variant of this swapping policy is used for priority-based scheduling algorithms. If a higher-
priority process arrives and wants service, the memory manager can swap out the lower-priority
process and then load and execute the higher-priority process. When the higher-priority process
finishes, the lower-priority process can be swapped back in and continued. This variant of
swapping is sometimes called roll out roll in.
Normally, a process that is swapped out will be swapped back into the same memory space it
occupied previously. This restriction is dictated by the method of address binding. If binding is
done at assembly or load time, then the process cannot be easily moved to a different location. If
execution-time binding is being used, however, then a process can be swapped into a different
memory space, because the physical addresses are computed during execution time.
2. Operating System
MCA II Semester
SWETA BARNWAL Page 2
Memory Allocation
The main memory must accommodate both the operating system and the various user processes.
We therefore need to allocate main memory in the most efficient way possible. This section
explains one common method, contiguous memory allocation.
The memory is usually divided into two partitions: one for the resident operating system and one
for the user processes. We can place the operating system in either low memory or high memory.
The major factor affecting this decision is the location of the interrupt vector. Since the interrupt
vector is often in low memory, programmers usually place the operating system in low memory
as well. Thus, in this text, we discuss only the situation in which the operating system resides in
low memory. The development of the other situation is similar.
We usually want several user processes to reside in memory at the same time. We therefore need
to consider how to allocate available memory to the processes that are in the input queue waiting
to be brought into memory.
In contiguous memory allocation, each process is contained in a single contiguous section of
memory.
Now we are ready to turn to memory allocation. One of the simplest methods for allocating
memory is to divide memory into several fixed-sized partitions. Each partition may contain
exactly one process. Thus, the degree of multiprogramming is bound by the number of partitions.
In this multiple partitions method when a partition is free, a process is selected from the input
queue and is loaded into the free partition. When the process terminates, the partition becomes
available for another process. This method was originally used by the IBM OS/360 operating
system (called MFT); it is no longer in use. The method described next is a generalization of the
fixed-partition scheme (called MVT); it is used primarily in batch environments. Many of the
ideas presented here are also applicable to a time-sharing environment.
3. Operating System
MCA II Semester
SWETA BARNWAL Page 3
Segmentation:
An. important aspect of memory management that became unavoidable with paging is the
separation of the user's view of memory from the actual physical memory. As we have already
seen, the user's view of memory is not the same as the actual physical memory. The user's view
is mapped onto physical memory. This mapping allows differentiation between logical memory
and physical memory.
It is a memory-management scheme that supports this user view of memory. A logical address
space is a collection of segments. Each segment has a name and a length. The addresses specify
both the segment name and the offset within the segment. The user therefore specifies each
address by two quantities: a segment name and an offset. (Contrast this scheme with the paging
scheme, in which the user specifies only a single address, which is partitioned by the hardware
into a page number and an offset, all invisible to the programmer.)
Normally, the user program is compiled, and the compiler automatically constructs segments
reflecting the input program.
A C (language) compiler might create separate segments for the following:
The code
Global variables
The heap, from which memory is allocated
The stacks used by each thread
The standard C library
Virtual Memory Management
Virtual memory is a technique that allows the execution of processes that are not completely in
memory. One major advantage of this scheme is that programs can be larger than physical
memory. Further, virtual memory abstracts main memory into an extremely large, uniform array
of storage, separating logical memory as viewed by the user from physical memory. This
technique frees programmers from the concerns of memory-storage limitations. Virtual memory
4. Operating System
MCA II Semester
SWETA BARNWAL Page 4
also allows processes to share files easily and to implement shared memory. In addition, it
provides an efficient mechanism for process creation. Virtual memory is not easy to implement,
however, and may substantially decrease performance if it is used carelessly. In this chapter, we
discuss virtual memory in the form of demand paging and examine its complexity and cost.
The requirement that instructions must be in physical memory to be executed seems both
necessary and reasonable; but it is also unfortunate, since it limits the size of a program to the
size of physical memory. In fact, an examination of real programs shows us that, in many cases,
the entire program is not needed. For instance, consider the following:
Programs often have code to handle unusual error conditions. Since these errors seldom,
if ever, occur in practice, this code is almost never executed.
Arrays lists and tables are often allocated more memory than they actually need. An array
may be declared 100 by 100 elements, even though it is seldom larger than 10 by 10
elements. An assembler symbol table may have room for 3,000 symbols, although the
average program has less than 200 symbols.
Certain options and features of a program may be used rarely. For instance, the routines
on U.S. government computers that balance the budget have not been used in many years.
Even in those cases where the entire program is needed, it may not all be needed at the same
time.
The ability to execute a program that is only partially in memory would confer many benefits:
A program would no longer be constrained by the amount of physical memory that is
available. Users would be able to write programs for an extremely large virtual address
space, simplifying the programming task.
5. Operating System
MCA II Semester
SWETA BARNWAL Page 5
Because each user program could take less physical memory, more programs could be
run at the sance time, with a corresponding increase in CPU utilization and throughput
but with no increase in response time or turnaround time.
Less I/O would be needed to load or swap user programs into memory, so each user
program would run faster.
Virtual memory involves the separation of logical memory as perceived by users from physical
memory. This separation allows an extremely large virtual memory to be provided for
programmers when only a smaller physical memory is available (Figure 9.1). Virtual memory
makes the task of programming much easier, because the programmer no longer needs to worry
about the amount of physical memory available; she can concentrate instead on the problem to
be programmed.
Demand paging
Consider how an executable program might be loaded from disk into memory. One option is to
load the entire program in physical memory at program execution time. However, a problem
with this approach is that we may not initially need the entire program in memory. Suppose a
program starts with a list of available options from which the user is to select. Loading the entire
program into memory results in loading the executable code for all options, regardless of
whether an option is ultimately selected by the user or not. An alternative strategy is to load
pages only as they are needed. This technique is known as demand paging and is commonly used
in virtual memory systems. With demand-paged virtual memory, pages are only loaded when
they are demanded during program execution; pages that are never accessed are thus never
loaded into physical memory.
A demand-paging system is similar to a paging system with swapping (Figure 9.4) where
processes reside in secondary memory (usually a disk). When we want to execute a process, we
swap it into memory. Rather than swapping the entire process into memory, however, we use a
lazy swapper never swaps a page into memory unless that page will be needed. Since we are now
viewing a process as a sequence of pages, rather than as one large contiguous address space, use
of the term swapper is technically incorrect.
A swapper manipulates entire processes, whereas a pager is concerned with the individual pages
of a process. We thus use pager, rather than swapper, in connection with demand paging.
6. Operating System
MCA II Semester
SWETA BARNWAL Page 6
Page Replacement
This representation is not strictly accurate, however. If a process of ten pages actually uses only
half of them, then demand paging saves the I/0 necessary to load the five pages that are never
used. We could also increase our degree of multiprogramming by running twice as many
processes. Thus, if we had forty frames, we could run eight processes, rather than the four that
could run if each required ten frames (five of which were never used).
If we increase our degree of multiprogramming, we are memory. If we run six processes, each of
which is ten pages in size but uses only five pages, we have higher CPU utilization and
throughput, ten frames to spare. It is possible, however, that each of these processes, for a
particular data set, may suddenly try to use all ten of its pages, resulting in a need for sixty
frames when only forty are available.
Figure 9.9 Need for page replacement
Further, consider that system memory is not used only for holding program pages. Buffers for
I/O also consume a considerable amount of memory. This use can increase the strain on
memory-placement algorithms. Deciding how much memory to allocate to I/O and how much to
program pages is a significant challenge. Some systems allocate a fixed percentage of memory
for I/O buffers, whereas others allow both user processes and the I/O subsystem to compete for
all system memory.