![Page 1: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/1.jpg)
Malloc uses the "buddy system" to allocate only fragments that are a power of two. Free marks fragments as unused but doesn't deallocate them. Free doesn't actually call sbrk or brk at all. O(1) malloc and O(1) free.
Pages handed out via system calls sbrk and brk are managed by user level library functions malloc and free.
So farMonday, October 30, 2017 10:49 AM
Virtual Page 1
![Page 2: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/2.jpg)
Internal fragmentation: processes must allocate more memory than needed. External fragmentation: unused memory lies in small fragments in the global memory map, between processes
Fragmentation: memory is broken up into segments with gaps between them; must find a "fragment" big enough for each use.
Memory management challenges
Memory management challengesWednesday, October 27, 2004 7:59 AM
Virtual Page 2
![Page 3: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/3.jpg)
We can't compact memory by eliminating fragments, becauseWe don't know where pointers are!So if something points to a thing we move, we don't know how to change the reference!
Fragmentation is necessaryTuesday, October 26, 2010 1:32 PM
Virtual Page 3
![Page 4: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/4.jpg)
Fragmentation kinds:
Kinds of fragmentationWednesday, October 27, 2004 7:59 AM
Virtual Page 4
![Page 5: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/5.jpg)
Program size is not a multiple of page size.
So there's room "left over"; internal fragmentation.
Fixed page size.○
Many algorithms depend upon dividing up resources.
Allocation sizes are a power of two of pages. This is internal fragmentation.
"Buddy system"○
Processes exit, freeing frames.
This leaves holes in the physical frame map.
This is external fragmentation.
Reclamation: ○
Fragmentation causes:
Causes of fragmentationWednesday, October 27, 2004 7:59 AM
Virtual Page 5
![Page 6: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/6.jpg)
So far, we've covered what happens in the heap. What happens in other parts of the process?What happens to memory in the OS itself?
Stacks can free unused memory, heaps cannot!
But the same mechanism for allocating pages applies to both!
Zooming out! Tuesday, October 26, 2010 1:35 PM
Virtual Page 6
![Page 7: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/7.jpg)
Paging and frame allocation changed dramatically between IA32 and IA64 architectures.
Linux changed to match.
These notes were originally written for IA32!
I am using this book chapterhttp://www.informit.com/articles/article.aspx?p=29961to navigate the differences.
A brief asideMonday, October 30, 2017 8:41 AM
Virtual Page 7
![Page 8: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/8.jpg)
Very simple and straightforward semantics(subject to O(1) time and O(1) space constraints and $ constraints)require exceedingly complex implementations.
As we look inside the operating system,
Simple and complexThursday, October 28, 2010 11:39 AM
Virtual Page 8
![Page 9: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/9.jpg)
Preserve the illusion that each process is autonomous. Map (OS) frames to (process) pages. Only permit certain operations on each page.
malloc: the heap.subroutine calls: the stack.
Respond to needs for new pages:
Simple semantics
Simple semanticsTuesday, October 25, 2011 2:19 PM
Virtual Page 9
![Page 10: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/10.jpg)
Allow process pages to exist in memory or on disk. Allow processes to run before pages are all in memory.Deal with processes' needs dynamically. Very, very quickly….
Complex implementation
Complex implementationTuesday, October 25, 2011 2:20 PM
Virtual Page 10
![Page 11: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/11.jpg)
A frame is a unit of memory in the OS. A page is a unit of memory in the process. Memory mapping associates (OS) frames with (process) pages.
The concept of memory mapping. Recall:
The concept of memory mappingTuesday, October 25, 2011 2:12 PM
Virtual Page 11
![Page 12: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/12.jpg)
Base register: physical address of logical address 0 in the page.
○
Bounds register: contains size of map○
Basic memory mapping: base and bound registers
In a modern OS, the bound is usually a constant or a power of two.
Recovering from fragmentation is a bin packing problem. Unconstrained bin packing is NP-complete Constraining to fixed size is polynomial.
why?
Hardware to the rescueThursday, October 28, 2004 6:20 AM
Virtual Page 12
![Page 13: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/13.jpg)
Hardware translation
Hardware translation simplifiedThursday, October 28, 2004 6:20 AM
Virtual Page 13
![Page 14: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/14.jpg)
Supported by a memory management unit (MMU)That the OS configures for each processThat functions autonomously from the OS, until it generates interrupts when it needs OS attention.So, the OS has to maintain data on what gets mapped where.
Memory mapping is:
Memory mapping is:Tuesday, October 25, 2011 2:27 PM
Virtual Page 14
![Page 15: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/15.jpg)
What frame goes with what process/page? Special handling instructions for pages: read-only, shared memory, private,copy-on-write...
What the OS needs to remember:
What the OS needs to rememberTuesday, October 25, 2011 2:35 PM
Virtual Page 15
![Page 16: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/16.jpg)
Create a page table that associates frames with pages.
Some notion of process ownership of the page.The address of a frame (whenever the page is present in memory).The address of the page that the frame represents. Optionally, the size of the page (if not constant).
r: is readable w: is writeable(x: can be executed as instructions (often not enforced))
Protection bits: rwx
present in physical memorydirty: written but not flushed to disk...
Status bits: whether the page is
This table contains:
An over-simplified model of memory allocationMonday, October 30, 2017 8:30 AM
Virtual Page 16
![Page 17: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/17.jpg)
Look it up in the page table
If not, issue a segmentation fault. Make sure the page should be present
If not, issue a segmentation fault. Make sure the page protections are appropriate.
If not, then generate a "page fault" and read it into memory from disk.
Make sure the page is assigned a frame and present.
Continue execution using the page.
When the CPU needs a page in executing code,
the representation of the page table in Kernel memory.
1.
the representation of the page table in the MMU. 2.How I copy from the page table in memory to the MMU.
3.
What I didn't tell you:
A high level and oversimplified model of memory mappingMonday, October 30, 2017 8:36 AM
Virtual Page 17
![Page 18: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/18.jpg)
What's done by hardware and what's done by software? How does the OS communicate its intent to hardware? How do things change when hardware changes?
What's missing from this depiction?Monday, October 30, 2017 8:47 AM
Virtual Page 18
![Page 19: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/19.jpg)
Each segment has a different memory protection. Pages in the segment are protected in that way.
IA32 machines have a hardware notion of segment.
There is a page table with the protections and mapping for each page. Multiple page sizes are supported. The processor caches the page table in a rather simple way.
IA64 machines in 64-bit mode don't have segments anymore (this is a.k.a. "flat memory" architecture)
A massive change in hardwareMonday, October 30, 2017 8:49 AM
Virtual Page 19
![Page 20: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/20.jpg)
A segment is a set of pages in a process (a.k.a. frames in the OS) that share the same attributes. A segment descriptor describes the attributes of a segment.Some segments and their attributes: Text segment: read-only.Stack segment: read-write, builds down. Heap segment: read-write, builds up. Data segment: read-write, static size (defined globals). BSS segment: read-write, globals with default (0) values.Etc.
The concept of a segment
In IA32, the segment is a hardware concept. In IA64. a segment is only a software concept!
The concept of a segmentTuesday, October 25, 2011 2:29 PM
Virtual Page 20
![Page 21: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/21.jpg)
Frame address: where the frame starts. Byte address: which byte of the frame.
Physical address of a byte (from the point of view of the OS) has (at least) two bitfields:
Optional segment addressPage addressByte address
Logical address of a byte (in a process) contains (at least) two or three bitfields:
A memory map is a correspondence (one-one function) between physical and logical addresses.
In IA32, the segment address is used. In IA64, the segment address is not used.
Memory addressingTuesday, October 25, 2011 2:42 PM
Virtual Page 21
![Page 22: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/22.jpg)
A page is resident if it is in memory and mapped. A page is virtual if not.
At any time, a process's pages are not all in memory
Organized by logical address. Left on disk even if page is resident.
Virtual pages are kept on disk in a swap partition
Moving a page from virtual to resident is called swapping in. Moving a page from resident to virtual is called swapping out.
New language:
Virtual memory
Virtual memoryThursday, October 28, 2010 12:44 PM
Virtual Page 22
![Page 23: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/23.jpg)
When a process accesses memory that isn't currently mapped to it, this is called a page fault.
The processor/MMU generates an interrupt .The OS handles the interrupt (not the process!) One of several things is done, depending upon the state of the process.
How this is implemented:
Handling memory "faults"
Handling "faults"Tuesday, October 25, 2011 2:13 PM
Virtual Page 23
![Page 24: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/24.jpg)
The process runs with only part of its pages resident.
block the processswap in the required pageunblock the process after the page is resident(it returns to the run queue)
If a page is needed and isn't resident
The semantics of virtual memory
Any time there is a page fault, the process loses its place in the run queue.
The semantics of virtual memoryThursday, October 28, 2010 11:58 AM
Virtual Page 24
![Page 25: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/25.jpg)
read desired contents from diskmap to requesting process page
If there is an unused frame,
pick a used frame to unmapunmap it from whatever process is using it nowmove the used page's contents to disk if neededread desired contents from disk. map the frame to the requesting processrelease the process to execute
else
Finding an available frame
How do I choose what to flush?
To find an available pageThursday, October 28, 2010 12:02 PM
Virtual Page 25
![Page 26: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/26.jpg)
If the memory image of a page matches the disk image, we say that the memory copy of a page is "clean". If the memory image is different, we say the memory copy of the page is "dirty". Dirty pages must be flushed to disk before they can be reused by other processes. Dirtiness is a page attribute.
An important concept in swapping: "dirtiness"
The dirty bitTuesday, October 30, 2012 11:27 AM
Virtual Page 26
![Page 27: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/27.jpg)
The dirty bit consumes space, but saves time. 1 bit per page saves unneeded flushes, perhaps of very large sets of pages.
A classic space-time tradeoff
A classic space-time tradeoffTuesday, October 30, 2012 11:29 AM
Virtual Page 27
![Page 28: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/28.jpg)
Pages in hardware cache are dirty if changed, and clean if not. Exact same semantics apply: dirty pages must be flushed before reuse.
The dirty bit doesn't just occur in software
Dirtiness in hardwareTuesday, October 30, 2012 11:30 AM
Virtual Page 28
![Page 29: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/29.jpg)
Picture of swappingThursday, October 28, 2010 7:58 PM
Virtual Page 29
![Page 30: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/30.jpg)
Another picture of swappingThursday, October 28, 2010 1:00 PM
Virtual Page 30
![Page 31: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/31.jpg)
States of a page Thursday, October 28, 2010 12:57 PM
Virtual Page 31
![Page 32: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/32.jpg)
We need to choose a page to swap out.
Least recently used (LRU)Least frequently accessed (LFA)
Two prevalent algorithms
Selecting what to swap outThursday, October 28, 2010 1:05 PM
Virtual Page 32
![Page 33: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/33.jpg)
There is no concept of "best" page reuse algorithm.
All choices are based upon "average" process behavior.
All choices are based upon the principle of locality: if a process has accessed a
page recently, it is more likely to access that page than to access another.
How do we choose?
How do we choose? Tuesday, October 30, 2012 11:31 AM
Virtual Page 33
![Page 34: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/34.jpg)
Accessing an array: many accesses to one page.
Accessing instructions: always linear. Access elements of a linked list: buddy system keeps them local to 1-2 pages.
The principle of locality in action
The principle of locality in actionTuesday, October 30, 2012 11:33 AM
Virtual Page 34
![Page 35: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/35.jpg)
Choose the frame (page) that was least recently accessed. Swap out its page and reuse it. Keep track of time of last access in a page descriptor(!).
Least recently used
Least recently used (LRU)Thursday, October 28, 2010 1:07 PM
Virtual Page 35
![Page 36: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/36.jpg)
The frame that is least frequently accessed (for a given time) gets unmapped, and is reused. Keep track of accesses in a page descriptor(!)
Least frequently accessed
Least frequently accessed (LFA)Thursday, October 28, 2010 1:09 PM
Virtual Page 36
![Page 37: Memory management challenges Fragmentation: Internal](https://reader031.vdocuments.net/reader031/viewer/2022020705/61fb922f2e268c58cd5fc102/html5/thumbnails/37.jpg)
LRU is better for linear access. LFA is better for loops.
Which one is best?Monday, October 30, 2017 5:25 PM
Virtual Page 37