Memory Management
Memory Management
v
alue=r
ead(
addr
ess)
:readt
hecont
ent
sofmemor
ylocat
ionataddr
ess
wr
ie(
t addr
ess,
val
ue)
:wr
iev
t al
uet
othememor
ylocat
ionataddr
ess
Thememor ymanageri
st he partofthe oper
ati
ng sy
stem t
hati
sresponsi
blef
or
al
locat
ingt
hisr
esour
cetoprocesses.
Memoryi
sast orageareaonthecomputert
hatcont
ainst
hei
nst
ruct
ionsanddat
athat
t
hecomputerusestoruntheappl
i
cati
ons.
Whent heappli
cati
onsortheoperat
ingsyst
em needmor ememorythani
savail
ableon
thecomputer,thesystem mustswapthecurrentcontentsofthememoryspacewi th
thecontent
soft hememor yspacethati
sbeingr equested.I
nthesameway,dif
ferent
sit
uat
ionsneeddiffer
entmemorymanagementt echniques.
LOGI
CALVERSUSPHYSI
CALADDRESSESI
NANOPERATI
NGSYSTEM
Al ogical
addr
essisgener
ated byCPU whi l
eapr ogr
am i srunning.Sincealogi
cal
addressdoesnotphysi
cal
l
yexisti
tisalsoknownasav irt
ualaddress.Thisaddr
essis
usedasar ef
erencebyt
heCPUt oaccesstheact
ual
physicalmemor ylocat
ion.
Thereisahardwar
edev i
cecalledMemory-ManagementUni
tisusedf
ormappi
ng
l
ogical
addr
esstoi
tscor
respondi
ngphy
sical
address.
1.The basi
c di
ffer
ence between Logicaland phy
sicaladdress i
sthatLogi
cal
addr
essisgeneratedbyCPUi nper spect
iveofaprogram whereast
hephysi
cal
addr
essisalocati
onthatexi
stsi
nt hememor yuni
t.
2.LogicalAddressSpaceist
hesetofalll
ogicaladdressesgener
atedbyCPUf ora
program wher easthesetofallphysi
caladdr essmapped t o cor
respondi
ng
l
ogicaladdressesiscal
l
edPhysi
calAddressSpace.
3.Thelogi
caladdressdoesnotexi
stphysi
cal
lyi
nt hememorywher easphy
sical
addr
essisalocati
oninthememor
ythatcanbeaccessedphy
sical
l
y.
4.Ident
icall
ogicaladdresses ar
e gener
ated by Compi
le-t
ime and Load ti
me
addressbinding met
hodswher eastheydif
fer
sf rom each ot
herinr un-
ti
me
addressbi
ndingmethod.
5.Thelogi
caladdr
essi sgener
ated byt
heCPU whi
l
etheprogr
am i
srunni
ng
wher
easthephysicaladdr
essiscomputedbyt
heMemor
yManagementUnit
(MMU).
Compar
isonChar
t:
Par
amet
er LOGI
CALADDRESS PHYSI
CALADDRESS
Basi
c gener
atedbyCPU l
ocat
ioni
namemor
yuni
t
User can v
iew the l
ogi
cal Usercan nevervi
ew phy
sical
Vi
sibi
l
ity
addr
essofaprogr
am. addr
essofpr
ogram.
Gener
ati
on gener
atedbyt
heCPU Comput
edbyMMU
Theusercanuset hel
ogi
cal
Theusercan i
ndi
rect
lyaccess
Access address t o access the
phy
sical
addr
essbutnotdi
rect
ly.
physical
address.
Mappi
ngVi
rt
ualAddr
essest
oPhy
sicalAddr
esses
Memoryconsistsoflar
gearrayaddresses.Iti
st her
esponsi
bil
i
tyoft
heCPUt ofet
ch
i
nstr
uct
ionaddressfr
om theprogram count
er.Thesei
nstr
ucti
onsmaycausel
oadi
ngor
st
ori
ngtospecif
icmemor yaddress.
Anaddr
essbi
ndi
ngcanbedonei
nthr
eedi
ff
erentway
s:
Compi
leTi
me–I
fatcompi
l
eti
mey
ouknow wher
eapr
ocesswi
l
lresi
dei
nmemor
y
thenanabsol ut
eaddresscanbegenerated-thati
saphy si
caladdr
essi sgeneratedin
theprogram executabl
edur i
ngcompil
ati
on.Loadi
ngsuchanexecutableintomemor yis
veryfast.Buti fthegenerat
edaddressspaceisoccupiedbyotherpr ocess,t
hent he
program cr ashes and i
tbecomes necessarytor ecompi
lethe program t o use a
vir
tualaddressspace.
Loadt i
me–I fiti snotknownatt hecompiletimewher eprocesswil
lresidethen
rel
ocatabl
eaddr esseswil
lbegenerated.Theloadertr
ansl
atestherel
ocat
ableaddress
toabsolut
eaddr ess.Thebaseaddressoft heprocessinmainmemor yisaddedtoall
l
ogicaladdressesbyt hel
oadert
ogener at
eabsoluteaddr
ess.I
fthebaseaddressofthe
processchangest henweneedtoreloadtheprocessagai
n.
Executi
ont i
me-Theinst
ructi
onsarealr
eadyloadedintomemor yandar
eprocessedby
theCPU.Addi ti
onalmemor ymaybeal locat
edand/ ordeal
locat
edatthi
st i
me.This
processisusedi ftheprocesscanbemov edfrom onememor ytoanotherduri
ng
executi
on (dynamicli
nki
ng-Linki
ng t
hati s done dur
ing l
oad orrun t
ime).e.g–
Compact i
on.
MemoryManagementUnit(
MMU)
Theruntimemappingbetweenv
irt
ualaddr
essandphy
sicaladdr
essi
sdonebya
har
dwaredev
iceknownasMMU.
I
nmemor ymanagement
,Oper
ati
ngSy
stem wil
lhandl
etheprocessesandmov esthe
processesbet
weendi
skandmemoryf
orexecuti
on.I
tkeepst
het rackofav
ail
ableand
usedmemor y.
SWAPPI
NGI
NOPERATI
NGSYSTEM
Thepurposeoftheswappi
nginoperati
ngsystem i
stoaccessthedatapresenti
nthe
harddi
skandbringi
ttoRAM sothattheappli
cati
onprogr
amscanusei t
.Thet hi
ngto
rememberist
hatswappi
ngisusedonlywhendataisnotpr
esentinRAM.
Alt
houghtheprocessofswappingaffect
stheper
formanceoft
hesyst
em,ithel
psto
runlar
gerandmorethanoneprocess.Thi
sisther
easonwhyswappi
ngi
salsoref
err
ed
toasmemor ycompacti
on.
Theconceptofswappi
ngi
sdi
vi
dedi
ntot
womor
econcept
s:Swap-
inandSwap-
out
.
o Swap-
outi
samet
hodofr
emov
ingapr
ocessf
rom RAM andaddi
ngi
ttot
hehar
d
di
sk.
o Swap-
ini
samet
hodofr
emov
ingapr
ogr
am f
rom ahar
ddi
skandput
ti
ngi
tback
i
ntot
hemai
nmemor
yorRAM.
Example:Supposet
heuserprocess'ssi
zeis2048KBandisast
andardhar
ddiskwhere
swappinghasadatatr
ansferrateof1Mbps.Nowwewi l
lcal
cul
atehowlongi
twil
ltake
totr
ansferf
rom mai
nmemor yt osecondar
ymemor y
.
1.Userpr
ocesssi
zei
s2048Kb
2.Dat
atr
ansf
err
atei
s1Mbps=1024kbps
3.Ti
me=pr
ocesssi
ze/t
ransf
err
ate
4. =2048/1024
5. =2seconds
6. =2000mi
l
li
seconds
7.Nowtaki
ngswap-
inandswap-
outt
ime,
thepr
ocesswil
ltake4000mi
l
li
seconds.
Adv
ant
agesofSwappi
ng
1.I
thel
pst
heCPUt
omanagemul
ti
plepr
ocesseswi
thi
nasi
ngl
emai
nmemor
y.
2.I
thel
pst
ocr
eat
eandusev
irt
ual
memor
y.
3.Swappi
ngall
owst heCPUt
operfor
m mult
ipl
et askssi
multaneousl
y.Ther
efor
e,
pr
ocessesdonothavet
owai
tver
ylongbef
oretheyareexecuted.
4.I
timpr
ovest
hemai
nmemor
yut
il
izat
ion.
Di
sadv
ant
agesofSwappi
ng
1.I
fthecomput
ersyst
em l
osespower,
theusermayl
oseal
linf
ormat
ionr
elat
edt
o
t
heprogr
am i
ncaseofsubst
ant
ial
swappi
ngacti
vi
ty.
2.I
ftheswappingalgori
thm i
snotgood,t
hecompositemethodcanincr
easet
he
numberofPageFaultanddecr
easet
heover
all
processi
ngperf
ormance.
Not
e:
o I
n a si
ngl
etaski
ng oper
ati
ng sy
stem,onl
yone pr
ocess occupi
es t
he user
pr
ogr
am ar
eaofmemor
yandst
aysi
nmemor
yunt
ilt
hepr
ocessi
scompl
ete.
o I
n a mul
ti
taski
ng oper
ati
ng sy
stem,a si
tuat
ion ar
ises when al
lthe act
ive
processescannotcoor
dinateinthemainmemor y,t
henapr ocessi
sswapout
fr
om themainmemor ysot hatot
herpr
ocessescanenteri
t.
Memor
yAl
locat
ion
Cont
iguousMemor
yAl
l
ocat
ion
Non-
Cont
iguousMemor
yAl
l
ocat
ion
InConti
guousMemor yAl
locat
ionwhenev eranyuserprocessr
equestf
orthememory
thenasinglesecti
onofthecont i
guousmemor ybl ockisall
ocat
edtothatpr
ocess
accor
dingtother
equir
ementsoftheprocess.
Cont
iguousmemor
yal
l
ocat
ioni
sachi
evedj
ustbydi
vi
dingt
hememor
yint
othef
ixed-
si
zedparti
ti
on.
I
nNon-cont
iguousmemor yall
ocat
ion,aprocessisal
l
owedtoacqui
resev
eralmemor
y
bl
ocksatdi
ffer
entl
ocat
ionsinthememor yaccordi
ngt
oit
sneed.
Memor
yPar
ti
ti
oni
ng
Conti
guousmemor yall
ocat
ioncanbeachi
evedwhenwedi
vi
det
hememor
yint
othe
fol
l
owingtypesofpar
ti
ti
ons:
Fi
xedPar
ti
ti
on
Var
iabl
ePar
ti
ti
on
Fixedpar ti
tioni
ngi sdef i
nedast hesystem ofdi vi
dingmemor yintonon- over
lapping
sizest hatarefixedandunmov able.Anothernamef ort
hisisstaticparti
tioni
ng.Inthis
case, t
hesystem getsdividedi
ntomul ti
plefi
xed-si
zedpar t
it
ions.Inthi
st ypeofscheme,
ever ypart
it
ionmayconsi stofexactl
yonepr ocess.Thisveryprocesslimit
st heextentat
whi chmul t
iprogrammi ngwouldoccur ,si
ncet hetotalnumberofpar t
it
ionsdecidest he
total numberofpr ocesses
Vari
abl
epartit
ioni
ngi st
hesy st
em ofdivi
dingmemor yint
onon-over
lappingbutvari
able
si
zes.Dynamicparti
ti
oningisanothernamef ort
his.Theschemeallocati
oninthistype
ofpart
it
ionisdonedy namicall
y.Here,t
hesizeofev er
yparti
ti
onisn’tdecl
aredini
ti
ally
.
Onl
yonceweknowt heprocesssize,wi
llweknowt hesizeoftheparti
ti
ons.Butinthis
case,thesizeoftheprocessandt
hepar
ti
ti
oni
sequal
;thus,i
thel
psi
npr
event
ing
i
nternalf
ragment
ati
on.
Par
ti
ti
onAl
locat
ionMet
hods
Thefol
l
owingar
efourcommonmemor
ymanagementt
echni
ques.Ther
ear
edi
ff
erent
Pl
acementAl
gor
it
hms:
Fi
rstFi
t
BestFi
t
Wor
stFi
t
NextFi
t
Fir
stFit
:Inthefir
stfi
t,t
hepart
it
ionisall
ocat
edwhi chi
sthef i
rstsuff
icientblockfr
om
thetopofMai nMemor y.I
tscansmemor yf
rom thebegi
nningandchoosest hefir
st
avai
labl
eblockthati
slar
geenough.Thus,i
tal
locat
esthefi
rstholethatislargeenough.
BestFit:Al
l
ocat
ethepr ocesst othepar t
it
ionwhichisthef i
rstsmal l
estsuf
fi
cient
part
it
ionamongt
hefreeavail
ablepart
it
ion.I
tsear
chestheenti
relistofholest
ofi
ndt he
small
esthol
ewhosesizeisgreat
erthanorequalt
othesizeofthepr ocess
Wor stFi
t:All
ocat
etheprocesstothepart
it
ionwhi
chi
sthelargestsuf
fi
cientamongthe
fr
eelyav ai
l
ablepart
it
ionsavail
abl
eint hemainmemory.Itisopposit
et othebest-
fi
t
al
gor i
thm.Itsear
chestheenti
reli
stofholestofi
ndt
hel ar
gestholeandal l
ocat
eitto
process.
NextFit
:Nextfiti
ssi
mil
artothef
ir
stf
itbuti
twi
l
lsear
chf
ort
hef
ir
stsuf
fi
cientpar
ti
ti
on
f
rom thelastal
l
ocati
onpoi
nt.
Pr
osofCont
iguousMemor
yAl
locat
ion
I
tsuppor
tsauser
’sr
andom accesst
ofi
l
es.
Theuserget
sexcel
l
entr
eadper
for
mance.
I
tisf
air
lysi
mpl
etoi
mpl
ement
.
ConsofCont
iguousMemor
yAl
locat
ion
Hav
ingaf
il
egr
owmi
ghtbesomewhatdi
ff
icul
t.
Thedi
skmaybecomef
ragment
ed.
Pagi
ngi
nOper
ati
ngSy
stem
Inpaging,thephysicalmemor yisdividedintofi
xed-si
zeblockscall
edpagef r
ames,
whichar ethesamesi zeast hepagesusedbyt heprocess.Theprocess’slogi
cal
addressspaceisalsodi v
idedintof
ixed-sizebl
ockscall
edpages,whichar ethesame
si
zeast hepagef rames.Whenapr ocessrequestsmemor y,theoperatingsyst
em
all
ocatesoneormor epagef r
amest ot heprocessandmapst heprocess’
slogi
cal
pagest othephy
sicalpageframes.
Themappi ngbetweenlogicalpagesandphy si
calpagef
ramesismai ntai
nedbythe
page t abl
e,which i
s used byt he memorymanagementunittot ransl
atel
ogi
cal
addressesintophysi
caladdresses.Thepaget
ablemapseachl
ogicalpagenumbert
oa
physicalpagefr
amenumber .
TheOSmayinfluencethef
ramesizes.Ev
eryf
rameneedstohavet
heexactsamesi
ze.
Thepagesi
zeshoul dbesi
milart
ot hefr
amesizesi
ncethepagesusedinpagi
ngare
mappedi
ntotheframes.
Exampl
eofPagi
ngi
nOS
Thepagesoft
heprocesseswil
lbeconti
nuousl
ystoredbecauseal
loft
heframesar
e
ori
ginal
l
yempt
y.Fr
ames,pages,
andthei
rmappingareshowninthedi
agr
am bel
ow.
Thinkaboutthescenar
iowhereP2andP4ar eeventual
l
ysentt
othewait
ingstat
e.Now
thatei
ghtframesareempty,otherpagescanbeloadedint
hei
rpl
ace.TheprocessP5,
whichis8KB( 8pages)i
nsize,i
slocatedi
nther
eadyqueue.
Wecansav etheprocessinmul
tipleplacest
hankst opagingbecausether
eare8
noncont
iguousframesthatar
eav
ailabl
ei nmemory.So,r
atherthanl
oadi
ngpagesP2
andP4,wecanl oadpageP5.
SEGMENTATI
ONI
NOPERATI
NGSYSTEM
Aprocessisdivi
dedintoSegments.Thechunksthatapr
ogram i
sdi
vi
dedint
owhichare
notnecessari
l
yal loft heexactsizesar
ecal l
edsegments.Segment
ati
ongiv
est he
user
’sview ofthepr ocesswhichpagingdoesnotpr ovi
de.Heretheuser
’svi
ew is
mappedt ophysi
calmemor y.
Ty
pesofSegment
ati
oni
nOper
ati
ngSy
stem
Vi
rt
ualMemor
y Segment
ati
on:Each pr
ocess i
s di
vi
ded i
nto a numberof
segment
s,butthesegmentat
ioni
snotdoneallatonce.Thi
ssegment
ati
onmay
ormaynottakepl
aceattherunti
meofthepr
ogram.
Si
mpl
eSegment
ati
on:Eachpr
ocessi
sdi
vi
dedi
ntoanumberofsegment
s,al
lof
whi
char
eloadedi
ntomemor
yatr
unt
ime,
thoughnotnecessar
il
ycont
iguousl
y.
Ther
eisnosimplerel
ati
onshi
pbet
weenlogi
caladdr
essesandphysi
caladdr
essesin
segment
ati
on.Atabl
estoresthei
nfor
mati
onaboutallsuchsegment
sandi scal
led
SegmentTabl
e.
Whati
sSegmentTabl
e?
Itmapsatwo-
dimensi
onal
Logi
cal
addr
essi
ntoaone-
dimensi
onal
Phy
sical
addr
ess.I
t’
s
eachtabl
eent
ryhas:
BaseAddr
ess:I
tcont
ainst
hest
art
ingphy
sicaladdr
esswher
ethesegment
s
r
esi
dei
nmemor
y.
SegmentLi
mit
:Al
soknownassegmentof
fset
.Itspeci
fi
est
hel
engt
hoft
he
segment
.
Tr
ansl
ati
onofTwo-
dimensi
onal
Logi
cal
Addr
esst
oDi
mensi
onal
Phy
sical
Addr
ess.
The address
gener
ated by the
CPUis di
vi
ded
i
nto:
Segmentnumber(
s):Numberofbi
tsr
equi
redt
orepr
esentt
hesegment
.
Segmentof
fset(
d):Numberofbi
tsr
equi
redt
orepr
esentt
hesi
zeoft
hesegment
.
Adv
ant
agesofSegment
ati
oni
nOper
ati
ngSy
stem
NoI
nter
nal
fragment
ati
on.
SegmentTabl
econsumesl
essspacei
ncompar
isont
oPaget
abl
einpagi
ng.
As a compl
ete modul
eisl
oaded al
latonce,segment
ati
on i
mpr
oves CPU
ut
il
izat
ion.
Theuser
’sper
cept
ionofphy
sical
memor
yisqui
tesi
mil
art
osegment
ati
on.User
s
can
Di
vi
deuserpr
ogr
amsi
ntomodul
esv
iasegment
ati
on.Thesemodul
esar
enot
hing
mor
ethansepar
atepr
ocesses’
codes.
The userspeci
fi
es t
he segmentsi
ze,wher
eas,i
n pagi
ng,t
he har
dwar
e
det
ermi
nest
hepagesi
ze.
Segment
ati
oni
samet
hodt
hatcanbeusedt
osegr
egat
edat
afr
om secur
it
y
oper
ati
ons.
Fl
exi
bil
it
y:Segment
ati
on pr
ovi
des a hi
gherdegr
ee off
lexi
bil
i
tyt
han pagi
ng.
Segmentscanbeofv ari
abl
esize,andpr ocessescanbedesignedt
o hav
e
mult
ipl
esegment
s,al
l
owingformorefi
ne-
grainedmemoryal
l
ocat
ion.
Shar
ing:Segment
ati
on al
l
ows f
or shar
ing of memor
y segment
s bet
ween
processes.Thi
scanbeusef
ulf
ori
nter
-pr
ocesscommuni
cat
ionorf
orshar
ing
codelibr
ari
es.
Pr
otect
ion:Segment
ati
on pr
ovi
des a l
evelofpr
otect
ion bet
ween segment
s,
prev
enti
ngoneprocessfr
om accessi
ngormodif
yinganotherpr
ocess’
smemor
y
segment.Thi
scanhelpi
ncreaset
hesecur
it
yandstabi
li
tyofthesyst
em.
Di
sadv
ant
agesofSegment
ati
oni
nOper
ati
ngSy
stem
Aspr
ocessesar
eloadedandr
emov
edf
rom t
hememor
y,t
hef
reememor
yspace
i
sbr
okeni
ntol
i
ttl
epi
eces,
causi
ngExt
ernal
fragment
ati
on.
Ov
erheadi
sassoci
atedwi
thkeepi
ngasegmentt
abl
eforeachact
ivi
ty.
Duet
otheneedf
ort
womemor
yaccesses,onef
ort
hesegmentt
abl
eandt
he
ot
herf
or
Mai
nmemor
y,accesst
imet
oret
ri
evet
hei
nst
ruct
ioni
ncr
eases.
Fr
agment
ati
on:Asment
ioned,segment
ati
oncanl
eadt
oext
ernalf
ragment
ati
on
asmemor
ybecomesdividedint
osmall
ersegment
s.Thi
scanl
eadt
owast
ed
memor
yanddecr
easedperf
ormance.
Ov
erhead:Usi
ng a segment t
abl
e can i
ncr
ease ov
erhead and r
educe
perf
ormance.Each segmentt abl
e entr
yr equi
res addit
ionalmemor
y,and
accessi
ngthetabl
et oret
ri
evememorylocat
ionscanincreasethet
imeneeded
formemoryoperat
ions.
Compl
exi
ty:Segment
ati
oncanbemor
ecompl
ext
oimpl
ementandmanaget
han
pagi
ng.In par
ti
cul
ar,managing multi
ple segments per pr
ocess can be
chal
l
engi
ng,
andthepotent
ial
forsegment
ationfaul
tscani
ncr
easeasaresult
.