computer systems 2009-2010 week 14: memory management amanda oddie

37
Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

Upload: bertram-lawson

Post on 14-Dec-2015

217 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

Computer Systems

2009-2010

Week 14: Memory Management

Amanda Oddie

Page 2: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

2

Session Aims Understand how main memory

(RAM) can be shared between programs running in time slices

Page 3: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

3

Main Memory Programs can only execute efficiently if

they are present in main memory during execution

Only main memory is fast enough to feed the CPU with enough instructions and data to keep it usefully occupied.

Thus main memory is a precious resource which must be managed carefully on a multiprogramming machine

Page 4: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

4

Main Memory In addition to holding instructions

RAM is also a workspace, a transient storage repository.

Space must also be made available for: Operating System Code Operating System Data Video Storage Space etc

Page 5: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

5

Imagine loading ... Microsoft Windows Microsoft Internet Explorer An email Application Microsoft Word And listening to music using media

player whilst you do all that multi-tasking!It doesn’t stop there. You need memory for your hardware. For example you require main memory to display images on your monitor

Page 6: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

6

Not enough memory Each time a PROCESS starts it

REQUESTS an amount of RAM The amount of total RAM requested

is usually more than the amount of Physical RAM available in Multiprogramming Operating Systems such as Windows and Linux

Page 7: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

7

Hard disk, Main memory and CPU

CPU

0

?

Hard disk

Fetch and execute

Memory

Load program into main memory

Start executing the program

Page 8: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

8

Single Process System Early method Operating system

loaded first Just one user

program loaded into locations above the operating system

Inefficient use of memory – small programs leave much unused memory space

UNUSED

User Process

Operating System

Memory

0

Page 9: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

9

Fixed Memory Partition Memory divided

into fixed partitions

Several programs could run simultaneously

Space still wasted inside each partition

Process C

Process B

Process A

Operating System100k

300k

600k

Memory address

Partition 1 200k

Partition 2 300k

Partition 3 400k

1000k

Memory

0

Page 10: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

10

Sharing by swapping Program and data

copied into memory at beginning of each time slice

copied back to disc at end of time slice

All main memory available to program

But, high overhead of swapping time

Page 11: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

11

Solution Paging

Page 12: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

12

Sharing by paging Divide program and data into pages

normally resident on the hard disk Each page is typically 4kB in size Only swap in the pages needed

during time slice Memory divided into pageframes Each pageframe can hold one page

Page 13: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

13

Loading pages

Pages can be loaded in any order Use a page table for each program to keep

track of where in memory its pages are located

Page 14: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

14

Translating addresses Given an instruction like JMP 314 314 means 314 locations from beginning

of program This does not refer to a physical address

in real memory but to a virtual address measured from the program start

Assume each page 100 locations long JMP 314 means

jump to page 3 offset 14 Where is the location in real memory ?

Page 15: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

15

Virtual Memory The Operating System has a

Memory Management Unit MMU that translates virtual addresses to physical addresses in RAM.

This is known as Address Translation

Page 16: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

16

Carrying out the translation

JMP 314

Page number

Real memory address: 2514

Page table

Page 17: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

17

Page fault Interrupt generated when required

page is not in main memory It causes:

Required page to be fetched from disc into a spare pageframe

If no pageframe is spare then an existing page must be freed up by copying its current contents back to the disk

Page table updated Program continues

Page 18: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

18

Thrashing Page faults are too frequent Most time spent swapping pages Caused by:

Bad program design Too many programs attempting to run

concurrently

Page 19: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

19

Paging strategies Fetch strategy

When to fetch a page Replacement strategy

When to swap page out

Page 20: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

20

Fetch strategies Demand fetch Anticipatory fetch

Page 21: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

21

Replacement strategies Optimal page replacement Longest resident Least recently used Least frequently used

Page 22: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

22

Page File The area of the hard disk that stores

the RAM image is called a page file. It holds pages of RAM on the hard disk The Operating System moves data back

and forth between the page file and RAM. Previous versions of Windows called

this a swap file and had a .SWP extension.

Page 23: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

23

Total Size of the Page File The size of the page file is based on

how much RAM is installed in the computer.

By default, Windows XP creates a page file which is 1.5 times the amount of installed RAM

The page file is placed on the hard drive in the same folder as the Windows XP operating system

Page 24: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

24

Question What is never paged out?

The Kernel remains resident in RAM,This is never paged out.

Page 25: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

25

Invalid Page Faults? The CPU trying to access an address

out of range The CPU trying to access an address

which is already occupied by a different process

When the page that the CPU is trying to access does not exist in either RAM or VM

Page 26: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

26

Invalid Page Faults Sometimes, through program or

hardware error, the page is not there.

The system then has an ‘Invalid Page Fault’ error.

This will be a fatal error if detected in a program

The Application is normally shut down by the Operating System

Page 27: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

27

Improving Performance The page file is a reserved section of the

hard drive where data may be written and retrieved as needed.

Since the paging file and operating system files are by default located on the same drive, parallel access to both locations is impossible.

One or the other has to wait, slowing down overall system performance.

Moving the page file to another drive will improve performance.

Page 28: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

28

Question How else could you improve

performance?

Installing more RAM

Page 29: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

29

Memory Management in Windows Processes

Processes Memory Usage Virtual Memory Usage

Performance Commit Charge Physical Memory Kernel Memory

Page 30: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

30

Windows Task Manager

Process NameVirtual Memory Usage

Each Process has a Amount of Virtual Memory Limit of 4GB

Physical Memory Usage

Page 31: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

31

Commit Charge

Physical Memory

Kernel Memory

Page 32: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

32

Question What is Fragmentation?

The scattering of parts of a file throughout a disk,e.g. when the operating system breaks up the file and fits it into the spaces left vacant by previously deleted files.

Page 33: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

33

Non-Contiguous Page Files Paging files are normally created when a

drive is relatively empty finding a large contiguous block of space is not

a problem. A page file is dynamic by default

it can be expanded and contracted depending on the amount of extra virtual memory that's needed.

If the initial block of drive space that was allocated at setup becomes surrounded by additional files that have been saved to the drive ... ...a split page file can occur when the operating

system expands it past the initial size.

Page 34: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

34

Question How do you defragment files?

Windows comes with a utility called Disk Defragmenter which will reorganise the files stored on disk

Page 35: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

35

Question What creates the Page File?

a) The CPUb) The RAMc) The Operating Systemd) It’s created by magic

Page 36: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

36

Question What creates the Page File?

a) The CPUb) The RAMc) The Operating Systemd) It’s created by magic

Answer C: With help from the CPU

Page 37: Computer Systems 2009-2010 Week 14: Memory Management Amanda Oddie

37

So What do we Know? Main memory (RAM) is a precious resource Every process and device requires RAM Virtual Memory through paging gives the

impression that there is more RAM than physical RAM

The Kernel is never Paged Out Paging is Managed by the Memory

Management Unit (MMU) Page Faults are normal interrupts Invalid Page Faults are bad How to view Memory Allocation and

Resources in Windows