What is memory All computer systems have physical memory; used for storing: CS: Memory Management executable program code, stacks, user and kernel space data structures, etc. Michael Clarke mfc5@aber.ac.uk We need to (and do) access it in a random fashion; we call it RAM - Random Access Memory. When we write code, and run it, we use some of the memory: int a = 5; On a x86 system this will use 4 bytes! What is memory What is memory But, it is actually more complicated. We have memory mapped I/O devices. We have holes in the address space because of backwards compatibility in the x86 architecture with i86, etc.
What is memory Remember the fetch execute cycle... Fetch CPU Execute Remember the fetch execute cycle... Remember the fetch execute cycle... Fetch Fetch CPU CPU Execute Execute
Remember the fetch execute cycle... Remember the fetch execute cycle... Fetch Fetch CPU CPU Execute Error! What do we do with memory Memory Allocation We need to be able to allocate memory between processes. Processes may need more memory as they execute. What if programs need more memory than the system has What if two programs need to access the same memory For example, to write to the graphics card It s slow having to load in the same libraries such as libc for every running program! What does int i = 5; mean What does Object a = new Object(); mean There are various strategies for dynamic memory allocation: Memory pool (fixed size blocks allocation), Buddy blocks, Heap based, The slab allocator, and many more...
Memory Fragmentation Memory Fragmentation Memory Fragmentation Memory Fragmentation
Memory Fragmentation Memory Fragmentation Memory Fragmentation Virtual Memory It s fantastic... and a pain!! It lets processes use more memory than actually exists. It allows processes to be split up in memory; every process has the illusion that it is the only one using the memory! It provides methods for memory protection. We can implement Virtual Memory through Segmentation and Paging.
Virtual Memory: Segmentation Virtual Memory: Segmentation CPU Register: ds, cs,... xffffffff x xaf x x xffffffff Segment Segment Segment Segment : Offset Physical Address Space Segment 4. M A R G O R P A T A D S T A C K Global Descriptor Table Segment Kernel Read/Write, User None Kernel Write, User Read Only Kernel and User Read/Write Segment Segment Virtual Memory: Segmentation Virtual Memory: Segmentation CPU Register: ds, cs,... CPU Register: ds, cs,... Virtual Address Physical Address Space Virtual Address Physical Address Space x : x6 Segment 4 x : x6 Segment 4 Global Descriptor Table Segment Global Descriptor Table Segment 4 4 Segment Segment Segment Segment
Virtual Memory: Segmentation There are problems: We still have to load libraries in more than once. We still can t use more memory than physically exists. Page: 4KBs Programs will never have access to a full address space; some of the virtual address is used to identify the segment. We haven t really fixed the problems with memory holes. Page: 4KBs Page: 4KB H E L O W R D C U Kernel Read/Write, User None Kernel Write, User Read Only Kernel and User Read/Write x Virtual Memory: 4GB (496 MBs) xffffffff
Virtual Address x xffffffff Virtual Memory: 4GB (496 MBs) Page: 4KB H E L O W R D C U Page Directory 4... CPU Register: CR Page Directory 4... Page Table 4... Page Table 4... Physical Address Space 4K Page 4K Page 4K Page 4K Page x Virtual Memory: 4GB (496 MBs) xffffffff Virtual Address We can map any virtual address to any physical address, including memory mapped I/O addresses. We don t have to load libraries in more than once. H E L O W R D C U S M A B I G Y Programs can have a full address space.
H E L L O C S H E L O W R D C U S M A B I G Y H E L L O C S H E L O W R D C U S M A B I G Y Z ZZ H E L L O C S H E L L O C S H E L O W R D C U S M A B I G Y H E L O W R D C U S B I G Y M A Z ZZ Z ZZ
So, what is swapping H E L L O C S ZZ Z Z H E L O W R D C U S M A B I G Y Swapping is when we move a whole process to disk. Why would we ever want do this Paging and Swapping A large number of processes can run in a small amount of physical memory. Swapping is very slow, Paging is slow. Paging requires hardware support, swapping does not. It can lead to thrashing. If it happens a lot, consider buying more RAM. Some amount of paging is normal. Conclusions When we have more than one program running on a computer, we need a method of managing memory. The kernel does this, but also uses hardware features such as the MMU (Memory Management Unit) in the CPU (Central Processing Unit) Virtual Memory allows: for the isolation and protection of processes, more efficient use of memory, the ability to deal with memory holes, and processes to have full address spaces There are two types of VM: Segmentation and Paging. Paging and Swapping allows us to use more RAM than physically exists; but has drawbacks.
Further Reading Understanding the Linux Kernel. Daniel P. Bovet, Marco Cesati. O Reilly Media. ISBN: 978--596-565-8 Solaris Internals. Richard McDougall & Jim Mauro ISBN: --489- Intel 64 and IA- Architectures Software Developer s Manual. Available online at: http://www.intel.com/products/processor/manuals/ The Slab Allocator: An Object-Caching Kernel Memory Allocator. Jeff Bonwick, Sun Microsystems SunOS Virtual Memory Implementation. Joseph P. Moran, Sun Microsystems Virtual Memory Architecture in SunOS. Robert A. Gingell, Joseph P. Moran, William A. Shannon, Sun Microsystems