02 Performance and Fragmentation

background image

University  of  Washington  

Sec3on  10:  Memory  Alloca3on  Topics  

¢

Dynamic  memory  alloca3on  

§

Size/number  of  data  structures  may  only  be  known  at  run  7me  

§

Need  to  allocate  space  on  the  heap  

§

Need  to  de-­‐allocate  (free)  unused  memory  so  it  can  be  re-­‐allocated  

¢

Implementa3on    

§

Implicit  free  lists  

§

Explicit  free  lists  –  subject  of  next  programming  assignment  

§

Segregated  free  lists  

¢

Garbage  collec3on  

¢

Common  memory-­‐related  bugs  in  C  programs  

Memory  Alloca3on  

background image

University  of  Washington  

Assump3ons  Made  in  This  Sec3on  

¢

Memory  is  word  addressed  (each  word  can  hold  a  pointer)  

§

block  size  is  a  mul7ple  of  words  

Allocated  block  

(4  words)  

Free  block  

(3  words)  

Free  word  

Allocated  word  

Memory  Alloca3on  

background image

University  of  Washington  

Alloca3on  Example  

p1 = malloc(4)

p2 = malloc(5)

p3 = malloc(6)

free(p2)

p4 = malloc(2)

Memory  Alloca3on  

What information does the allocator need to keep track of?

background image

University  of  Washington  

Constraints  

¢

Applica3ons  

§

Can  issue  arbitrary  sequence  of  malloc()  and  free()  requests  

§

free()  requests  must  be  made  only  for  a  previously  malloc()’d  block  

¢

Allocators  

§

Can’t  control  number  or  size  of  allocated  blocks  

§

Must  respond  immediately  to  malloc()  requests  

§ 

i.e.,  can’t  reorder  or  buffer  requests  

§

Must  allocate  blocks  from  free  memory  

§ 

i.e.,  blocks  can’t  overlap  

§

Must  align  blocks  so  they  sa7sfy  all  alignment  requirements  

§ 

8  byte  alignment  for  GNU  malloc  (libc  malloc)  on  Linux  boxes  

§

Can’t  move  the  allocated  blocks  once  they  are  malloc()’d  

§ 

i.e.,  compac7on  is  not  allowed.  

Why  not?  

Memory  Alloca3on  

background image

University  of  Washington  

Performance  Goal:  Throughput  

¢

Given  some  sequence  of  malloc  and  free  requests:  

§

 R

0

,  R

1

,  ...,  R

k

,  ...  ,  R

n-­‐1  

¢

Goals:  maximize  throughput  and  peak  memory  u3liza3on  

§

These  goals  are  oQen  conflic7ng  

¢

Throughput:  

§

Number  of  completed  requests  per  unit  7me  

§

Example:  

§ 

5,000    malloc()  calls  and  5,000  free()  calls  in  10  seconds    

§ 

Throughput  is  1,000  opera7ons/second  

Memory  Alloca3on  

background image

University  of  Washington  

Performance  Goal:  Peak  Memory  U3liza3on  

¢

Given  some  sequence  of  malloc  and  free  requests:  

§

 R

0

,  R

1

,  ...,  R

k

,  ...  ,  R

n-­‐1

 

¢

Def:

 Aggregate  payload  P

k

   

§

 malloc(p)  results  in  a  block  with  a  

payload

 of  p  bytes  

§

AQer  request  R

k  

has  completed,  the  

aggregate  payload  

P

k    

is  the  sum  of  

currently  allocated  payloads  

¢

Def:

 Current  heap  size  =  H

k  

§

Assume  H

k

 is  monotonically  nondecreasing  

§ 

Allocator  can  increase  size  of  heap  using  sbrk()

¢

Def:

 Peak  memory  u<liza<on  a=er  k  requests    

§

U

k

 =  (  max

i<k

 P

i  

)    /    H

k  

§

Goal:  maximize  u7liza7on  for  a  sequence  of  requests.  

§

Why  is  this  hard?  And  what  happens  to  throughput?

 

Memory  Alloca3on  

background image

University  of  Washington  

Fragmenta3on  

¢

Poor  memory  u3liza3on  is  caused  by  

fragmenta<on  

§

internal

 fragmenta7on  

§

external

 fragmenta7on  

Memory  Alloca3on  

background image

University  of  Washington  

Internal  Fragmenta3on  

¢

For  a  given  block,  

internal  fragmenta<on  

occurs  if  payload  is  smaller  than  

block  size  

 

¢

Caused  by    

§

overhead  of  maintaining  heap  data  structures  (inside  block,  outside  payload)  

§

padding  for  alignment  purposes  

§

explicit  policy  decisions  (e.g.,  to  return  a  big  block  to  sa7sfy  a  small  request)    

           

why  would  anyone  do  that?

 

¢

Depends  only  on  the  paRern  of  

previous

 requests  

§

thus,  easy  to  measure  

payload  

Internal    

fragmenta3on  

block  

Internal    

fragmenta3on  

Memory  Alloca3on  

background image

University  of  Washington  

External  Fragmenta3on  

¢

Occurs  when  there  is  enough  aggregate  heap  memory,  but  no  

single  free  block  is  large  enough  

¢

Depends  on  the  paRern  of  future  requests  

§

Thus,  difficult  to  measure  

 

p1 = malloc(4)

p2 = malloc(5)

p3 = malloc(6)

free(p2)

p4 = malloc(6)

Oops!  (what  would  happen  now?)  

Memory  Alloca3on  


Wyszukiwarka

Podobne podstrony:
02 Modeling and Design of a Micromechanical Phase Shifting Gate Optical ModulatorW42 03
(2)GT 01&02 ideas and examplesid 941 ppt
Dragonlance Dwarven Nations 02 Hammer and Axe Dan Parkinson 1 0
Dragonlance Dwarven Nations 02 Hammer and Axe # Dan Parkinson
Julia Talbot [Full Moon Dating 02] Coy and Denver [Torquere MM] (pdf)
John Gregory Betancourt New Amber Trilogy 02 Chaos And Amber
Shiloh Walker The Hunters 02 Eli and Sarel
Pacyński Tomasz Cykl Sherwood 04 Wrota światów 02 Garść popiołu (Fragment powieści niedokończonej
Performance and evaluation of small
Leadership TPC H benchmark performance and price performance using Red Hat Enterprise Linux 6
S M Stirling & Shirley Meier Fifth Millenium 02 Saber and Shadow
Harry Harrison Stars And Stripes 02 Stars And Stripes In Peril v3 0 (lit)
PN Elrod [Barrett 02] Death And The Maiden
Simon Brown Keys of Power 02 Fire and Sword
Asimov & Bear 2nd Foundation 02 Foundation and Chaos(1)
Dragonlance Tales of the Fifth Age 02 Heroes and Fools
Edgar Rice Burroughs New Tarzan 02 Tarzan and the Cave City # Barton Werper
Energy performance and efficiency of two sugar crops for the biofuel

więcej podobnych podstron