Presentation is loading. Please wait.

Presentation is loading. Please wait.

Memory Management Memory management is the art and the process of coordinating and controlling the use of memory in a computer system Why memory management?

Similar presentations


Presentation on theme: "Memory Management Memory management is the art and the process of coordinating and controlling the use of memory in a computer system Why memory management?"— Presentation transcript:

1 Memory Management Memory management is the art and the process of coordinating and controlling the use of memory in a computer system Why memory management? Multiple processing Relocation Protection Sharing Logical Organization Physical Organization Is the task carried out by the OS and hardware to accommodate multiple processes in main memory If only a few processes can be kept in main memory, then much of the time all processes will be waiting for I/O and the CPU will be idle Hence, memory needs to be allocated efficiently in order to pack as many processes into memory as possible In most schemes, the kernel occupies some fixed portion of main memory and the rest is shared by multiple processes MM Requirement: Relocation Programmer cannot know where the program will be placed in memory when it is executed A process may be (often) relocated in main memory due to swapping swapping enables the OS to have a larger pool of ready-to-execute processes Memory references in code (for both instructions and data) must be translated to actual physical memory address

2 Memory Management Requirements
Protection processes should not be able to reference memory locations in another process without permission impossible to check addresses at compile time in programs since the program could be relocated address references must be checked at run time by hardware Sharing must allow several processes to access a common portion of main memory without compromising protection cooperating processes may need to share access to the same data structure better to allow each process to access the same copy of the program rather than have their own separate copy Logical Organization users write programs in modules with different characteristics instruction modules are execute-only data modules are either read-only or read/write some modules are private others are public To effectively deal with user programs, the OS and hardware should support a basic form of module to provide the required protection and sharing Physical Organization secondary memory is the long term store for programs and data while main memory holds program and data currently in use moving information between these two levels of memory is a major concern of memory management (OS) it is highly inefficient to leave this responsibility to the application programmer

3 Three areas of memory management
Memory management hardware MMUs (Memory Management Unit), RAM, etc. Operating system memory management: Virtual memory, protection, etc. Application memory management: Allocation, garbage collection, etc. Memory management hardware consists of the electronic devices and associated circuitry that store the state of a computer. These devices include RAM, MMUs (memory management units), caches(1), disks, and processor registers. The design of memory hardware is critical to the performance of modern computer systems. In fact, memory bandwidth is perhaps the main limiting factor on system performance. Application memory management involves obtaining memory(2) from the operating system, and managing its use by an application program. Application programs have dynamically changing storage requirements. The application memory manager must cope with this while minimizing the total CPU overhead, interactive pause times, and the total memory used.

4 Memory Management Hardware
Memory management devices include RAM, MMUs, caches, disks, and processor registers Ex. MMU a hardware device responsible for handling memory(2) accesses requested by the main processor translates virtual addresses to physical addresses Memory management at the hardware level is concerned with the electronic devices that actually store data. This includes things like RAM and memory caches. Memory management hardware consists of the electronic devices and associated circuitry that store the state of a computer. These devices include RAM, MMUs (memory management units), caches(1), disks, and processor registers. The design of memory hardware is critical to the performance of modern computer systems. In fact, memory bandwidth is perhaps the main limiting factor on system performance. MMU (also known as Memory Management Unit) The MMU (Memory Management Unit) is a hardware device responsible for handling memory(2) accesses requested by the main processor. This typically involves translation of virtual addresses to physical addresses, cache(1) control, bus arbitration, memory protection, and the generation of various exceptions. Not all processors have an MMU.

5 Operating System Memory Management
Virtual Memory Operating systems simulates having more memory than is available as main memory, by storing part of the data in backing store, typically on disk. If the page referenced by the virtual address is not currently in main memory, a page fault occurs, triggering an operating system handler that swaps in the page. Some other page might be swapped out to make room Benefits Operating system memory management is concerned with using the memory management hardware to manage the resources of the storage hierarchy and allocating them to the various activities running on a computer. The most significant part of this on many systems is virtual memory(1), which creates the illusion that every process has more memory than is actually available. OS memory management is also concerned with memory protection and security, which help to maintain the integrity of the operating system against accidental damage or deliberate attack. It also protects user programs from errors in other programs.

6 Operating System Memory Management
Protection (also known as memory protection, page protection) Operating systems can protect memory pages against a combination of read, write or execute accesses by a process. A process which attempts a protected access will trigger a protection fault. Many operating systems support protection of memory(2) pages. Individual pages may be protected against a combination of read, write or execute accesses by a process. A process which attempts a protected access will trigger a protection fault. Protection is typically implemented in hardware by the MMU as part of the support for virtual memory(1) . Pages can be protected for a number of reasons: a generational or incremental garbage collector may want to place barriers(1) on pages; an operating system may want to protect pages for security, or to implement "copy-on-write" or "demand-zero-filled" pages.

7 Application Memory Management: Allocation
When the program requests a block of memory, the memory manager must allocate that blockout of the larger blocks it has received from the operating system. The part of the memory manager that does this is known as the allocator. Allocation techniques: First fit Buddy system Suballocators In the first fit algorithm, the allocator keeps a list of free blocks (known as the free list) and, on receiving a request for memory, scans along the list for the first block that is large enough to satisfy the request. If the chosen block is significantly larger than that requested, then it is usually split, and the remainder added to the list as another free block. In a buddy system, the allocator will only allocate blocks of certain sizes, and has many free lists, one for each permitted size. The permitted sizes are usually either powers of two, or form a Fibonacci sequence (see below for example), such that any block except the smallest can be divided into two smaller blocks of permitted sizes. When the allocator receives a request for memory, it rounds the requested size up to a permitted size, and returns the first block from that size's free list. If the free list for that size is empty, the allocator splits a block from a larger size and returns one of the pieces, adding the other to the appropriate free list. When blocks are recycled, there may be some attempt to merge adjacent blocks into ones of a larger permitted size (coalescence). To make this easier, the free lists may be stored in order of address. The main advantage of the buddy system is that coalescence is cheap because the "buddy" of any free block can be calculated from its address. There are many examples of application programs that include additional memory management codecalled a suballocator. A suballocator obtains large blocks of memory from the system memory manager and allocates the memory to the application in smaller pieces.

8 Application Memory Management: Recycling
When memory blocks have been allocated, but the data they contain is no longer required by the program, then the blocks can be recycled for reuse. There are two approaches to recycling memory: either the programmer must decide when memory can be reused (known as manual memory management); or the memory manager must be able to work it out (known as automatic memory management). Manual memory management is where the programmer has direct control over when memory may be recycled. Advantages: It can be easier for the programmer to understand exactly what is going on; Some manual memory managers perform better when there is a shortage of memory. Disadvantages: The programmer must write a lot of code to do repetitive bookkeeping of memory; Memory management must form a significant part of any module interface; Manual memory management typically requires more memory overhead per object; Memory management bugs are common. Automatic memory management is a service, either as a part of the language or as an extension, that automatically recycles memory that a program would not otherwise use again. Advantages: The programmer is freed to work on the actual problem; Module interfaces are cleaner; There are fewer memory management bugs; Memory management is often more efficient. Disadvantages: Memory may be retained because it is reachable, but won't be used again; Automatic memory managers (currently) have limited availability.

9 Application memory manager constraints
CPU overhead The additional time taken by the memory manager while the program is running Interactive pause times How much delay an interactive user observes Memory overhead How much space is wasted for administration, rounding (known as internal fragmentation), and poor layout (known as external fragmentation). An application memory manager must usually work to several constraints, such as: CPU overhead The additional time taken by the memory manager while the program is running; Interactive pause times How much delay an interactive user observes; Memory overhead How much space is wasted for administration, rounding (known as internal fragmentation), and poor layout (known as external fragmentation).

10 Memory management problems
Premature free or dangling pointer Memory leak External fragmentation Poor locality of reference Inflexible design Interface complexity Premature free or dangling pointer Many programs give up memory, but attempt to access it later and crash or behave randomly. This is known as premature free, and the surviving reference to the memory is known as a dangling pointer. This is usually confined to manual memory management. Memory leak Some programs continually allocate memory without ever giving it up and eventually run out of memory. External fragmentation A poor allocator can do its job of giving out and receiving blocks of memory so badly that it can no longer give out big enough blocks despite having enough spare memory. This is because the free memory can become split into many small blocks, separated by blocks still in use. This condition is known as external fragmentation. Poor locality of reference Another problem with the layout of allocated blocks comes from the way that modern hardware and operating system memory managers handle memory: successive memory accesses are faster if they are to nearby memory locations. If the memory manager places far apart the blocks a program will use together, then this will cause performance problems. This condition is known as poor locality of reference. Inflexible design Memory managers can also cause severe performance problems if they have been designed with one use in mind, but are used in a different way. These problems occur because any memory management solution tends to make assumptions about the way in which the program is going to use memory, such as typical block sizes, reference patterns, or lifetimes of objects. If these assumptions are wrong, then the memory manager may spend a lot more time doing bookkeeping work to keep up with what's happening. Interface complexity If objects are passed between modules, then the interface design must consider the management of their memory.


Download ppt "Memory Management Memory management is the art and the process of coordinating and controlling the use of memory in a computer system Why memory management?"

Similar presentations


Ads by Google