lecture_slides_10_102-memallocation-examples
lecture_slides_10_102-memallocation-examples
lecture_slides_10_102-memallocation-examples
of Washington
Memory
Alloca3on
University
of
Washington
Memory
Alloca3on
University
of
Washington
Alloca3on
Example
p1 = malloc(4)
p2 = malloc(5)
p3 = malloc(6)
free(p2)
p4 = malloc(2)
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
University
of
Washington
¢ 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
University
of
Washington
Memory
Alloca3on
University
of
Washington
Fragmenta3on
¢ Poor
memory
u3liza3on
is
caused
by
fragmenta<on
§ internal
fragmenta7on
§ external
fragmenta7on
Memory
Alloca3on
University
of
Washington
Internal
Fragmenta3on
¢ For
a
given
block,
internal
fragmenta<on
occurs
if
payload
is
smaller
than
block
size
block
Internal
Internal
payload
fragmenta3on
fragmenta3on
¢ 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?
Memory
Alloca3on
University
of
Washington
External
Fragmenta3on
¢ Occurs
when
there
is
enough
aggregate
heap
memory,
but
no
single
free
block
is
large
enough
p1 = malloc(4)
p2 = malloc(5)
p3 = malloc(6)
free(p2)