Lecture 6: Garbage Collection
Lecture 6 discusses garbage collection. It reviews a number of
well-known garbage collection algorithms, including the mark-sweep,
mark-compact, copying, and generational algorithms. It discusses
their relative performance and the trade-offs of using garbage
collection compared to manual memory management and region-based
memory management. Various practical factors that affect garbage
collection behaviour are discussed.
Part 1: Garbage Collection
The first part of the lecture introduces the idea of garbage
collection, and discusses three basic garbage collection algorithms:
the mark-sweep, mark-compact, and copying algorithms. The mark-sweep
algorithm is simple to implement, but inefficient. It stops the program
while running, has high and unpredictable collection duration, and has
poor locality of reference and results in memory fragmentation. The
mark-compact collector improves on this, improving application times
and reducing fragmentation, but is more complex, slow, and still has
poor locality of reference. And the copying collector, in turn,
improves performance and reduces fragmentation, but at the cost of
higher memory overhead.
00:00:00.000
In this lecture I’d like to talk
00:00:02.065
about garbage collection.
00:00:04.050
So why garbage collection, given that this
00:00:07.132
is a systems programming course and,
00:00:09.774
as we discussed in the previous lecture,
00:00:12.857
most systems programming languages don't actually use
00:00:15.939
garbage collection.
00:00:16.920
Well, I guess, there are two reasons.
00:00:19.798
The first is that garbage collection is
00:00:22.677
very widely implemented and very widely used
00:00:25.555
in programming in general.
00:00:27.300
And, if we're going to make a
00:00:29.882
decision not to use garbage collection in
00:00:32.464
systems languages, we should understand the trade-offs,
00:00:35.046
and understand how it behaves, just to
00:00:37.627
make sure that we are being correct
00:00:40.209
in our assumption that it's not predictable
00:00:42.791
enough.
00:00:43.260
The second is that the region based
00:00:45.653
memory management schemes, like we discussed in
00:00:48.046
the last lecture, and like are used
00:00:50.439
in Rust, are still pretty new.
00:00:52.590
And they trade off program complexity,
00:00:55.221
with all the different pointer types,
00:00:57.851
in order to get predictable resource management.
00:01:01.020
And maybe that's the right trade off,
00:01:03.128
maybe it's not, but, again, we need
00:01:05.236
to understand what garbage collectors can do,
00:01:07.344
and how they actually behave, to know
00:01:09.452
if we are making the right trade
00:01:11.560
off between performance and complexity and predictability
00:01:13.668
and so.
00:01:16.710
What I want to talk about this
00:01:19.249
lecture is garbage collection algorithms. In this
00:01:21.789
part I’ll talk through
00:01:23.340
some of the basic algorithms, the mark
00:01:26.341
sweep, mark compact, and copying collectors,
00:01:28.914
and then in the later parts I'll
00:01:31.915
talk about generational garbage collection, incremental algorithms,
00:01:34.916
real-time algorithms, and some of the practical
00:01:37.917
factors that affect garbage collection performance.
00:01:40.590
The paper you see linked on the
00:01:43.337
slide, the “Uniprocessor Garbage Collection Techniques” paper
00:01:46.084
is a survey of some of these
00:01:48.831
techniques. It's getting a little old now,
00:01:51.578
I think it's from 1992,
00:01:53.640
but it's actually a really nice introduction
00:01:56.070
and survey of these basic techniques,
00:01:58.153
and is very much worth reading to
00:02:00.583
get more detail on how the principles
00:02:03.013
work.
00:02:07.950
Okay, so let's start with basic garbage
00:02:10.942
collection techniques: mark sweep algorithms, mark compact
00:02:13.935
algorithms, and the copying garbage collectors.
00:02:19.800
So the principle of garbage collection is
00:02:23.347
to avoid some of the problems with
00:02:26.893
reference counting, and avoid the complexity of
00:02:30.440
compile time ownership tracking in region based
00:02:33.987
memory management,
00:02:35.100
by building a system which can explicitly
00:02:39.285
trace through memory and collect unused objects;
00:02:43.471
and explicitly collect the garbage.
00:02:46.560
The way garbage collection works, in general,
00:02:49.697
is that the collector traces through the
00:02:52.833
memory, traces through all of the objects
00:02:56.070
which have been allocated, that have been
00:02:58.529
used, that are allocated on the heap,
00:03:00.988
and it tries to find which of
00:03:03.447
those objects are still in use.
00:03:05.555
And if some of those objects which
00:03:08.014
are on the heap are not somehow
00:03:10.473
referenced, it disposes of them.
00:03:12.330
It automatically frees the memory.
00:03:16.320
And essentially this moves the garbage collection,
00:03:19.750
so instead of being integrated into the
00:03:23.180
object’s lifecycle, in the way a region
00:03:26.610
based scheme
00:03:27.690
integrates managing when the object lives into
00:03:30.800
knowing when it goes out of scope,
00:03:33.909
it moves it into a separate phase
00:03:37.019
of execution, a separate garbage collection system,
00:03:40.129
that runs alongside the program.
00:03:42.450
So the operation of the program –
00:03:45.314
what garbage collection researcher call “the mutator”
00:03:48.178
– and the garbage collector is sort
00:03:51.042
of interleaved.
00:03:51.960
The program runs for a while,
00:03:54.399
and then it pauses. The garbage collector
00:03:57.244
runs, collects some garbage, reclaims some memory.
00:04:00.089
Then the program restarts. And they bounce
00:04:02.935
around between the two phases of execution.
00:04:05.880
There's a bunch of different ways the
00:04:08.514
garbage collector can work. The basic algorithms
00:04:11.148
I'll talk about today, are the mark
00:04:13.782
sweep, mark compact, and copying collectors.
00:04:16.140
And then, in the next part,
00:04:17.767
I’lll move on to talk about generational
00:04:19.665
garbage collection, and some of the more
00:04:21.563
incremental algorithms in the later parts.
00:04:25.740
So let's start with mark sweep garbage
00:04:29.666
collection algorithms; mark-sweep collectors.
00:04:32.010
The mark sweep approaches is the simplest
00:04:35.058
of the automatic garbage collection schemes.
00:04:37.770
It’s a two phase algorithm. In the
00:04:42.616
first phase
00:04:44.100
it's runs through the heap, and tries
00:04:46.596
to find that the live objects and
00:04:49.092
separate them from the dead objects.
00:04:51.231
Essentially it's marking the objects which are
00:04:53.727
still alive.
00:04:54.540
And then, in the second phase,
00:04:56.337
it goes through, and reclaims the garbage.
00:04:58.434
It sweeps away the objects which have
00:05:00.531
not been marked.
00:05:01.530
It’s a non-incremental algorithm, in that it
00:05:03.970
pauses the program when the garbage collection,
00:05:06.410
while the garbage collector runs. So,
00:05:08.501
when the system detects that it’s running
00:05:10.941
short of memory, the program gets paused,
00:05:13.381
and
00:05:13.830
the garbage collector starts running. It runs
00:05:16.816
through the heap, marks the live objects,
00:05:19.802
runs through the heap again, to sweep
00:05:22.787
up, to reclaim, the garbage.
00:05:25.020
And, only then, restarts the execution of
00:05:26.793
the program.
00:05:31.020
The first phase is the marking phase.
00:05:33.327
The goal of the marking phase is
00:05:35.633
to distinguish the objects which are alive.
00:05:38.040
The goal is to find the set
00:05:40.021
of objects which are actually reachable,
00:05:41.719
actually still in use by the program.
00:05:43.800
To do this, it starts by finding
00:05:46.180
what's called the root set of objects.
00:05:48.660
The root set is the set of
00:05:54.018
global variables, anything allocated,
00:05:57.180
globally in the program, and it's the
00:05:59.866
set of variables which are allocated on
00:06:02.552
the stack.
00:06:03.420
And, when you look at the set
00:06:05.393
of variables allocated on the stack,
00:06:07.084
you don't just look at the current
00:06:09.057
stack frame, for the currently executing function,
00:06:11.130
you look at all of the parents
00:06:13.353
stack frames for this, all the way
00:06:15.575
up to the stack frame for main().
00:06:17.798
So it's all of the local variables
00:06:20.020
executed in the call stack, up to
00:06:22.243
the current point of execution, plus any
00:06:24.465
global variables.
00:06:25.200
And this comprises the root set.
00:06:28.350
The garbage collector then starts with this
00:06:30.967
root set, and follows pointers. Any object
00:06:33.584
in that root set, which has a
00:06:36.201
pointer to another object, it follows that
00:06:38.819
pointer to that object, and then,
00:06:41.062
recursively, from there on follows the pointers
00:06:43.679
out to find all of the other
00:06:46.296
objects.
00:06:46.770
And maybe that's a breadth-first search,
00:06:48.639
maybe it's a depth-first search, it doesn’t
00:06:50.819
particularly matter what algorithm you use to
00:06:52.999
follow the pointers. The key thing is
00:06:55.179
that you start from the root set,
00:06:57.359
and you follow the pointers to find
00:06:59.539
all of the other objects in the
00:07:01.719
system.
00:07:02.130
And, as you follow the pointers,
00:07:04.537
you mark the objects. You set a
00:07:07.345
bit in the object header, or set
00:07:10.154
a bit in some table somewhere,
00:07:12.561
to recognise that you've reached a particular
00:07:15.369
object.
00:07:15.870
And, if you find an object which
00:07:18.149
you've already reached, you can stop,
00:07:20.103
circle back, and search some of the
00:07:22.382
other pointers. And eventually you’ll run out
00:07:24.661
of pointers to follow. Eventually you’ve traversed
00:07:26.940
the whole graph, and found all of
00:07:29.220
the objects that are reachable from the
00:07:31.499
root set.
00:07:32.250
If you have a cycle of objects,
00:07:34.978
that just means you’ll come back to
00:07:37.706
yourself, and you'll stop once you've gone
00:07:40.434
around the loop once, and backtrack,
00:07:42.772
and look at the rest of the
00:07:45.500
objects.
00:07:45.990
If you have a cycle of objects
00:07:47.730
which reference each other, but are not
00:07:49.470
reachable from the root set, then you'll
00:07:51.210
never you'll never be able to reach
00:07:52.950
those, and so they’ll never be marked.
00:07:58.740
That's the marking phase. The second phase
00:08:01.275
is what's called the sweep phase,
00:08:03.448
where you find the objects which are
00:08:05.983
no longer alive.
00:08:07.170
And the way this works is that
00:08:09.162
it passes linearly through the entire heap,
00:08:11.154
and it looks every object in the
00:08:13.145
heap.
00:08:13.530
If the object has been marked in
00:08:16.156
the marking phase as being alive,
00:08:18.407
then it keeps it. Otherwise is it
00:08:21.034
frees the memory to reclaim the space.
00:08:23.760
When an object is reclaimed, it marks
00:08:26.145
its memory as being available for reuse.
00:08:28.529
And the system maintains a free list,
00:08:30.914
it maintains a list of unused blocks
00:08:33.299
of memory.
00:08:34.080
And when it allocates objects, it puts
00:08:36.937
them into some of the space that
00:08:39.793
was in the free list, and removes
00:08:42.650
that space in the list.
00:08:44.790
And the sweep phase just go through
00:08:46.913
the entire heap. It starts the beginning,
00:08:49.037
works its way through to the end,
00:08:51.160
and any object which was marked it
00:08:53.283
keeps, and any object which was not
00:08:55.407
marked is added onto the free list.
00:08:57.630
When it comes to allocating new objects
00:08:59.527
in the future, as I say,
00:09:01.153
it takes them off the free list.
00:09:03.150
Objects don't move around, so if an
00:09:05.187
object is reclaimed there's a gap in
00:09:07.224
memory. And there may be objects on
00:09:09.260
either side of it, so the memory
00:09:11.297
is potentially fragmented.
00:09:12.270
But I think this is no worse
00:09:14.300
than using malloc() or free(), which also
00:09:16.330
don't move objects around. If you allocate
00:09:18.360
lots of small objects, and release them
00:09:20.390
in an unpredictable order, you end up
00:09:22.420
with memory which is quite fragmented,
00:09:24.160
with lots of little holes in it.
00:09:29.580
And this works. It’s very simple,
00:09:33.300
but it's quite inefficient.
00:09:35.880
Mark sweep algorithms are very slow,
00:09:38.636
and the amount of time they take
00:09:41.851
is unpredictable.
00:09:42.870
The program gets stopped while the collector
00:09:45.132
runs, so it has to wait for
00:09:47.394
the garbage collector to execute.
00:09:49.110
How long it takes the garbage collector
00:09:51.371
to run will depend on how many
00:09:53.633
objects are alive, because it has to
00:09:55.894
search though
00:09:56.640
from the root set, and follow all
00:09:59.102
of the pointers, so the more memory
00:10:01.565
the program has allocated, the longer it
00:10:04.027
will take it to follow all the
00:10:06.489
pointers, and mark the live objects.
00:10:08.700
Similar, how long the garbage collector takes
00:10:11.539
to run will depend on the size
00:10:14.378
of the heap, because it has to
00:10:17.217
sweep through the entire heap and check
00:10:20.056
to see if the objects can be
00:10:22.894
reclaimed.
00:10:23.400
And, I guess, this depends on the
00:10:25.925
maximum amount of memory that the program
00:10:28.449
has ever allocated, because it knows what's
00:10:30.974
the maximum region of the heap that’s
00:10:33.499
been touched.
00:10:34.320
But, if a program has a lot
00:10:36.706
of memory allocated, or if a program
00:10:39.091
has previously allocated a lot of memory,
00:10:41.477
so we know it's touched a lot
00:10:43.862
of the heap, the mark sweep garbage
00:10:46.248
collection gets slower.
00:10:47.370
And this is in contrast to reference
00:10:51.319
counting and region based systems
00:10:54.240
which just depends on the particular set
00:10:57.165
of objects which that they're looking at.
00:11:00.090
This depends on the total size of
00:11:03.015
the memory allocated and the total size
00:11:05.941
that has been previously allocated.
00:11:08.130
And mark-sweep collectors have no locality of
00:11:14.447
reference.
00:11:15.450
If you're using a reference counting scheme,
00:11:19.456
for example,
00:11:20.700
or region-based scheme, when you manipulate a
00:11:24.812
pointer, you change the reference count,
00:11:28.337
you maybe allocate or free that object,
00:11:32.550
it’s only that object you're currently accessing
00:11:35.549
where the reference count gets updated.
00:11:38.220
A mark-sweep collector goes through the entire
00:11:40.958
heap. It accesses every object in the
00:11:43.696
system when it runs.
00:11:45.360
And this can disrupt the cache,
00:11:47.679
it can disrupt the virtual memory subsystem,
00:11:50.385
by bringing all of the objects into
00:11:53.091
the cache, so it evicts the previous
00:11:55.797
working set.
00:11:56.670
And, if you have a virtual memory
00:11:58.458
system, and some of the memory is
00:12:00.247
paged out to disk, then it has
00:12:02.035
to access those pages, bring them in
00:12:03.823
from disk, in order to scan through
00:12:05.612
them in the mark and sweep phase.
00:12:07.500
So this disrupts the cache, and it
00:12:09.527
brings things in off of the virtual
00:12:11.554
memory, so it can be quite slow
00:12:13.581
as a result.
00:12:14.550
And also, you potentially have problems with
00:12:18.114
fragmentation of the heap.
00:12:20.250
Objects don't get moved around, so when
00:12:24.644
things get freed there's a gap which
00:12:29.037
can be reused.
00:12:31.020
But this could mean that the memory,
00:12:33.230
the free memory, exists as a bunch
00:12:35.440
of small fragments, a bunch of small
00:12:37.650
pieces, rather than a large contiguous region.
00:12:39.960
And this can make it difficult to
00:12:41.904
allocate large objects, even if you have
00:12:43.849
enough memory, there may not be a
00:12:45.793
large enough contiguous block of memory.
00:12:51.600
So that's mark sweep algorithms.
00:12:54.600
The first extension to the mark sweep
00:12:57.693
algorithm is what's known as a mark
00:13:00.786
compact collector.
00:13:01.770
The goal of the mark compact collectors
00:13:05.038
is to solve the fragmentation problems,
00:13:07.839
and to speed up memory allocation.
00:13:10.740
And a mark compact collector works in
00:13:13.766
three phases.
00:13:14.730
The first phase is a marking phase,
00:13:17.353
just like in the mark sweep collectors.
00:13:19.977
It finds the root set of objects,
00:13:22.600
and then it scans through the memory,
00:13:25.224
following the pointers from the root set,
00:13:27.847
to find the set of objects which
00:13:30.470
are alive.
00:13:31.320
And that it does conceptually another pass
00:13:34.143
through the memory, with the goal of
00:13:36.967
reaching,
00:13:37.470
the goal of reclaiming, any unused objects.
00:13:40.395
So it's just like the sweep phase
00:13:43.320
in a mark sweep collector. It runs
00:13:46.244
through the whole heap, and any objects
00:13:49.169
which are alive, which have been marked
00:13:52.094
in the traversal phase, are kept,
00:13:54.601
and anything else is deallocated.
00:13:56.790
And then, conceptually, it makes a third
00:13:59.469
pass through the heap, and it compacts
00:14:02.147
the live objects. So if there are
00:14:04.826
gaps between the objects, where something has
00:14:07.504
been reclaimed, it moves those objects
00:14:09.900
so that the allocated memory is in
00:14:12.901
a contiguous space, and all the free
00:14:15.902
memory is in another contiguous block at
00:14:18.903
the end.
00:14:19.860
And, if you're clever in how you
00:14:22.123
implement this, the reclaiming and the compacting
00:14:24.387
can be done in one pass,
00:14:26.327
but it still goes through the entire
00:14:28.590
address space, and it still touches all
00:14:30.853
of the memory, and potentially move some
00:14:33.117
of the objects around.
00:14:37.470
These mark compact collectors have two big
00:14:42.633
advantages.
00:14:43.470
The first is that they solve the
00:14:46.156
fragmentation problem. By moving the objects around,
00:14:48.841
they make sure that all of the
00:14:51.527
free memory is in one contiguous block
00:14:54.213
after the
00:14:55.080
collector has run. And therefore you don't
00:14:57.246
need to worry about the fact that
00:14:59.412
you only have a small numbers of
00:15:01.577
free bytes here and there, and no
00:15:03.743
large blocks. So all the free spaces
00:15:05.909
is left in one contiguous block,
00:15:07.765
and you can allocate as much as
00:15:09.931
you'd need.
00:15:10.650
They also make memory allocation very fast,
00:15:13.625
because the memory
00:15:15.000
the free memory, is in a contiguous
00:15:17.345
block, you don't have to search through
00:15:19.690
some sort of complicated free list structure
00:15:22.035
to find the appropriate sized gap for
00:15:24.380
the memory you need to allocate.
00:15:26.490
Memory allocation is just a case of
00:15:29.025
taking the first address in the free
00:15:31.561
region, bumping a pointer to where next
00:15:34.096
free address will be, and returning the
00:15:36.632
previous block. It's just an addition and
00:15:39.167
a return of a pointer, so it's
00:15:41.702
always very, very fast to allocate new
00:15:44.238
memory.
00:15:44.700
The disadvantages, though,
00:15:47.100
like the mark sweep collectors, the locality
00:15:50.445
of reference is bad. It has to
00:15:53.791
pass through the entire heap,
00:15:56.280
pull things in from virtual memory,
00:15:58.844
and it has to do this at
00:16:01.835
least twice.
00:16:02.790
it's also slow, because it has to
00:16:05.279
move objects around. It has to copy
00:16:07.767
some objects in memory, and could potentially
00:16:10.256
have to copy quite a lot of
00:16:12.744
objects.
00:16:13.200
And how long it takes will depend
00:16:15.691
on how many objects it has to
00:16:18.183
copy, how many objects get moved around.
00:16:20.674
It depends on the size of the
00:16:23.165
reachable memory, and it depends on the
00:16:25.656
size of the heap.
00:16:27.180
And it's complicated. You have to move
00:16:29.756
objects around, and that means you have
00:16:32.331
to change anything which points to those
00:16:34.907
objects, you have to change the pointer
00:16:37.482
values.
00:16:37.950
So, not only are you marking the
00:16:41.207
objects, but you're moving them, and you're
00:16:44.463
updating all the pointers that point to
00:16:47.720
those objects.
00:16:48.750
And this means you need a runtime
00:16:51.082
system that knows what is a pointer,
00:16:53.415
and knows which pointers point to particular
00:16:55.747
objects, and can go back from objects
00:16:58.079
to the pointers and update them to
00:17:00.411
point to a new location when the
00:17:02.744
object moves.
00:17:03.510
So this really needs some sort of
00:17:05.406
virtual machine or interpreter, where you can
00:17:07.302
easily update the values of the pointers,
00:17:09.198
where you can easily find and update
00:17:11.094
the values of all of the pointers.
00:17:18.240
The mark compact idea, though,
00:17:22.200
is quite nice because it gives you
00:17:26.158
very fast allocation, once it's completed.
00:17:29.550
And it’s the inspiration for another class
00:17:33.507
of garbage collection algorithms, known as copying
00:17:37.465
collectors.
00:17:38.130
The idea of copying collectors is to
00:17:41.177
try to integrate all of these operations
00:17:44.224
into one pass.
00:17:45.630
So it tries to integrate the traversing
00:17:49.027
through the object graph, the marking of
00:17:52.424
the live objects, and the copying of
00:17:55.821
those objects into a contiguous region,
00:17:58.732
into one pass. And make freeing the
00:18:02.129
remaining memory essentially free.
00:18:04.170
The idea is that, by the time
00:18:06.491
that first pass has executed, all of
00:18:08.812
the live objects have been copied into
00:18:11.134
one region of memory. And all the
00:18:13.455
remaining memory, which is outside of that
00:18:15.776
region, is garbage, or has not been
00:18:18.097
used, and can immediately be marked as
00:18:20.418
free.
00:18:20.850
It’s kind-of like a mark compact scheme,
00:18:22.981
but it's more efficient, and the time
00:18:25.113
it takes to collect depends on number
00:18:27.244
of live objects, depends on the number
00:18:29.375
of objects it finds and copies into
00:18:31.506
the new space. And reclaiming the remaining
00:18:33.638
objects takes essentially no time.
00:18:38.970
So, how does this work?
00:18:41.850
Well, it starts by dividing the heap
00:18:44.632
into two halves, each of which comprises
00:18:47.414
a contiguous block of memory. So you're
00:18:50.196
only working in one half of the
00:18:52.978
total heap memory.
00:18:54.270
So, you’ve immediately wasted half the memory.
00:18:56.981
You're using half the memory at a
00:18:59.693
time.
00:19:00.180
And you allocate memory from that half
00:19:02.993
of the heap only. So, every time
00:19:05.806
programmer allocates a new object, it allocates
00:19:08.619
memory in a contiguous fashion in one
00:19:11.432
half of the heap.
00:19:13.140
And memory allocation is fast, because it's
00:19:15.346
just allocating the next free address in
00:19:17.552
the heap, and it just proceeds,
00:19:19.443
in order, through the memory in a
00:19:21.650
contiguous fashion.
00:19:22.380
And it means you didn't need to
00:19:24.542
worry about fragmentation, because you've got the
00:19:26.704
whole of this half of the heap
00:19:28.867
to allocate from, and again you're just
00:19:31.029
passing through it linearly in a contiguous
00:19:33.191
way.
00:19:33.600
And you follow this through, until you
00:19:35.838
get to perform an allocation and you
00:19:38.076
find it won’t fit. You find you've
00:19:40.314
used the entirety of that half of
00:19:42.552
a heap, and there's no more space
00:19:44.790
left.
00:19:45.210
At that point the garbage collector is
00:19:46.995
triggered.
00:19:50.190
The garbage collector stops the execution of
00:19:53.216
the program,
00:19:54.180
and makes a pass through the active
00:19:56.297
half of the heap, the half of
00:19:58.413
the heap you were just allocating from.
00:20:00.630
It passes linearly through that, through the
00:20:03.453
heap, based on the root set of
00:20:06.275
the program, and any live objects it
00:20:09.098
finds, it copies into the other half
00:20:11.920
of the heap.
00:20:13.230
So it identifies the root set,
00:20:15.289
based on the global variables and the
00:20:17.690
stack, the variables on the stack frames,
00:20:20.092
and follows the pointers from those into
00:20:22.494
the heap.
00:20:23.280
And any of those objects it adds
00:20:25.423
to this to the unused half,
00:20:27.260
what’s called the “to space”, the other
00:20:29.403
half of the heap. It follows all
00:20:31.546
the pointers, adding them into the heap
00:20:33.689
in order. So it moves them into
00:20:35.832
a contiguous region of the other half
00:20:37.975
of the heap memory.
00:20:39.300
It uses an algorithm known as the
00:20:42.077
Cheney algorithm to do that.
00:20:44.160
And once it's followed all of the
00:20:48.404
pointers,
00:20:49.110
anything which has not been copied into
00:20:52.279
the other half of the heap is
00:20:55.449
unreachable, and gets ignored.
00:20:57.360
At that point, once it's copied everything
00:20:59.897
over,
00:21:00.360
it restarts the program, but with allocations
00:21:03.137
running from the other half of the
00:21:05.915
heap memory, the half of the heap
00:21:08.692
towards it just copied the all of
00:21:11.469
the live data, the “to space”.
00:21:13.950
And which half of the heap is
00:21:16.336
then active is just switched over,
00:21:18.381
and it runs, and it carries on
00:21:20.767
as normal, allocating in a contiguous pattern
00:21:23.153
in the other half of the heap
00:21:25.539
memory.
00:21:25.980
So, essentially, the program only uses half
00:21:29.249
of the heap.
00:21:30.750
And it uses that until it run
00:21:32.817
all the way through, and used that
00:21:34.883
region. And then the collector runs,
00:21:36.654
and it copies into the other half
00:21:38.721
of the memory, allocates from there.
00:21:40.492
And then, once it's full, the collector
00:21:42.559
runs again and it flips back.
00:21:44.430
So it's only ever using half of
00:21:46.548
the available heap memory at once.
00:21:48.363
So it's wasting half of the memory.
00:21:50.580
But when the collector runs, it just
00:21:52.659
has to copy the live objects to
00:21:54.738
the other side, and carries on.
00:21:56.520
It flips around between the two halves
00:21:58.599
of the memory.
00:22:03.240
How does it do the copying?
00:22:05.460
It uses what’s called a breadth-first algorithm,
00:22:08.989
known as the Cheney algorithm.
00:22:11.610
The idea of this is that you
00:22:15.044
have a queue of objects waiting to
00:22:18.479
be copied.
00:22:19.560
You start by looking at the root
00:22:21.728
set of objects, the global variables and
00:22:23.895
all the stack allocated variables, and for
00:22:26.063
each of those you push them into
00:22:28.231
the queue.
00:22:28.950
And then you start at the beginning
00:22:31.505
of the queue,
00:22:32.700
with the first object in the queue,
00:22:34.906
and you look at that object and
00:22:37.113
you see does it have pointers to
00:22:39.319
other objects we haven't seen yet?
00:22:41.310
If it does, you push those objects
00:22:44.530
which are referenced on to the end
00:22:47.750
of the queue.
00:22:49.230
Then, you take the object at the
00:22:51.161
head of the queue. you mark it
00:22:53.093
has as having been processed, and you
00:22:55.024
copy it into the other semi-space,
00:22:56.680
into the other half of the heap.
00:22:58.611
And then you move on, you do
00:23:00.543
this for the next object in the
00:23:02.474
queue,
00:23:02.850
and you add anything it references on
00:23:04.941
to the end of the queue,
00:23:06.734
you copy it into the other semi-space
00:23:08.825
and so on. So you're continually going
00:23:10.916
through this queue of objects,
00:23:12.510
anything they reference gets added to the
00:23:14.655
end of the queue, and you keep
00:23:16.799
going until eventually run out of the
00:23:18.944
queue.
00:23:19.350
So you’re sort-of racing through the queue,
00:23:21.026
taking things off the head of the
00:23:22.702
queue whilst adding them on to the
00:23:24.378
end as you find new objects.
00:23:25.815
And eventually you reach the end of
00:23:27.491
the queue, that means you found all
00:23:29.167
of the live objects and you're done.
00:23:30.843
And everything has been copied over.
00:23:36.750
So, why is this a benefit?
00:23:41.220
Well, the time it takes to collect
00:23:44.024
the memory depends on how many things
00:23:46.829
that copied.
00:23:47.730
And that depends on the number of
00:23:50.141
live objects. The only things that get
00:23:52.551
copied are
00:23:53.340
objects which are reachable from the root
00:23:55.607
set. The only things that get copied
00:23:57.874
are objects which are alive at the
00:24:00.141
point when the collector runs.
00:24:01.860
And the number of dead objects doesn't
00:24:04.378
affect the performance. And the total the
00:24:06.896
size of the heap doesn't affect the
00:24:09.414
performance. The only thing that affects the
00:24:11.933
performance is the set of objects which
00:24:14.451
are currently accessible.
00:24:15.630
Now, if most objects die young,
00:24:19.033
if most objects don't live very long,
00:24:23.004
at the point that collector runs it
00:24:26.974
doesn't need to process them.
00:24:29.910
So you can trade-off the
00:24:32.880
amount of time spent on the garbage
00:24:35.785
collector,
00:24:36.300
by changing the size of the semi-spaces,
00:24:38.964
by changing the amount of memory allocated
00:24:41.628
to the system.
00:24:42.870
The bigger the semi-space, the less often
00:24:45.597
the garbage collector has to run.
00:24:47.935
And if most objects are no longer
00:24:50.663
alive at the point when it runs,
00:24:53.490
it's only copying a small number of
00:24:55.841
objects. So it's copying a fairly small
00:24:58.192
set, and it's copying it less often,
00:25:00.544
so the total amount of time taken
00:25:02.895
for the garbage collector goes down.
00:25:05.010
So you can trade-off between how much
00:25:07.682
memory the system uses, how big the
00:25:10.355
heap has to be, how big the
00:25:13.027
semi spaces have to be, for how
00:25:15.699
long the garbage collector is running.
00:25:18.090
Do most objects do you? Are most
00:25:21.252
objects of short-lived in programs?
00:25:23.610
Well, the statistics show that yes,
00:25:26.158
they do. Most programs have a set
00:25:29.130
of core, of long-lived, objects that comprise
00:25:32.102
their fundamental data structures,
00:25:33.900
and then a lot of ephemeral objects
00:25:36.291
just live for a small amount of
00:25:38.682
time, and disappear after a particular function
00:25:41.074
has finished, for example.
00:25:42.540
So, quite often, this is a good
00:25:45.377
trade-off. By only copying the objects which
00:25:48.214
are currently alive,
00:25:49.530
and ignoring those that have just lived
00:25:52.227
for a little while and are ephemeral,
00:25:54.924
you can get quite a good performance
00:25:57.621
win, in terms of time spent collecting
00:26:00.318
garbage, by using a copying collector.
00:26:02.730
The disadvantage, though, is it uses more
00:26:05.032
memory. At any point it's only using
00:26:07.335
half of the available heap memory.
00:26:09.308
And the more memory you can give
00:26:11.611
it, the better it performs in terms
00:26:13.913
of processor overhead.
00:26:15.000
So you have an automatic memory management
00:26:18.113
scheme that trades-off unused, wasted, memory for
00:26:21.226
low processor overhead.
00:26:27.600
So that's the basic garbage collection algorithms:
00:26:31.079
the mark sweep, the mark compact,
00:26:34.062
and the copying algorithms.
00:26:36.150
Where they differ, is where the cost
00:26:40.158
is.
00:26:40.830
Do they spend time when memory allocation
00:26:43.958
happens, like a mark sweep algorithm,
00:26:46.639
because it has to search through the
00:26:49.767
free list and find an appropriate sized
00:26:52.896
space to put the object,
00:26:55.230
so they can have quite a high
00:26:58.908
overhead to allocate memory?
00:27:01.110
Or, do they,
00:27:03.000
like them up the mark compact,
00:27:05.868
and especially the copying collectors, have a
00:27:09.214
more complex collection algorithm, where they have
00:27:12.560
to copy some of these objects around,
00:27:15.906
but gain from making memory allocation very
00:27:19.252
fast?
00:27:19.830
So, they’re trading-off memory usage for processing
00:27:23.493
time. And some of these algorithms,
00:27:26.633
mark sweep,
00:27:27.780
has less memory overhead, but it's bad
00:27:30.964
in terms of processing time, time for
00:27:34.148
the collector, in terms of allocation time,
00:27:37.331
and in terms of poor locality of
00:27:40.515
reference.
00:27:41.070
Whereas the copying collectors have very good
00:27:43.922
locality of reference, they're very efficient,
00:27:46.367
but they waste a lot of memory.
00:27:49.320
So you have this trade-off between the
00:27:53.629
difference purposes.
00:27:54.960
The mark sweep algorithm doesn't move memory
00:27:57.587
around, so it can work in any
00:28:00.215
language. The mark compact, and the copying
00:28:02.842
algorithms, move data, so they need to
00:28:05.469
be able to unambiguously identify pointers,
00:28:07.721
and update the pointers to the objects
00:28:10.349
which had been moved.
00:28:15.150
And that's it for this part.
00:28:17.353
In the next part, I’ll move on
00:28:19.923
and talk about generational garbage collection algorithms,
00:28:22.494
which extend the idea of the copying
00:28:25.064
collectors to get improved efficiency.
Part 2: Generational and Incremental Garbage Collection
The second part of the lecture shows how copying garbage collection
algorithms can be improved, taking into account typical object
lifetimes, to produce the widely used generation garbage collection
algorithm. Then, it discusses how generation algorithms can, in turn,
be enhance to support incremental operations that reduces the pause
times for the program.
00:00:00.000
In this part of the lecture,
00:00:02.006
I’d like to move on and talk
00:00:04.345
about generational and incremental garbage collection algorithms.
00:00:06.685
I’ll talk a bit about object lifetimes,
00:00:09.025
about copying generational garbage collectors, and about
00:00:11.365
how to make garbage collection incremental.
00:00:15.300
So how long do the objects that
00:00:17.621
need to be garbage collected live?
00:00:19.710
Well, people have done studies of a
00:00:22.275
lot of programs, and it seems that
00:00:24.840
most of the time, most of the
00:00:27.405
objects in the programs actually have a
00:00:29.971
fairly short lifetime.
00:00:31.170
There’s a core of objects that are
00:00:33.632
long lived, that live for a significant
00:00:36.095
fraction of the duration of the program,
00:00:38.557
and that comprise the main data structure
00:00:41.019
that the program is working with.
00:00:43.230
And then, in most cases, there are
00:00:45.812
a large number of ephemeral objects which
00:00:48.395
come into being, are processed during the
00:00:50.977
lifetime of a particular function, or a
00:00:53.560
particular method, or a particular object,
00:00:55.773
and then which die fairly quickly,
00:00:57.987
and then are no longer referenced.
00:01:00.300
And this seems to be generally true.
00:01:02.952
People have done studies in a range
00:01:05.604
of different languages,
00:01:06.840
and programs in a range of different
00:01:09.549
domains, and over a long time period,
00:01:12.258
and the same statistic seems to be
00:01:14.967
popping up again and again. Most objects
00:01:17.675
live for a very short time,
00:01:19.997
but there's a core of very long
00:01:22.706
lived objects.
00:01:23.580
Now, obviously different programs, different programming languages,
00:01:26.621
produce different amounts of garbage, but the
00:01:29.662
principle seems to hold.
00:01:31.500
There are some implications of this when
00:01:33.966
it comes to building garbage collectors.
00:01:36.180
The first is that, when the garbage
00:01:39.095
collector runs, it's likely that live objects
00:01:42.010
will be a minority. There'll be a
00:01:44.925
relatively small number of objects which have
00:01:47.840
been around for a long time that
00:01:50.756
comprise the core data that the program
00:01:53.671
is working on.
00:01:55.020
And there'll be a bunch of objects
00:01:57.657
that have been created, used for some
00:02:00.294
purpose since the last round of the
00:02:02.932
collector, and are now no longer reachable.
00:02:05.569
And that the majority of objects that
00:02:08.206
the garbage collector is looking at won't
00:02:10.843
be any more reachable.
00:02:12.450
It also seems likely that the longer
00:02:14.978
an object has lived, the longer it's
00:02:17.506
likely to live.
00:02:18.690
If an object becomes part of the
00:02:20.974
core data on which the system is
00:02:23.258
working, it’s likely to live for most
00:02:25.542
of the lifetime of a program,
00:02:27.600
whereas if it isn’t, it's likely to
00:02:29.840
die very quickly.
00:02:30.900
And anything which survives for a significant
00:02:32.986
fraction of time, anything that lives for
00:02:35.071
more than a couple of runs of
00:02:37.157
the garbage collector, is likely to be
00:02:39.242
one of those long lived objects.
00:02:41.130
Things either die very quickly, or they
00:02:43.757
live for a very long time,
00:02:46.008
and there’s not so many objects that
00:02:48.634
have intermediate lifetimes.
00:02:49.860
I think the question, then, is can
00:02:51.988
we design a garbage collector to take
00:02:54.115
advantage of this statistic? Can we design
00:02:56.243
a garbage collector which understands that most
00:02:58.371
objects die young, and optimises its behaviour
00:03:00.498
as a result?
00:03:05.640
There's a class of garbage collection algorithms,
00:03:08.637
known as generational garbage collection, which tries
00:03:11.634
to do this. It tries to optimise
00:03:14.631
the garbage collection based on the statistics
00:03:17.627
of object life times.
00:03:19.440
In your typical generational garbage collector,
00:03:22.298
the heap is split into two regions.
00:03:25.632
One region for long lived objects,
00:03:28.490
and one region for short lived,
00:03:31.347
young, objects.
00:03:32.400
And the regions holding the young objects
00:03:35.270
are garbage collected quite frequently, whereas the
00:03:38.140
regions holding the older, long-lived, objects are
00:03:41.010
collected less frequently, on the assumption those
00:03:43.880
objects like to stay alive longer.
00:03:46.440
And objects are moved between the regions
00:03:49.004
as it becomes clear that those objects
00:03:51.568
are likely to be long lived,
00:03:53.766
are likely to have a long lifetime.
00:03:56.430
And the way this is typically described
00:03:59.167
is with two generations: a young generation,
00:04:01.904
and a long lived older generation.
00:04:04.350
But, of course, there's no reason you
00:04:07.099
can't split it into multiple generations,
00:04:09.455
and have a young, a middle aged,
00:04:12.204
and a long lived generation if you
00:04:14.952
want, although the benefits of multiple generations
00:04:17.701
go down once you more than two.
00:04:20.550
A typical way this is done,
00:04:23.322
is what's called a stop-and-copy algorithm using
00:04:26.557
semi-spaces with the two generations. This is
00:04:29.791
essentially running two instances of the copying
00:04:33.025
collector we described in the last part,
00:04:36.260
one to manage each generation.
00:04:38.670
The way this works is, initially,
00:04:41.249
everything starts as a young object.
00:04:43.827
And the heap is partitioned into two
00:04:46.836
regions, one for young objects, and one
00:04:49.844
for long lived objects. And initially all
00:04:52.853
the objects are allocated from the younger
00:04:55.861
generation region of the heap.
00:04:58.110
Each of those two regions, that for
00:05:00.658
young objects, and that for long lived
00:05:03.205
objects, is in turn split into two.
00:05:05.753
So we've divided the heap into quarters.
00:05:08.400
And each of those regions is
00:05:11.250
managed using a copying collector. So,
00:05:13.641
in the space allocated for the younger
00:05:16.430
generation we're using half of that space
00:05:19.320
initially, and then, when that half gets
00:05:21.654
full, we do the usual copying collector
00:05:23.988
thing of copying across into the other
00:05:26.322
half of the space, and freeing up
00:05:28.656
anything which wasn't copied.
00:05:30.090
So allocations initially start in the younger
00:05:33.304
generation’s region of the heap.
00:05:35.700
They start in the initial semi-space for
00:05:41.111
that region, and
00:05:43.530
memory is allocated linearly in the usual
00:05:46.394
way for a copying collector.
00:05:48.540
When that region becomes full, a garbage
00:05:51.155
collection happens as usual.
00:05:55.440
And, as usual, with a copying collector,
00:05:58.424
it passes through the heap and anything
00:06:01.407
which is still alive gets copied over
00:06:04.391
to the other half of the semi-space
00:06:07.375
for the young region.
00:06:09.180
The addition here, though, is that as
00:06:11.811
it’s copying the objects, it tags them
00:06:14.443
with how many times they’ve successfully been
00:06:17.074
copied.
00:06:17.550
So, if an object survives the initial
00:06:20.241
garbage collection, and gets copied into the
00:06:22.933
initial half of the semi space,
00:06:25.240
the counter for how many times it
00:06:27.931
has lived
00:06:28.800
is incremented by one.
00:06:31.860
And this process continues in the space
00:06:34.602
allocated for the younger generation, with the
00:06:37.344
usual copying collector flipping between the two
00:06:40.086
halves of the semi-space, each time it
00:06:42.828
collects.
00:06:43.320
Objects that survive more than a certain
00:06:46.631
number of garbage collection cycles, and that
00:06:49.943
may be as small as one or
00:06:53.254
two cycles,
00:06:54.300
are assumed to be long lived objects.
00:06:56.701
So, if they're alive after, if they
00:06:59.101
survived some threshold number of collections,
00:07:01.159
they’re assumed to be long lived objects,
00:07:03.660
and when the collector next runs,
00:07:07.398
rather than copying into the other half
00:07:11.759
of the younger generation semi-space, they’re copied
00:07:16.119
into the space for the older generation.
00:07:20.580
And this process continues, and eventually the
00:07:23.531
space for the older generation becomes full,
00:07:26.481
as more and more objects that copied
00:07:29.432
into it. And that point the older
00:07:32.382
generation space is garbage collected.
00:07:34.590
And again, that follows the usual approach
00:07:37.723
you'd expect with a copying collector,
00:07:40.408
and it takes that half of the
00:07:43.540
older generation space, copies the live objects
00:07:46.673
into the other half, and deallocates any
00:07:49.805
unreferenced objects in the older generation.
00:07:52.590
What we see is that the younger
00:07:55.927
generations are collected very frequently,
00:07:58.410
and there’s a lot, there's a lot
00:08:00.276
of short lived objects, so that space
00:08:02.141
tends to fill up quite quickly.
00:08:03.840
And the younger generation is bouncing between
00:08:06.432
the two halves of that semi-space quite
00:08:09.024
quickly. And then, much more slowly,
00:08:11.245
objects get copied into the older generation
00:08:13.837
space, and eventually that will fill up
00:08:16.428
and collection will be performed there.
00:08:18.750
And, as the diagram on the left
00:08:21.388
shows, we see the young generation repeatedly
00:08:24.026
bouncing around between the two halves of
00:08:26.664
its space, and then the older generation
00:08:29.302
gradually filling up and eventually being copied.
00:08:32.040
And, the way this diagram is drawn,
00:08:34.434
it looks like the younger generation and
00:08:36.829
the older generation both have half of
00:08:39.223
the heap, and have equal amounts of
00:08:41.618
memory.
00:08:42.060
In practice, the older generation probably needs
00:08:44.224
less space than the younger generation,
00:08:46.079
as there tend to be a lot
00:08:48.243
more short lived objects, so you might
00:08:50.408
adjust the size of the different regions
00:08:52.572
to match.
00:08:57.450
Now the younger generation and the older
00:09:00.014
generation must be collected independently. The short
00:09:02.578
lived objects are collected and much more
00:09:05.142
frequently than the long lived objects.
00:09:07.440
But it's also possible that there are
00:09:10.796
references between the different generations. There may
00:09:14.152
be young objects that,
00:09:16.170
short lived objects that, hold references to
00:09:19.335
long-lived objects, and there might be long
00:09:22.500
lived objects that hold references to young,
00:09:25.666
short-lived, objects.
00:09:26.670
References from short-lived objects to long-lived objects
00:09:29.922
is straightforward. Most of the time,
00:09:32.710
the short-lived object is going to die
00:09:35.962
before the long-lived object is collected;
00:09:38.850
most of the time it's even going
00:09:41.842
to die before the garbage collection of
00:09:44.833
the younger generation is performed.
00:09:47.070
So, if it does happen that a
00:09:49.976
collection of the long-lived generation is scheduled,
00:09:52.882
then it's probably sufficient to treat the
00:09:55.788
young generation as part of the root
00:09:58.694
set for the long-lived generation.
00:10:00.870
There won’t be too many live objects
00:10:03.183
in the younger generation, so if you
00:10:05.497
just scan through the young generation,
00:10:07.480
find all of those objects, and treat
00:10:09.793
them as the root set, and then
00:10:12.106
they will reference into the long lived
00:10:14.420
objects.
00:10:14.850
References from long-lived objects to younger objects
00:10:20.886
more problematic.
00:10:22.710
The issue here is that, obviously,
00:10:25.262
you need to scan the portion of
00:10:28.240
the heap allocated for the long lived
00:10:31.218
objects in order to detect those,
00:10:33.870
but the benefit of the generational collection
00:10:36.355
comes from separating the two regions of
00:10:38.840
the heap out, such that you don't
00:10:41.325
need to perform such scans.
00:10:43.200
If you're going to scan the whole
00:10:46.123
heap to find the references from long-lived
00:10:49.046
to short-lived objects, you've lost a lot
00:10:51.969
of the benefits of doing the generational
00:10:54.892
collection.
00:10:55.410
Quite often, therefore, what happens is that
00:10:59.242
pointers from long-lived to short-lived objects are
00:11:03.073
done using an indirection table.
00:11:05.910
The long-lived objects points to a region,
00:11:13.410
known as the indirection table, a region
00:11:17.594
that holds references to short-lived objects,
00:11:21.181
so they’re pointers to pointers,
00:11:24.270
whereas pointers within the long-lived generation are
00:11:29.408
just regular pointers.
00:11:31.710
And the idea here is that when
00:11:34.494
you’re garbage collecting the young generation,
00:11:36.881
you treat the indirection table
00:11:38.970
as part of the root set of
00:11:41.568
the younger generation, and you don't have
00:11:44.166
to scan the rest of the heap.
00:11:46.765
You only explicitly look at known pointers
00:11:49.363
from long-lived objects to short-lived objects.
00:11:51.690
This is also a benefit because obviously
00:11:56.534
the short-lived generation
00:11:58.710
gets garbage collected much more frequently,
00:12:01.716
so those objects move between the two
00:12:05.222
halves of the young generation semi-space quite
00:12:08.729
frequently,
00:12:09.330
which means that, if there are references
00:12:12.079
from long lived objects to short-lived objects,
00:12:14.829
you need to update those references quite
00:12:17.578
often, as the objects are frequently copied
00:12:20.327
around.
00:12:20.820
And having those references in an indirection
00:12:23.743
table means you don't have to scan
00:12:26.665
the whole of long-lived generation’s heap in
00:12:29.588
order to update the references as well.
00:12:32.610
This tends not to be a big
00:12:35.068
issue. It’s not particularly common for long-lived
00:12:37.525
objects to refer to short-lived objects.
00:12:39.632
It’s much more common for them to
00:12:42.089
be the other way around in a
00:12:44.547
lot of code.
00:12:49.530
And this approach is actually very widely
00:12:52.697
used.
00:12:53.250
This is the way the HotSpot garbage
00:12:57.157
collector in the Java virtual machine works,
00:13:01.064
for example.
00:13:02.280
And it can be very efficient,
00:13:05.302
in terms of processor overhead.
00:13:07.920
The cost of a copying generational collector
00:13:11.845
depends on the number of live objects.
00:13:15.870
And most objects are in the short-lived
00:13:20.799
generation, most objects die young,
00:13:24.420
and so it's
00:13:26.940
frequently garbage collecting the short-lived generation,
00:13:29.653
but it's typically not copying many objects
00:13:32.817
each time, because most of the objects
00:13:35.982
haven't lived very long.
00:13:37.890
So there's not much processor overhead in
00:13:40.618
doing that, and the objects which do
00:13:43.346
live for a long time, and which
00:13:46.073
would need to be repeatedly copied,
00:13:48.412
are in the long-lived generation,
00:13:50.460
and that's not
00:13:52.200
garbage collected particularly often, and so the
00:13:55.975
overhead of copying them is small.
00:13:59.310
Although when it does need to garbage
00:14:02.482
collect the long-lived generation, that can be
00:14:05.654
quite slow.
00:14:06.660
The cost, though, is in terms of
00:14:09.250
memory.
00:14:09.720
It’s split the heap in into four
00:14:12.531
regions, and it's only using half of
00:14:15.342
each region at once, so there's quite
00:14:18.154
a high memory overhead.
00:14:19.860
It's got a lot of unused memory
00:14:22.466
at any one point with a copying
00:14:25.071
generational collector. So it’s trading off low
00:14:27.677
processor overhead for high memory overheads.
00:14:34.110
So, as we saw, a generation collector
00:14:37.884
can be very efficient.
00:14:40.140
But, it stops the world while it
00:14:44.200
runs.
00:14:44.880
And often that's not a big problem.
00:14:47.640
Often that's not a big problem,
00:14:49.885
because it is just collecting the
00:14:52.230
heap for the younger generation, the short-lived
00:14:55.391
generation, and that happens quite quickly.
00:14:58.200
But occasionally it needs to collect the
00:15:01.008
heap for the long-lived generation, and that
00:15:03.816
can involve scanning a reasonable amount of
00:15:06.625
space, copying a lot of long-lived objects,
00:15:09.433
and that can be quite slow.
00:15:11.940
Incremental garbage collection algorithms try to spread
00:15:14.941
the cost of garbage collection out.
00:15:17.512
They try to run the garbage collection
00:15:20.513
in a way that the program doesn't
00:15:23.514
need to be stopped to allow the
00:15:26.514
collector to run.
00:15:27.900
And this is beneficial for interactive applications,
00:15:30.986
where you don't want a pause which
00:15:34.072
would affect user behaviour, or be user
00:15:37.157
visible, and it's
00:15:38.580
important for real-time applications. If you're building
00:15:41.279
a video conferencing tool, for example,
00:15:43.592
in a garbage collected language you’d,
00:15:45.906
want to bound the time the collector
00:15:48.604
runs so that doesn't disrupt the rendering
00:15:51.303
of the video.
00:15:52.560
And, if you're building a real-time control
00:15:55.621
system in such a language, you'd want
00:15:58.682
to know how long that the collector
00:16:01.744
was running, for each hyper period of
00:16:04.805
the system, so you can schedule real-time
00:16:07.866
tasks to meet all their deadlines.
00:16:10.590
So it'd be useful to have a
00:16:13.638
garbage collector that could operate incrementally.
00:16:16.350
It’d be useful to have a garbage
00:16:18.517
collector that could interleave small amounts of
00:16:20.684
garbage collection, along with small runs of
00:16:22.851
the program execution.
00:16:23.880
So, rather than letting the program run
00:16:26.208
for a while, and then pausing it,
00:16:28.536
scanning the whole heap, or the whole
00:16:30.864
of one generation of the heap in
00:16:33.192
a generational collector,
00:16:34.290
which necessarily takes a long time,
00:16:36.154
it would be useful to have a
00:16:38.328
collector that could collect a small portion
00:16:40.502
of the heap. That takes a very
00:16:42.676
small amount of time to run,
00:16:44.640
so it can spread the execution of
00:16:47.151
the collection out and interleave it with
00:16:49.661
the operation of the program, every time
00:16:52.172
it performs some pointer operation, or every
00:16:54.683
time it
00:16:55.500
enters or exits a method, or something
00:16:58.331
like that, just to spread the cost
00:17:01.161
out significantly.
00:17:02.070
The implication of that, is that the
00:17:04.900
garbage collector can't scan the whole heap.
00:17:07.830
If you allow the collector to scan
00:17:09.745
the heap, it takes a significant amount
00:17:11.660
of time, and requires you to stop
00:17:13.575
the program while it does it.
00:17:15.216
If you want the collector to run
00:17:17.131
much more quickly, it only has the
00:17:19.046
scan part of the heap, it’s only
00:17:20.961
got time to scan parts of the
00:17:22.876
heap.
00:17:23.250
So it's got a scan a fragment
00:17:25.642
of the heap each time.
00:17:27.450
The problem is, if the collector is
00:17:29.902
only scanning part of the heap,
00:17:32.004
then there’s the risk that when the
00:17:34.457
program runs it will change something,
00:17:36.559
while the collector,
00:17:37.710
it will change the heap between the
00:17:39.981
runs up the collector. And so you
00:17:42.252
need some way of coordinating what the
00:17:44.522
garbage collector is doing and what the
00:17:46.793
program is doing. The collector can't stop
00:17:49.064
the program and sweep through the whole
00:17:51.335
heap, marking the objects as alive or
00:17:53.606
dead
00:17:54.030
because, when you pause the collector partway
00:17:56.802
through, the program runs and it obsoletes
00:17:59.574
the marking. So you need some way
00:18:02.346
of keeping track of changes, so as
00:18:05.118
the program runs while the collector is
00:18:07.890
also running, they can coordinate.
00:18:13.080
The way this tends to be done
00:18:15.945
is using an algorithm known as tri-colour
00:18:18.811
marking.
00:18:19.320
Every object in the system is labeled
00:18:21.951
with a colour. And the colour of
00:18:24.583
the object is changed as the collector
00:18:27.214
runs.
00:18:27.690
Objects can be marked as white,
00:18:29.523
which indicates that the garbage collector hasn't
00:18:31.661
looked at them yet in this cycle.
00:18:33.900
They can be marked as grey,
00:18:35.828
which indicates that the garbage collector has
00:18:38.078
looked at them, and it knows that
00:18:40.328
object is alive, but it hasn't yet
00:18:42.578
checked some of the direct children of
00:18:44.827
that object.
00:18:45.570
Or they can be marked as black,
00:18:48.142
which indicates that the object is alive,
00:18:50.713
and all of its directs children have
00:18:53.285
been checked.
00:18:54.120
The basic way the incremental garbage collector
00:18:57.462
works, therefore, is that it scans through
00:19:00.805
the heap.
00:19:01.860
And, as it goes, it marks,
00:19:03.890
it changes the colour of the objects.
00:19:06.258
As it starts to look at an
00:19:08.627
object, it marks grey.
00:19:10.080
And then it checks the references,
00:19:12.432
and marks them grey, and once the
00:19:15.175
objects it references have been checked,
00:19:17.527
it marks the initial object as black.
00:19:20.370
And, there’s a sort of wavefront sweeping
00:19:22.835
through the heap, with white objects ahead
00:19:25.299
of it, grey objects at the head
00:19:27.764
of the wavefront, at the head of
00:19:30.229
the region that's being checked, and black
00:19:32.693
objects behind which are known to be
00:19:35.158
alive.
00:19:35.610
And, eventually, the collector will reach the
00:19:38.220
end of the heap. It will have
00:19:40.830
passed through the whole of the heap,
00:19:43.439
and at that point anything which is
00:19:46.049
still labeled white, which hasn't been found
00:19:48.659
by the collector, is unreachable and is
00:19:51.269
known to be garbage.
00:19:52.860
One of the key invariants is that
00:19:55.322
it's not possible to get a direct
00:19:57.784
pointer from a black object to a
00:20:00.247
white object.
00:20:01.050
Initially, before the heap has been scanned,
00:20:03.764
all the objects are coloured white,
00:20:06.090
and they have pointers to each other,
00:20:08.803
so you have pointers from white objects
00:20:11.517
to white objects.
00:20:12.780
In the part of the heap that
00:20:14.910
has been checked, and is known to
00:20:17.039
be alive, you have black objects which
00:20:19.169
reference other live black objects.
00:20:20.790
And at the wave front, you have
00:20:22.973
objects which were just coloured from white
00:20:25.155
to grey indicating that they may be
00:20:27.338
checked.
00:20:27.750
Add those grey objects may be referencing
00:20:30.315
some objects which are known to be
00:20:32.880
alive, and they may be referencing some
00:20:35.445
objects which are not yet checked and
00:20:38.011
are coloured white.
00:20:39.210
At that grey region, in the wavefront
00:20:41.712
when the collection is happening, you can
00:20:44.214
have pointers to either black or white
00:20:46.715
objects. But in the region that’s not
00:20:49.217
yet checked, or the region that has
00:20:51.719
been checked, you know that all the
00:20:54.221
objects have pointers to the same colour
00:20:56.723
objects.
00:20:57.180
And this is the invariant. Any program
00:20:59.562
operation that tries to create a direct
00:21:01.945
pointer from a black object to a
00:21:04.327
white object requires coordination with the garbage
00:21:06.710
collector.
00:21:10.170
So the program and the collector need
00:21:13.009
to coordinate.
00:21:13.920
The program runs for a while,
00:21:16.730
generates some garbage, is paused to allow
00:21:20.008
part of the garbage collection scan,
00:21:22.818
and the garbage collector runs.
00:21:25.260
In this case, if we look at
00:21:28.075
the before portion of the diagram,
00:21:30.487
object A has been scanned, and is
00:21:33.302
known to be alive,
00:21:35.010
and therefore is marked as black.
00:21:37.490
Objects B and C are reachable via
00:21:40.383
that object,
00:21:41.310
and the garbage collector has found them
00:21:43.976
but has not yet checked all of
00:21:46.642
their children, therefore, it has marked those
00:21:49.307
objects as grey.
00:21:50.550
And object D, and the other object
00:21:52.904
referenced by B, have not yet been
00:21:55.258
checked. So the garbage collector has been
00:21:57.612
running, and has marked these objects.
00:21:59.730
And then the garbage collector is paused.
00:22:02.128
This is an incremental algorithm, and it's
00:22:04.527
interleaving the operation of the collector and
00:22:06.925
the program.
00:22:07.710
So the garbage collector is paused,
00:22:10.142
the program runs, and it changes some
00:22:12.979
of the pointers around.
00:22:14.700
It swaps the pointer from objects A,
00:22:17.168
which was pointing from object A to
00:22:19.637
object C, and the pointer from object
00:22:22.105
B to object D, such that A
00:22:24.573
is now pointing at D, and the
00:22:27.042
object B is now pointing at C
00:22:29.610
And if it does that, it will
00:22:31.596
create a pointer from a black object
00:22:33.582
to a white object. it will create
00:22:35.569
a pointer from object A, which has
00:22:37.555
already been checked and is known to
00:22:39.541
be alive, and therefore its coloured black,
00:22:41.527
down to object D, which has not
00:22:43.514
yet been checked and is coloured white.
00:22:45.600
As it does that, the program has
00:22:48.389
to coordinate with the garbage collector.
00:22:50.880
The program has to change the colours
00:22:53.120
of some of the marked objects.
00:22:55.140
If it doesn’t, when the collector next
00:22:57.730
runs, it will look and find that
00:23:00.320
object A is marked as black,
00:23:02.540
indicating that it’s already been checked,
00:23:04.860
and therefore it won’t check it again.
00:23:06.965
It will look at object B,
00:23:08.770
and see that it's been marked black,
00:23:10.875
and again it won’t check it again.
00:23:13.080
And it will then follow its children,
00:23:15.056
and look at object C, and the
00:23:17.032
other object, which are marked as grey
00:23:19.008
and start checking their children.
00:23:20.520
But what it won't ever do is
00:23:22.713
reach object D, because object D is
00:23:24.907
referenced from an object which is known
00:23:27.100
to be alive, that is marked black,
00:23:29.293
and therefore has been checked.
00:23:30.960
And therefore there's no need to check
00:23:33.449
any of its outstanding children, so object
00:23:35.937
D will be missed, and won't be
00:23:38.426
marked as alive, even though it is
00:23:40.914
reachable.
00:23:41.370
So, to avoid this, when the program
00:23:43.457
is running, if it does any manipulation
00:23:45.543
of the points that create a pointer
00:23:47.630
from an object which is marked black
00:23:49.716
to an object which is marked white,
00:23:51.803
it needs to coordinate with the collector
00:23:53.890
and somehow update the colours.
00:23:58.920
There’s two approaches to doing this.
00:24:00.860
It can do it using either a
00:24:03.123
read barrier, or it can do it
00:24:05.387
using a write barrier.
00:24:06.780
The read barrier approach works by every
00:24:09.576
time the program reads a pointer to
00:24:12.372
a white object, every time it tries
00:24:15.167
get a reference to an object and
00:24:17.963
finds that object is coloured white,
00:24:20.360
then it changes the colour of that
00:24:23.155
object to grey, and then lets the
00:24:25.951
program continue.
00:24:26.850
The idea here is that it's not
00:24:28.934
possible for the program to get a
00:24:31.017
pointer to a white object. And,
00:24:32.803
since it can't get a pointer to
00:24:34.887
a white object, it can't create a
00:24:36.971
pointer from a black object to a
00:24:39.055
white object.
00:24:39.750
Any object the program reads gets marked
00:24:41.743
as grey, which puts it in the
00:24:43.735
set of objects for which the collector
00:24:45.728
knows it has the scan their children.
00:24:47.820
It makes sure that every object that
00:24:50.781
is read, if it is referenced,
00:24:53.320
if the program does change the pointers
00:24:56.281
so it’s referenced by black object,
00:24:58.819
is coloured grey such that the collector
00:25:01.781
will check it.
00:25:03.150
So it avoids creating pointers from black
00:25:05.753
objects to white objects, by making it
00:25:08.356
impossible to get a reference to a
00:25:10.959
white object in the first place.
00:25:13.290
A write barrier, on the other hand,
00:25:16.016
traps attempts to change pointers. So,
00:25:18.353
if the system notices that,
00:25:20.400
if the program tries to change a
00:25:22.445
pointer, such that it's creating a pointer
00:25:24.491
from a black object to a white
00:25:26.536
object, then it changes the colour of
00:25:28.581
one of those objects.
00:25:29.850
It either changes the black object back
00:25:32.357
to grey, such as it gets looked
00:25:34.863
at next time the garbage collector runs.
00:25:37.470
Or it recolours the white objects as
00:25:39.850
grey, again so that it gets looked
00:25:42.230
at next time.
00:25:43.350
And any object which is coloured grey,
00:25:45.730
by either a read barrier or a
00:25:48.110
write barrier, is put back onto the
00:25:50.490
list of objects whose children need to
00:25:52.870
be checked next time the collector runs.
00:25:55.350
And the system proceeds in this way.
00:25:57.763
The collector runs, looks at part of
00:26:00.177
the heap, changes the colour of those
00:26:02.590
objects as it's checking them to see
00:26:05.003
if they’re reachable, and gradually colours the
00:26:07.417
objects from white to grey to black.
00:26:09.930
And then every so often the collector
00:26:12.235
is paused, the program runs for a
00:26:14.541
while,
00:26:14.970
manipulates some pointers, and those pointers change
00:26:17.727
some of the objects back degree,
00:26:20.190
and that those pointer manipulations change some
00:26:22.107
of those objects back to grey.
00:26:23.850
And the two are interleaved, and they’re
00:26:25.745
gradually racing through the heap. And the
00:26:27.640
collector is turning the objects black,
00:26:29.264
and the program is turning them back
00:26:31.159
to grey, and they sort of race
00:26:33.054
until they get all the way through
00:26:34.949
the heap.
00:26:38.520
And there’s a bunch of different variants
00:26:41.589
of this. Some languages prefer read barriers,
00:26:44.658
some languages prefer write barriers.
00:26:46.950
I think that the trade off depends
00:26:49.619
on
00:26:50.100
how common are reads versus write,
00:26:52.723
how efficient is the hardware at trapping
00:26:55.784
pointer accesses, how are pointers was represented
00:26:58.845
in the language and the virtual machine,
00:27:01.906
and so.
00:27:02.880
Typically, I think, this is done using
00:27:05.532
it write barrier, because writes are less
00:27:08.184
common than reads,
00:27:09.420
which makes it cheaper to implement a
00:27:12.581
write barrier, but both approaches work.
00:27:15.390
And you've kind of got a balance
00:27:17.294
between the two.
00:27:18.210
You've got the collector, the garbage collector,
00:27:20.738
running through the memory, gradually trying to
00:27:23.265
collect the heap. And each time the
00:27:25.793
collector is allowed to run it collects
00:27:28.320
a little bit of the heap,
00:27:30.487
marks some of the objects as black.
00:27:33.015
And you've got the program
00:27:34.920
running concurrently, which is changing the objects
00:27:37.317
back to grey, and is creating new
00:27:39.714
unchecked objects. And they're kind of racing
00:27:42.111
through the heap, and you have to
00:27:44.508
hope the garbage collector keeps up with
00:27:46.906
the rate at which the program is
00:27:49.303
generating new garbage.
00:27:50.430
And the risk, of course, is that
00:27:52.743
the garbage collector isn't given enough cycles
00:27:55.057
to run, and the program gets ahead
00:27:57.370
of it, and the garbage collection cycle
00:27:59.683
never finishes. The program is always creating
00:28:01.997
new garbage, faster than garbage collector can
00:28:04.410
mark it, such that the collector never
00:28:06.785
gets to the end of the heap
00:28:09.161
scan,
00:28:09.600
and can never reclaim the memory.
00:28:12.960
If that happens, eventually, the system will
00:28:14.887
run out of memory. It will just
00:28:16.814
have filled the heap space, because the
00:28:18.742
collector hasn't finished the collection and freed
00:28:20.669
some of it up.
00:28:21.870
And at that point, the only thing
00:28:23.999
you can do is just stop the
00:28:26.127
program, let the garbage collector finish,
00:28:27.951
and it will then reclaim the memory.
00:28:30.180
And the art of building an incremental
00:28:32.650
collector is in sizing the amount of
00:28:35.121
time given to the garbage collection algorithm,
00:28:37.591
and the time slices given to the
00:28:40.061
garbage collection algorithm,
00:28:41.220
such it can keep up with the
00:28:43.557
program, so it can keep up with
00:28:45.895
the rate of allocation, and does successfully
00:28:48.233
work its way through the whole heap,
00:28:50.670
free up some memory,
00:28:52.830
and begin the next cycle, and the
00:28:55.318
program doesn't always out race it.
00:29:00.120
So that's all I want to say
00:29:03.184
about the generational and incremental algorithms.
00:29:05.910
The generational algorithms trade-off
00:29:09.660
memory use for processor time.
00:29:13.710
They’re processor efficient, they don't use much
00:29:16.490
processor time, but because they split the
00:29:19.270
memory into multiple regions, they tend to
00:29:22.050
end up wasting a lot of memory.
00:29:24.930
The incremental algorithms have relatively high overhead,
00:29:28.648
because they have to track the reads
00:29:32.367
and writes to the pointers, because they’re
00:29:36.085
continually marking the objects,
00:29:38.310
but they allow the garbage collection pauses
00:29:41.568
to be made a lot smaller.
00:29:44.361
So you're trading off the total time
00:29:47.620
spent garbage collecting, for allowing that time
00:29:50.878
to be performed in small pauses rather
00:29:54.137
than in big blocks of time.
00:29:57.030
In the next part, I’ll move on
00:23:29.534
to talk about real-time collection, which builds
00:17:02.038
on the incremental garbage collection ideas,
00:11:29.899
and talk about some of the practical
00:05:02.403
problems that affect garbage collectors.
Part 3: Practical Factors
The final part of this lecture discusses some practical factors that
affect garbage collection. It considers how garbage collection can be
adapted to support real-time systems, building on the ideas of
incremental garbage collection. And it considers the memory overhead
of garbage collection and its interactions with virtual memory, and
compares this behaviour to that of manual memory management and region
based memory management. Finally, garbage collection for weakly typed
programming languages is briefly considered.
00:00:00.000
In this final part, I just want
00:00:01.971
to touch briefly on some of the
00:00:03.942
practical factors that affect garbage collection.
00:00:05.731
We’ll talk quickly about real time garbage
00:00:08.575
collection, about the memory overheads of using
00:00:11.419
garbage collection,
00:00:12.331
the way it interacts with virtual memory,
00:00:14.715
and how one goes about performing garbage
00:00:17.098
collection for weakly type languages. And then
00:00:19.481
I’ll finish up with just a general
00:00:21.865
discussion of the various trade-offs inherent in
00:00:24.248
different approaches to memory management.
00:00:28.561
So, as we touched on in the
00:00:31.351
last part of the lecture, it's entirely
00:00:34.140
possible to build garbage collectors for real-time
00:00:36.930
systems, although it's not particularly common.
00:00:39.421
The way this is done is that
00:00:42.404
they're built from incremental garbage collectors.
00:00:45.061
And the way you do this,
00:00:47.051
is that you schedule the garbage collector
00:00:49.373
as a periodic task that gets scheduled
00:00:51.695
along with all the other tasks in
00:00:54.018
the system.
00:00:54.781
And real-time systems tend to comprise a
00:00:57.590
set of things operating according to a
00:01:00.399
periodic schedule, performing the different tasks in
00:01:03.208
the system.
00:01:04.111
And the goal is to run an
00:01:07.411
incremental collector that is allocated enough time
00:01:10.710
that it can collect the garbage generated
00:01:14.010
during a complete cycle of the system's
00:01:17.310
operation.
00:01:17.881
So, you need to measure the operation
00:01:20.800
of the system, look at how much
00:01:23.719
garbage each of the various tasks in
00:01:26.637
the system will generate during a complete
00:01:29.556
period of the system’s execution,
00:01:31.741
and schedule a garbage collection task with
00:01:35.263
enough time, enough processor time, that it
00:01:38.785
can collect that much garbage.
00:01:41.401
You need to arrange it such that
00:01:44.252
the amount of garbage generated by the
00:01:47.104
program is bound to be less than
00:01:49.955
the capacity of the collector to collect
00:01:52.807
that garbage in a given cycle.
00:01:55.351
If you're building a hard real time
00:01:57.967
system, that has very strict correctness bounds,
00:02:00.582
very strict deadlines, then you need to
00:02:03.198
be very conservative in the design of
00:02:05.814
the collector,
00:02:06.661
and in the amount of processor time
00:02:09.378
allocated to it, to be sure that
00:02:12.095
it always, no matter what, can collect
00:02:14.811
the amount of garbage that may be
00:02:17.528
generated by the program in each cycle
00:02:20.245
of execution.
00:02:21.121
A soft real time system can have
00:02:24.642
more statistical bounds.
00:02:26.251
And, depending on the available memory capacity,
00:02:29.376
it may be acceptable for it to
00:02:32.502
not to be able to collect,
00:02:35.281
it may be acceptable if it cannot
00:02:37.672
collect, all of the garbage every cycle,
00:02:40.062
provided, on average, it can keep up,
00:02:42.453
and the memory usage can grow and
00:02:44.843
shrink as it does so.
00:02:46.651
The key thing is to make sure
00:02:48.946
that, overall, the collector can keep up
00:02:51.242
and that there’s enough buffer
00:02:52.981
in the system to cope with the
00:02:55.024
cases where it cannot.
00:02:59.041
One thing that should have been clear
00:03:01.889
from the discussion of garbage collection,
00:03:04.330
is that garbage collection algorithms trade-off
00:03:06.871
ease of use for predictability and memory
00:03:09.559
overheads. They’re designed to make it simple
00:03:12.248
for the programmer. They’re designed such that
00:03:14.936
the programmer doesn't need to worry about
00:03:17.624
managing memory, and the garbage collection algorithm
00:03:20.313
will take care of it for them.
00:03:23.101
A consequence is that they are,
00:03:25.427
in many ways, less predictable than manual
00:03:28.140
memory management, in that the programmer tends
00:03:30.853
not to know when the garbage collector
00:03:33.566
will run.
00:03:34.441
And they can have overheads, both in
00:03:37.358
terms of processor time, for the time
00:03:40.276
it takes for the collector to run,
00:03:43.193
and in terms of amount of memory
00:03:46.111
which is used.
00:03:47.461
And, as we saw talking about real
00:03:50.170
time algorithms, as we saw talking about
00:03:52.878
incremental algorithms in the last part,
00:03:55.200
it’s possible to
00:03:56.461
distribute the processor overhead so it's amortised
00:03:59.358
across the execution of the program,
00:04:01.841
or it's possible to have stop-the-world style
00:04:04.738
collectors, as we discussed in the first
00:04:07.636
part of this lecture,
00:04:09.391
which perhaps have lower overhead overheads,
00:04:12.468
but pause the program for long periods
00:04:16.057
of time while they collect.
00:04:18.721
The other aspect of garbage collectors is
00:04:22.122
that they tend to use significantly more
00:04:25.523
memory than correctly written programs that use
00:04:28.924
manual memory management.
00:04:30.481
And a lot of that is because
00:04:33.286
the garbage collection algorithms are trading-off
00:04:35.791
memory usage for CPU usage, and we
00:04:38.946
saw this when we were talking about
00:04:42.102
the copying collectors. By having the two
00:04:45.257
semi-spaces and copying between them,
00:04:47.611
since they only need to copy the
00:04:50.010
live objects, the amount of copying needed
00:04:52.409
is quite small, which means that the
00:04:54.808
CPU usage of these collectors is quite
00:04:57.207
small, and they can get good locality
00:04:59.606
of reference.
00:05:00.391
But the trade off is that they
00:05:02.721
use twice as much memory, because they
00:05:05.051
have two semi-spaces, only one of which
00:05:07.381
is in use at any particular time.
00:05:09.811
And, again, as we saw in the
00:05:12.396
last part when we talk about generational
00:05:14.982
collectors,
00:05:15.451
you have multiple generations, each of which
00:05:17.987
with multiple semi-spaces, and again the system
00:05:20.522
is using only a small fraction of
00:05:23.058
the memory which is allocated to it,
00:05:25.593
so they have a relatively high memory
00:05:28.129
overheads.
00:05:28.591
If the goal is to design a
00:05:31.540
system that uses the least amount of
00:05:34.488
memory, then a manual memory management scheme
00:05:37.437
or a region based memory management scheme
00:05:40.385
can, if implemented correctly, have significantly lower
00:05:43.334
memory overheads than a garbage collector.
00:05:45.961
The problem, of course, is that manual
00:05:48.376
memory management is very difficult to do
00:05:50.791
correctly, and programs that use manual memory
00:05:53.206
management incorrectly can have significant memory leaks,
00:05:55.621
and can waste a lots of memory
00:05:58.036
in that respect.
00:06:03.751
Another issue with
00:06:06.241
garbage collectors is that they interact poorly
00:06:09.788
with the virtual memory system.
00:06:12.421
Garbage collectors need to scan through the
00:06:15.821
heap to find which memory has been
00:06:19.321
in use, which objects are still alive,
00:06:22.614
which objects are ready to be reclaimed.
00:06:25.906
And this means that they need to
00:06:29.199
look through the entire heap.
00:06:31.651
This disrupts the cache, in that it's
00:06:34.622
pulling memory into the cache, so it
00:06:37.593
evicts any hot data from the cache
00:06:40.563
and just pulls in
00:06:42.361
a complete view of the memory,
00:06:44.286
so it trashes the cache.
00:06:45.991
It also interacts poorly with virtual memory,
00:06:48.556
in that if any of these pages
00:06:51.121
were paged out to disk, because they’re
00:06:53.685
not used when the garbage collector runs,
00:06:56.250
it will have to page them in
00:06:58.815
again, from disk to memory, to check
00:07:01.380
those pages and inspect them for live
00:07:03.945
objects.
00:07:04.411
And this can
00:07:06.811
affect performance, because it evicts things from
00:07:09.740
cache, because it evicts needed and frequently
00:07:12.669
used pages from RAM, and possibly pages
00:07:15.598
them out to disk, and it can
00:07:18.527
lead to thrashing if the working set
00:07:21.457
of the garbage collector is larger than
00:07:24.386
the physical memory.
00:07:25.741
And I think it’s, to some extent,
00:07:28.188
an open research issue how to effectively
00:07:30.634
combine virtual memory with garbage collection.
00:07:36.751
In addition, garbage collectors rely on being
00:07:41.231
able to identify pointers.
00:07:43.891
They rely on being able to identify
00:07:46.337
which are live objects and, for many
00:07:48.784
of these collectors, they rely on being
00:07:51.230
able to move objects around and update
00:07:53.676
references to point to the new location
00:07:56.123
for those objects.
00:07:57.271
This means they need to be able
00:07:59.495
to determine what is a pointer.
00:08:01.501
And in strongly typed languages, in languages
00:08:05.020
running on virtual machines, or in interpreters,
00:08:08.540
this is relatively straightforward.
00:08:10.651
The type system knows what's a pointer,
00:08:13.422
what's a reference, and it knows how
00:08:16.193
it's implemented, and can you trawl through
00:08:18.965
the innards of the virtual machine and
00:08:21.736
update the pointers when objects move.
00:08:24.211
In more weakly typed languages, that can
00:08:27.261
be difficult. It the language permits casts
00:08:30.311
between integers and pointers, for example,
00:08:32.925
like as possible in C or in
00:08:35.975
C++,
00:08:36.511
it's possible for programs to hide pointers
00:08:40.142
in integers, and perform pointer arithmetic to
00:08:43.773
generate pointers which the garbage collector can’t
00:08:47.404
easily see.
00:08:48.541
And this makes it difficult, and in
00:08:52.230
some cases impossible, to write garbage collectors
00:08:55.920
for these languages.
00:08:57.601
For example, if you wanted to write
00:08:59.783
a garbage collector for C,
00:09:01.441
which would do away with the free()
00:09:03.814
call and just automatically reclaim memory that
00:09:06.187
was no longer referenced, it's difficult to
00:09:08.560
do so because it's hard to tell
00:09:10.932
what is a valid pointer in C,
00:09:13.305
because it can be cast to and
00:09:15.678
from integers, and because of pointer arithmetic.
00:09:18.151
It's not impossible.
00:09:20.431
You can just assume that anything that
00:09:23.295
could potentially be a pointer, is a
00:09:26.160
pointer. and treat all integers, all pointer
00:09:29.024
sized integers, as if they were valid
00:09:31.888
pointers, and keep the memory of those
00:09:34.753
locations alive.
00:09:35.671
But it has some costs to doing
00:09:39.392
so. The link on the slide points
00:09:43.114
to a garbage collector that does this,
00:09:46.835
and works for C, for strictly conforming
00:09:50.556
C programs, but it's not generally a
00:09:54.278
recommended approach.
00:09:55.441
Languages which are strongly typed, but dynamic,
00:09:59.791
such as Python or Ruby, for example,
00:10:01.971
would avoid this problem. It’s always possible
00:10:04.151
to tell what's a pointer there,
00:10:06.020
even though the types of objects can
00:10:08.200
change,
00:10:08.611
so it would be possible to write
00:10:10.879
a garbage collector for such languages,
00:10:12.823
although the implementations don't currently use one.
00:10:20.491
Fundamentally, when we think about memory management,
00:10:24.646
there's a trade off.
00:10:27.121
There's a trade off between complexity and
00:10:31.515
performance, where the complexity happens, and how
00:10:35.910
predictable the performance is.
00:10:38.521
Garbage collected languages sit at one end
00:10:41.477
of that trade-off. They have runtime complexity,
00:10:44.432
that they need to implement the garbage
00:10:47.388
collector, and to be able to move
00:10:50.343
objects around, and update pointers, and so
00:10:53.299
on.
00:10:53.821
And they are relatively less predictable,
00:10:58.201
in that it’s not clear when the
00:11:00.694
garbage collector will run, or how long
00:11:03.187
it will take to run, or how
00:11:05.680
it will move objects around.
00:11:07.561
But they're relatively straightforward for the programmer.
00:11:10.812
They don't have a lot of cognitive
00:11:14.063
overhead on the programmer.
00:11:16.021
On the other end of the spectrum
00:11:18.878
is manual memory management and automatic memory
00:11:21.734
management techniques, based on region based schemes,
00:11:24.591
such as those in Rust.
00:11:26.731
And these are much more predictable,
00:11:29.126
if correctly implemented, because you know exactly
00:11:31.919
when objects are going to be allocated
00:11:34.713
and freed.
00:11:35.611
But they move the complexity, they move
00:11:38.825
the complexity to compile time.
00:11:41.221
In a language like Java, for example,
00:11:44.809
you only have one type of reference,
00:11:48.397
and the runtime garbage collector takes care
00:11:51.985
of deallocating references, and saves the programmer
00:11:55.573
from worrying about
00:11:57.211
object lifetimes, and so on. Whereas if
00:11:59.581
you look into language like Rust,
00:12:01.611
you've got three different types of reference,
00:12:04.081
and borrowing and ownership rules, and the
00:12:06.632
programmer has to think about ownership from
00:12:09.183
a very early stage.
00:12:10.741
So it's giving more cognitive overhead to
00:12:13.430
the programmer. It’s giving the programmer more
00:12:16.120
design time, more compile time, things to
00:12:18.809
worry about. But it gets much more
00:12:21.498
predictable performance, and much lower runtime overheads,
00:12:24.188
both in terms of memory and CPU
00:12:26.877
costs.
00:12:27.361
And ultimately, I think that's the trade
00:12:31.526
off.
00:12:32.221
Are you willing to push the complexity
00:12:35.298
on to the programmer, get them to
00:12:38.376
think about memory management, think about the
00:12:41.453
overheads, think about ownership of data?
00:12:44.191
And, as a result, get good performance.
00:12:47.341
Or are you willing to trade that
00:12:50.553
off, and say that the programmer shouldn't
00:12:53.765
need to worry about these things,
00:12:56.519
and we're willing to accept less predictable
00:12:59.731
behaviour, higher runtime CPU overheads, higher runtime
00:13:02.943
memory overheads.
00:13:03.961
What's the trade-off you make? For some
00:13:07.019
applications, it's perfectly reasonable to put that
00:13:10.076
trade-off onto runtime, and save the programmer
00:13:13.134
the complexity. And for others, the runtime
00:13:16.192
overheads are too significant, and you need
00:13:19.250
to get the programmers to think about
00:13:22.307
these issues.
00:13:23.281
And systems code tends to be on
00:13:25.694
to the side of compile time performance,
00:13:28.107
and pushing this overhead on to the
00:13:30.519
programmers, because it often operates at the
00:13:32.932
limits of what's achievable.
00:13:34.411
Whereas a lot of the applications,
00:13:36.451
the performance constraints are perhaps lower,
00:13:38.491
and it makes more sense to use
00:13:40.871
a garbage collected language, save the programmer
00:13:43.251
the overhead, but accept the runtime costs.
00:13:49.021
So that's what I want to say
00:13:51.618
about memory management.
00:13:52.831
We spoke about bunch of different garbage
00:13:56.008
collection algorithms, starting with the very simple
00:13:59.185
mark sweep algorithm, mark compact, copying,
00:14:01.909
generational, and incremental algorithms, and touching on
00:14:05.086
some of the real-time issues and the
00:14:08.263
practical factors.
00:14:09.271
In the next lecture, I want to
00:14:11.301
start to talk, instead, about concurrency.
Discussion
Lecture 6 focussed on garbage collection. It started with a discussion
of simple mark-sweep garbage collectors, then moved on to discuss the
gradually more sophisticated mark-compact, copying, and generational
algorithms. It made the observation that most objects die young, and
used this to motivate generational algorithms, and noted that these
have good performance and are widely implemented. It also discussed
incremental garbage collection and tri-colour marking, and suggested
that this could form a basis for real-time collection. It concluded
by discussing the overheads of garbage collection, and the trade-offs
inherent in different automatic memory management schemes.
Discussion will be, primarily, on how the operation of garbage collection
algorithms, but will also focus on the trade-offs inherent in automatic
memory management.
Rust pushes memory management complexity onto the programmer, in the form
of a more complex type system and the need to consider multiple different
types of pointer, and in limiting the types of data structure that can be
expressed. In return, it gives predictable run-time performance, low
run-time overheads, and a uniform resource management framework. Garbage
collection, on the other hand, imposes more run-time costs and complexity,
but is considerably simpler for programmer. What is the right trade-off?