Java Programming - Unit - 4
Java Programming - Unit - 4
Java Programming - Unit - 4
Memory management
Virtual memory
• Memory 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.
Logical and Physical Address in Operating System
• Difficult Implementation:
Implementing variable Partitioning is difficult as compared to Fixed
Partitioning as it involves allocation of memory during run-time rather
than during system configure.
• External Fragmentation:
There will be external fragmentation in spite of absence of internal
fragmentation. For example, suppose in above example- process
P1(2MB) and process P3(1MB) completed their execution. Hence
two spaces are left i.e. 2MB and 1MB. Let’s suppose process P5 of
size 3MB comes. The empty space in memory cannot be allocated
as no spanning is allowed in contiguous allocation. The rule says that
process must be contiguously present in main memory to get
executed. Hence it results in External Fragmentation.
• Now P5 of size 3 MB cannot be accommodated in spite
of required available space because in contiguous no
spanning is allowed.
Compaction
• We got to know that the dynamic partitioning suffers
from external fragmentation. However, this can cause
some serious problems.
• To avoid compaction, we need to change the rule which
says that the process can't be stored in the different
places in the memory.
• We can also use compaction to minimize the probability
of external fragmentation. In compaction, all the free
partitions are made contiguous and all the loaded
partitions are brought together.
• By applying this technique, we can store the bigger
processes in the memory. The free partitions are merged
which can now be allocated according to the needs of
new processes. This technique is also called
defragmentation.
• As shown in the image above, the process P5, which
could not be loaded into the memory due to the lack of
contiguous space, can be loaded now in the memory
since the free partitions are made contiguous.
Problem with Compaction
• The efficiency of the system is decreased in the case of
compaction due to the fact that all the free spaces will
be transferred from several places to a single place.
• Huge amount of time is invested for this procedure and
the CPU will remain idle for all this time. Despite of the
fact that the compaction avoids external fragmentation, it
makes system inefficient.
• Let us consider that OS needs 6 NS to copy 1 byte from
one place to another.
• 1 B transfer needs 6 NS
• 256 MB transfer needs 256 X 2^20 X 6 X 10 ^ -9 secs
• hence, it is proved to some extent that the larger size
memory transfer needs some huge amount of time that
is in seconds.
Paging with Example
• In Operating Systems, Paging is a storage mechanism used to
retrieve processes from the secondary storage into the main memory
in the form of pages.
• The main idea behind the paging is to divide each process in the form
of pages. The main memory will also be divided in the form of frames.
• One page of the process is to be stored in one of the frames of the
memory. The pages can be stored at the different locations of the
memory but the priority is always to find the contiguous frames or
holes.
• Pages of the process are brought into the main memory only when
they are required otherwise they reside in the secondary storage.
• Different operating system defines different frame sizes. The sizes of
each frame must be equal. Considering the fact that the pages are
mapped to the frames in Paging, page size needs to be as same as
frame size.
Example
• Let us consider the main memory size 16 Kb and Frame
size is 1 KB therefore the main memory will be divided
into the collection of 16 frames of 1 KB each.
• There are 4 processes in the system that is P1, P2, P3
and P4 of 4 KB each. Each process is divided into
pages of 1 KB each so that one page can be stored in
one frame.
• Initially, all the frames are empty therefore pages of the
processes will get stored in the contiguous way.
• Frames, pages and the mapping between the two is
shown in the image below.
•
• Let us consider that, P2 and P4 are moved to waiting
state after some time. Now, 8 frames become empty and
therefore other pages can be loaded in that empty place.
The process P5 of size 8 KB (8 pages) is waiting inside
the ready queue.
• Given the fact that, we have 8 non contiguous frames
available in the memory and paging provides the
flexibility of storing the process at the different places.
Therefore, we can load the pages of process P5 in the
place of P2 and P4.
Why Segmentation is required?
• In the Operating System, an important drawback of memory
management is the separation of the user's view of memory and the
actual physical memory. Paging is the scheme which provides the
separation of these two memories.
• The user's view is mapped onto the physical storage. This mapping
permits differentiation between logical memory and physical memory.
• Operating System may divide the same function into different pages
and those pages may or may not be loaded at the same time into the
memory and it doesn't care about the User's view of the process.
This technique decreases the efficiency of the system.
• Segmentation is better than this because it divides the process into
the segments.
What is Segmentation?
• Segmentation is a memory management technique
which supports user's view of memory. This technique of
division of a computer's primary memory into sections
called segments.
• Types of Segmentation
• Virtual memory segmentation
Each processor job is divided into several segments, It is
not essential all of which are resident at any one point in
time.
• Simple segmentation
Each process is divided into many segments, and all
segments are loaded into the memory at run time, but
not necessarily contiguously.
Basic method for Segmentation
• In a computer system using segmentation, a logical address space
can be viewed as multiple segments. The size of the segment may
grow or shrink that is it is of variable length.
• During execution, each segment has a name and a length. The
address specifies both the segment name and the displacement
within the segment. The user, therefore, specifies each address by
two quantities; segment name and an offset.
• Normally it is implemented as segments are numbered and are
referred to by a segment number, in place of a segment name. Thus
a logical address consists of two tuples:
• < segment – number, offset >
• Segment number(s) – It is the total number of bits required to
represent the segment.
• Segment Offset(d) – It specifies the number of bits required to
represent the size of the segment.
Hardware support for segmentation
• In the program, the user refers to objects by a two-
dimensional address, the actual physical memory is still,
of course, a one- dimensional sequence of bytes. Thus
we have to define an implementation to map two-
dimensional user-defined addresses into one-
dimensional physical addresses.
• This mapping is affected by a segment table. In the
segment table, each entry has a segment base and a
segment limit.
• Segment Base – It contains the starting physical
address where the segment kept in memory.
• Segment Limit – It specifies the length of the segment.
Segmentation Hardware
• The logical address consists of two parts: a segment
number (s) and an offset (d) into that segment.
• The segment number used as an index into the
segment table.
• The offset d of the logical address must be between 0
and the segment limit.
• If offset is beyond the end of the segment, we trap the
Operating System.
• If offset is in the limit, then it is combined with the
segment base to produce the address in physical
memory, hence the segment table is an array of base
limit and register pairs.
Advantages of Segmentation
• There is no internal fragmentation.
• Segment Table is used to record the segments and it
consumes less space in comparison to the Page table in
paging.
Disadvantage of Segmentation
• At the time of swapping, processes are loaded and
removed from the main memory, then the free memory
space is broken into small pieces, cause of this occurs
External fragmentation.