memory managment

15
Memory A memory is just like a human brain. It is used to store data and instructions. Computer memory is the storage space in computer where data is to be processed and instructions required for processing are stored. The memory is divided into large number of small parts called cells. Each location or cell has a unique address which varies from zero to memory size minus one. For example if computer has 64k words, then this memory unit has 64 * 1024=65536 memory locations. The address of these locations varies from 0 to 65535. Memory is primarily of three types Cache Memory Primary Memory/Main Memory Secondary Memory Cache Memory Cache memory is a very high speed semiconductor memory which can speed up CPU. It acts as a buffer between the CPU and main memory. It is used to hold those parts of data and program which are most frequently used by CPU. The parts of data and programs are transferred from disk to cache memory by operating system, from where CPU can access them. Secondary Memory This type of memory is also known as external memory or non- volatile. It is slower than main memory. These are used for storing data/Information permanently. CPU directly does not access these memories instead they are accessed via input-output routines. Contents of secondary memories are first transferred to main memory, and then CPU can access it. For example : disk, CD-ROM, DVD etc.

Upload: shahbaz-khan

Post on 15-Apr-2017

41 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Memory managment

MemoryA memory is just like a human brain. It is used to store data and instructions. Computer memory is the storage space in computer where data is to be processed and instructions required for processing are stored. The memory is divided into large number of small parts called cells. Each location or cell has a unique address which varies from zero to memory size minus one. For example if computer has 64k words, then this memory unit has 64 * 1024=65536 memory locations. The address of these locations varies from 0 to 65535.

Memory is primarily of three types

Cache Memory Primary Memory/Main Memory Secondary Memory

Cache MemoryCache memory is a very high speed semiconductor memory which can speed up CPU. It acts as a buffer between the CPU and main memory. It is used to hold those parts of data and program which are most frequently used by CPU. The parts of data and programs are transferred from disk to cache memory by operating system, from where CPU can access them.

Secondary MemoryThis type of memory is also known as external memory or non-volatile. It is slower than main memory. These are used for storing data/Information permanently. CPU directly does not access these memories instead they are accessed via input-output routines. Contents of secondary memories are first transferred to main memory, and then CPU can access it. For example : disk, CD-ROM, DVD etc.

Primary Memory (Main Memory)As human beings have a memory system to remember even a tiny thing which went

through in one’s life, similarly computer systems do contain a memory system which

can store data and can be retrieved when desired. Why not after all it’s been designed

and created by intellectual humans only.  Every computer system contains two kinds of

Page 2: Memory managment

memory out of which one is primary and the other is secondary memory. Basically a computer memory can store particularly two things. They are the data and

the set of instructions to execute a program.

Now let us take a brief look towards the types of primary storage (or) primary memory.

This primary memory can be directly accessed by the processing unit. The contents in

the primary memory are temporary. While performing a task if there is any power cut

problems then we may lose the data which is in primary memory. One benefit is that we

can store and retrieve the data with a considerable speed. Primary memory is more

expensive when compared to secondary memory.

RAM (Random Access Memory) could be the best example of primary memory. The

primary memory in the computer system is in the form of Integrated Circuits. These

circuits are nothing but RAM. Each of RAM’s locations can store one byte [1 Byte = 8

bits] of information. This can be in either of the forms 1 or 0. The primary storage

section is made up of several small storage locations in the integrated circuits called

cells. Every single cell can store fixed number of bits called word length. Each cell

contains a unique number assigned to it and it has the unique address; these addresses

are used to identify the cells. The address starts at level 0 and goes up to (N-1).

Types of primary OR Main memory:

RAM [RANDOM ACCESS MEMORY]RAM is the best example of primary storage. We have very good reason to justify because in this kind of memory we can select randomly, use that at any location of the memory, then store and finally retrieve processed data which is information. RAM is a volatile memory because it loses its contents when there is a power failure in the computer system. The memories which lose their contents on power failure are called volatile memories.

Page 3: Memory managment

Static RAM (SRAM)The word static indicates that the memory retains its contents as long as power is being supplied. However, data is lost when the power gets down due to volatile nature. SRAM chips use a matrix of 6-transistors and no capacitors. Transistors do not require power to prevent leakage, so SRAM need not have to be refreshed on a regular basis.

Because of the extra space in the matrix, SRAM uses more chips than DRAM for the same amount of storage space, thus making the manufacturing costs higher. So SRAM is used as cache memory and has very fast access.

Dynamic RAM (DRAM)DRAM, unlike SRAM, must be continually refreshed in order to maintain the data. This is done by placing the memory on a refresh circuit that rewrites the data several hundred times per second. DRAM is used for most system memory because it is cheap and small. All DRAMs are made up of memory cells which are composed of one capacitor and one transistor.

ROM [READ ONLY MEMORY]:ROM is also formed by Integrated Circuits. The data which is stored in ROM is permanent. The ROM can only read the data by CPU but can’t be edited or manipulated.ROM is a non-volatile memory because it will not lose its contents when there is a power failure in the computer system. The basic I/O

Page 4: Memory managment

program is stored in the ROM and it examines and initializes various devices attached to the computer when the power is ON. The contents in the ROM can neither be changed nor deleted.

PROM [PROGRAMMABLE READ ONLY MEMORY]:As we know that we cannot edit (or) modify data in the ROM. To overcome this problem to some extent PROM is very helpful that is in PROM we can store our programs in PROM chip. Once the programs are written it cannot be changed and remain intact even if the power is switched off. Therefore programs written in PROM cannot be erased or edited.

MROM (Masked ROM)The very first ROMs were hard-wired devices that contained a pre-programmed set of data or instructions. These kind of ROMs are known as masked ROMs which are inexpensive.

EPROM [ERASABLE PROGRAMMABLE READ ONLY MEMORY]:EPROM will overcome the problem of PROM. EPROM chip can be programmed time and again by erasing the information stored earlier in it. EPROM chip

Page 5: Memory managment

has to be exposed to sunlight for some time so that ultra violet rays fall on the chip and that erases the data on the chip and the chip can be re-programmed using a special programming facility. There is another type memory called EEPROM that stands for Electrically Erasable Programmable Read Only Memory in which we can erase the data and re-program it with a fresh content.

REGISTERS:Actually computer system uses a number of memory units called registers. Registers store data or information temporarily and pass it on as directed by the control unit.

FLASH MEMORY:It is a non-volatile computer memory that can be electrically erased and reprogrammed. Examples are memory cards, chips, pen drives, and USBflash drives etc. flash memory costs very less than byte-programmable EEPROM. It is very portable in nature.

Memory hierarchy:

In computer architecture the memory hierarchy is a concept used to discuss performance 

issues in computer architectural design, algorithm predictions, and lower 

level programming constructs involving locality of reference. The memory hierarchy 

in computer storage separates each of its levels based on response time. Since response time, 

complexity, and capacity are related,[1] the levels may also be distinguished by their 

performance and controlling technologies.

Designing for high performance requires considering the restrictions of the memory hierarchy, 

i.e. the size and capabilities of each component. Each of the various components can be viewed 

as part of a hierarchy of memories (m1,m2,...,mn) in which each member mi is typically smaller 

Page 6: Memory managment

and faster than the next highest member mi+1 of the hierarchy. To limit waiting by higher levels, 

a lower level will respond by filling a buffer and then signaling to activate the transfer.

There are four major storage levels. 

1. Internal – Processor registers and cache.

2. Main – the system RAM and controller cards.

3. On-line mass storage – Secondary storage.

4. Off-line bulk storage – Tertiary and Off-line storage.

This is a general memory hierarchy structuring. Many other structures are useful. For example, 

a paging algorithm may be considered as a level for virtual memory when designing a computer 

architecture, and one can include a level of nearline storage between online and offline storage.

Page 7: Memory managment

Memory ManagementMemory management is the functionality of an operating system which handles or manages primary memory and moves processes back and forth between main memory and disk during execution. Memory management keeps track of each and every memory location, regardless of either it is allocated to some process or it is free. It checks how much memory is to be allocated to processes. It decides which process will get memory at what time. It tracks whenever some memory gets freed or unallocated and correspondingly it updates the status.

Page 8: Memory managment

Process Address SpaceThe process address space is the set of logical addresses that a process references in its code. For example, when 32-bit addressing is in use, addresses can range from 0 to 0x7fffffff; that is, 2^31 possible numbers, for a total theoretical size of 2 gigabytes.

The operating system takes care of mapping the logical addresses to physical addresses at the time of memory allocation to the program. There are three types of addresses used in a program before and after memory is allocated 

1 Symbolic addresses

The addresses used in a source code. The variable names, constants, and instruction labels are the basic elements of the symbolic address space.

2 Relative addresses

At the time of compilation, a compiler converts symbolic addresses into relative addresses.

3 Physical addresses

The loader generates these addresses at the time when a program is loaded into main memory.

Virtual and physical addresses are the same in compile-time and load-time address-binding schemes. Virtual and physical addresses differ in execution-time address-binding scheme.

The set of all logical addresses generated by a program is referred to as a logical address space. The set of all physical addresses corresponding to these logical addresses is referred to as a physical address space.

Page 9: Memory managment

The runtime mapping from virtual to physical address is done by the memory management unit (MMU) which is a hardware device. MMU uses following mechanism to convert virtual address to physical address.

The value in the base register is added to every address generated by a user process, which is treated as offset at the time it is sent to memory. For example, if the base register value is 10000, then an attempt by the user to use address location 100 will be dynamically reallocated to location 10100.

The user program deals with virtual addresses; it never sees the real physical addresses.

Static vs Dynamic LoadingThe choice between Static or Dynamic Loading is to be made at the time of computer program being developed. If you have to load your program statically, then at the time of compilation, the complete programs will be compiled and linked without leaving any external program or module dependency. The linker combines the object program with other necessary object modules into an absolute program, which also includes logical addresses.

If you are writing a Dynamically loaded program, then your compiler will compile the program and for all the modules which you want to include dynamically, only references will be provided and rest of the work will be done at the time of execution.

At the time of loading, with static loading, the absolute program (and data) is loaded into memory in order for execution to start.

If you are using dynamic loading, dynamic routines of the library are stored on a disk in relocatable form and are loaded into memory only when they are needed by the program.

Static vs Dynamic LinkingAs explained above, when static linking is used, the linker combines all other modules needed by a program into a single executable program to avoid any runtime dependency.

Page 10: Memory managment

When dynamic linking is used, it is not required to link the actual module or library with the program, rather a reference to the dynamic module is provided at the time of compilation and linking. Dynamic Link Libraries (DLL) in Windows and Shared Objects in Unix are good examples of dynamic libraries.

SwappingSwapping is a mechanism in which a process can be swapped temporarily out of main memory (or move) to secondary storage (disk) and make that memory available to other processes. At some later time, the system swaps back the process from the secondary storage to main memory.

Though performance is usually affected by swapping process but it helps in running multiple and big processes in parallel and that's the reason Swapping is also known as a technique for memory compaction.

Page 11: Memory managment

The total time taken by swapping process includes the time it takes to move the entire process to a secondary disk and then to copy the process back to memory, as well as the time the process takes to regain main memory.

Let us assume that the user process is of size 2048KB and on a standard hard disk where swapping will take place has a data transfer rate around 1 MB per second.

Memory AllocationMain memory usually has two partitions −

Low Memory − Operating system resides in this memory. High Memory − User processes are held in high memory.

Page 12: Memory managment

Operating system uses the following memory allocation mechanism.

1 Single-partition allocation

In this type of allocation, relocation-register scheme is used to protect user processes from each other, and from changing operating-system code and data. Relocation register contains value of smallest physical address whereas limit register contains range of logical addresses. Each logical address must be less than the limit register.

2 Multiple-partition allocation

In this type of allocation, main memory is divided into a number of fixed-sized partitions where each partition should contain only one process. When a partition is free, a process is selected from the input queue and is loaded into the free partition. When the process terminates, the partition becomes available for another process.

FragmentationAs processes are loaded and removed from memory, the free memory space is broken into little pieces. It happens after sometimes that processes cannot be allocated to memory blocks considering their small size and memory blocks remains unused. This problem is known as Fragmentation.

Fragmentation is of two types

1 External fragmentation

Total memory space is enough to satisfy a request or to reside a process in it, but it is not contiguous, so it cannot be used.

2 Internal fragmentation

Page 13: Memory managment

Memory block assigned to process is bigger. Some portion of memory is left unused, as it cannot be used by another process.

The internal fragmentation can be reduced by effectively assigning the smallest partition but large enough for the process.

PagingA computer can address more memory than the amount physically installed on the system. This extra memory is actually called virtual memory and it is a section of a hard that's set up to emulate the computer's RAM. Paging technique plays an important role in implementing virtual memory.

Paging is a memory management technique in which process address space is broken into blocks of the same size called pages (size is power of 2, between 512 bytes and 8192 bytes). The size of the process is measured in the number of pages.

Page 14: Memory managment

Similarly, main memory is divided into small fixed-sized blocks of (physical) memory called frames and the size of a frame is kept the same as that of a page to have optimum utilization of the main memory and to avoid external fragmentation.