Chapter 5 - Memory management. This chapter is devoted to the fundamentals of memory management. It begins by discussing how memory protection is implemented in the hardware by using special registers in the CPU. It then discusses how efficient use of memory is achieved by reusing memory released by a process while handling subsequent memory requests, and how techniques for fast memory allocation and deallocation may cause memory fragmentation.
Trang 1in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGrawHill
Trang 2Managing the memory hierarchy
• The memory hierarchy is comprised of several memory units with different speeds and cost
– Efficient operation of a process and the system depends on
effective use of the memory hierarchy
* Efficient operation of a process depends on hit ratios in faster memories of the hierarchy, i.e., the cache and memory
* Efficient operation of the system requires many processes to be present in memory
Trang 3Managing the memory hierarchy
• How different levels in the hierarchy are managed
– L1 cache and L2 cache
* Allocation and use is managed by hardware to ensure high hit ratios
– Memory
* Use is managed by run time libraries of programming languages
* Allocation is managed by the kernel It must
Accommodate many processes in memory
Ensure high hit ratios
– Disk
* Allocation and use is managed by the kernel
Quick loading and storing of process address spaces is important
Trang 4Managing the memory hierarchy
• Efficient use of memory
– Involves sharing of memory among processes and reuse of
memory previously allocated to other processes
– Requires speedy allocation and de-allocation within the memory allocated to a process
– We discuss these aspects in Chapter 5 and this set of slides
• The memory hierarchy consisting of memory and a disk
is called virtual memory
– We discuss it separately in Chapter 6
Trang 5Memory binding
• Each entity has a set of attributes; e.g., a variable has
type, size and dimensionality
– Binding is the action of specifying values of attributes of an
entity, e.g.
* Declaration of type of a variable is the binding of its type attribute
* Memory allocation is the binding its memory address attribute
– Two types of binding are used in practice:
* Early binding (static binding ─ binding before operation begins)
Restrictive, but leads to efficient operation of a process
* Late binding (dynamic binding ─ binding during operation)
Flexible, but may lead to less efficient operation of a process
Trang 6Features of static and dynamic memory allocation
• Static memory allocation
– Allocation is performed before a process starts operating
– Size of memory required should be known a priori; otherwise,
* Wastage may occur if size is overestimated
* A process may have to be terminated if it requires more memory than allocated to it
– No allocation or de-allocation actions required during operation
• Dynamic memory allocation
– Allocation is performed during operation of a process
– Allocation equals size; hence no wastage of memory
– Allocation / de-allocation overhead is incurred during operation
Trang 7Memory allocation preliminaries
• Stack
– LIFO allocation
– A ‘contiguous’ data structure—data occupy adjoining locations
– Used for data allocated ‘automatically’ on entering a function, such as parameters of a function call and local variables of a function
• Heap
– Non-contiguous data structure—data may not occupy adjoining
locations
* Pointer-based access to allocated data
– Used for program controlled data (PCD data) that is explicitly allocated
and de-allocated in a program, e.g malloc / calloc
– ‘holes’, i.e., unused areas, may develop in memory during operation
Trang 8A hole develops in a heap due to de-allocation
(a) Three variables are allocated in the heap
(b) Deallocation of the memory of floatptr2 creates a hole in memory
Trang 9Memory allocation for a process
• Memory requirements of the components of a process
– Sizes of code and static data of the program to be executed are known a priori
* Hence code and static data can be allocated statically
– Sizes of stack and program controlled data (PCD) data vary
during operation
* Hence stack and PCD data require dynamic allocation
– The memory allocation model for a process incorporates both these requirements
Trang 10Memory allocation model for a process
• The code and static data are given a fixed allocation
• Stack and PCD data can grow in opposite directions
• Their sizes can vary independently; problem arises only if they overlap
Trang 11Memory allocation for a process
• A process is created when a program is to be executed Two issues concerning program loading and execution are:
– If the start address of the program is different from the start
address of the allocated memory area
* Addresses in instructions should be changed so that they would access the intended operands
* This requirement is called relocation of a program
– Programs should not interfere with each other’s execution
* This requirement is called memory protection
Trang 12Relocation register
• A computer provides a relocation register in the CPU to
assist in relocation of a program
– When the current instruction wishes to access memory, the CPU
* takes the memory address from the instruction, say address bbb
* adds contents of the relocation register, say aaa, to it
* accesses the byte with the resulting address bbb + aaa
Q: How to execute a program with the start address 50,000 in the
memory area starting on address 70,000?
A: Load 20,000 in the relocation register (see next slide)
Trang 13Program relocation using the relocation register
Trang 14Memory protection using bound registers
• CPU has two registers: Lower bound register (LBR), and Upper bound
register (UBR) containing start and end addresses of memory for a program
• An interrupt is raised if the program accesses an address outside the
range defined by contents of LBR and UBR
Trang 15Kernel actions for memory protection
• The kernel performs the following actions:
– While allocating memory to a process
* Write the start and end addresses of the memory area allocated to the program in the PCB of the process
– While dispatching a process
* Access the PCB and load the start and end addresses of the memory area in the LBR and UBR registers
Trang 16Reuse of memory
• The kernel keeps track of free memory areas in a heap and reuses them while making fresh memory allocations
– It maintains a free list to store information about free memory areas
– A memory request is satisfied by using a part of a free memory area
* The remaining memory area is put back into the free list
– Three techniques for reuse of free memory
* First fit technique:
Use the first free area in the list that can satisfy the request
* Best fit technique:
Use the free area that leaves smallest remaining free area
* Next fit technique:
Use the next free area in the list that satisfies the request
Trang 17Free area management in a heap
(a) Singly linked free list
(b) Doubly linked free list
∙ Permits fast insertions and deletions
∙ Consider allocation of memory area y in best fit allocation
Trang 18Heap management
(a) Free list
(b)–(d) Allocations using first, best and next fit allocation
Trang 21How to counter external fragmentation?
• Do not allow free memory areas to become too small
– Best fit leads to successively smaller memory areas!
– Perform merging by combining adjoining free areas into a single
larger memory area
* Boundary tags are used to assist in merging– Perform compaction of allocated memory so that a single free
memory area exists
Trang 22Boundary tags
• A tag is a descriptor of a memory area
• Tags are stored at both boundaries of a memory area
– Hence tags of adjoining memory areas are in adjoining memory locations
Trang 23Merging using boundary tags
(a) Singly linked free list(b)–(d) Freeing of areas X, Y or Z
Trang 25Buddy system allocator
• The allocator works as follows:
– Status of memory blocks is recorded in a status map
– When memory is to be allocated
* If an appropriate free block exists, allocate it
* Otherwise, split a larger free block into two (or more) free areas
These blocks are buddies of one another
One of the buddies is used for satisfying a memory request (either directly or through successive splitting)
The other buddy block remains free
– When a memory block is to be freed
* If its buddy block is already free, the two are merged to form a single free block The resulting free block is similarly merged
– Binary buddy: A block is split into two blocks of same size
Trang 26A binary buddy system
• Several free lists are used; each one contains blocks of a specific size
• At a request for n bytes, look for a block of size 2 p s.t. 2p <= n, 2 p+1 > n
• Allocation / deallocation is fast if no splits/merges are needed
Trang 27Binary buddy system operation
• Free blocks are numbered for convenience
• Block marked with the downarrow is freed
Before freeing a block After freeing the block
Trang 28Powers of two memory allocator
• Allocation is in terms of blocks whose sizes are different powers of 2
– Blocks are not split or merged
* Hence allocations and de-allocations are fast
– Free lists are maintained for different block sizes
– The header element of a block contains information about block
status and size It is stored in the block itself
* Header element occupies memory, hence memory efficiency is low for requests that are powers of two in size
Trang 29Approaches to memory allocation
• Contiguous memory allocation
– Allocates a single contiguous memory area to a request
* Suffers from fragmentation
* Requires provision to counter fragmentation
• Noncontiguous memory allocation
– Allocates a set of disjoint memory areas to a request
* Reduces fragmentation
* Requires address translation during execution of programs to
correctly address the allocated memory areas
Trang 30Fragmentation in contiguous memory allocation
• Both forms of fragmentation may be present
– Internal fragmentation
* When a memory area allocated to a process is not fully used
* Happens when kernel allocates only memory blocks with standard
sizes (they are called partitions)
– External fragmentation
* A free memory area is too small to be allocated
* Compaction can be performed to obtain a single free area
It involves relocation of a program
it requires provision of a relocation register
Trang 31Memory compaction in contiguous memory
allocation
(a) Initial arrangement of programs in memory
(b) Program B terminates
(c) Compaction is performed; E is initiated in the resulting free area
Trang 32Noncontiguous memory allocation
• Several disjoint areas of memory may be allocated in response to a request; reduces fragmentation
– A process is not aware of non-contiguity of memory areas
* Hence two views of a process exist
Logical view: View by the user or process
Physical view: Actual situation regarding allocation
* The organization of components in the logical view is called logical
organization; addresses used in it are logical addresses
* Addresses used in the physical view are called physical addresses
– Hardware must aid in the execution of programs
Trang 33Noncontiguous memory allocation
• It is performed as follows:
– A process is considered to consist of a set of components
* Each component is allocated a contiguous area of memory that can accommodate it
* Each operand address in an instruction is considered to be a logical address It consists of a pair (component #, byte #)
– The memory management unit (MMU) facilitates operation of the
process
* MMU is a hardware unit
* It converts a (component #, byte #) pair into an absolute memory
address
Trang 34Noncontiguous memory allocation
• Three components of a process P are loaded in different areas of memory (see next slide)
Process component size memory start address
P-1 50K 100K
P-2 30K 300K
P-3 60K 450K
Following slides show how memory is allocated to P and
how its execution is realized
Trang 35Noncontiguous memory allocation
• xyz has the address 51,448 in P. This is its logical address
• Its address in memory is 307,488. This is its physical address
* Errata: Read 312,488 as 307,488
Trang 36A schematic of address translation in noncontiguous
memory allocation
• The kernel contains a table showing memory allocated to components of P
• The MMU uses the table to translate a logical address into a physical one
Trang 37Approaches to Noncontiguous memory allocation
• Paging
– A process consists of components of a fixed size, called pages
* The page size is a power of 2 and is fixed in the hardware
– Memory is divided into parts called page frames, each page
frame has the same size as a page
– Kernel functions in paging
* Allocate memory to all pages of a process
Internal fragmentation exists in the last page of the process
No external fragmentation
* Build a page table that stores information about addresses of page
frames allocated to processes
The MMU uses information in the page table to perform address translation
Trang 38• Address translation in paging
– A logical address is viewed as a pair (page #, byte #)
* This splitting is performed using bit-splitting of the logical address since the page size is a power of 2
– The page # is used to index the page table of the process to obtain the frame # of the page frame where the page has been loaded
– The corresponding physical address is obtained as follows:
* The byte # is juxtaposed with the start address of the page frame
Trang 39Processes in paging
• Six page frames are allocated to process P and 3 to process R
• The page tables of P and R show the page frames allocated to them
• The free frames list contains 4, the page frame # of the free frame
Trang 40Approaches to noncontiguous memory allocation
• Segmentation
– Each component in a process is a logical unit called a segment,
e.g., a function, a module, or an object
* Components can have different sizes
– The kernel allocates memory to all components and builds a
segment table
* Avoids internal fragmentation, but external fragmentation exists
– Each logical address is a pair of the form (segment id, byte id)
* Segment id could be a segment name or segment number
* Byte id could be a byte name or byte number
– The MMU uses the segment table to perform address translation
Trang 41A process Q in segmentation
• A logical address is assumed to use segment and byte ids
Trang 42Comparison of contiguous and noncontiguous
memory allocation
• Contiguous memory allocation
– No allocation / de-allocation overhead during program execution – Internal fragmentation exists in partitioned allocation
– External fragmentation exists in first/best/next fit allocation
– Relocation register necessary if a swapped-out process is to be swapped-in in a different memory location
• Noncontiguous memory allocation
– Address translation is performed during program execution
– Paging: No external fragmentation; internal fragmentation exists – Segmentation: only external fragmentation exists
– Swapped-in process may be loaded in any part of memory
Trang 43Kernel memory allocation
• The kernel needs memory for its own use
– kernel creates and destroys data structures used to store
information about processes and resources at a very high rate, e.g., PCBs and ECBs
* Hence efficiency of kernel memory allocation directly influences its overhead
* The kernel uses special memory allocation techniques for efficient memory allocation to its data structures
These techniques exploit the fact that sizes of these data structures are known in advance