| 1 |
.. TODO cleanup after merge of gc-del |
| 2 |
|
| 3 |
|
| 4 |
.. _garbage-collection: |
| 5 |
|
| 6 |
============================= |
| 7 |
Garbage Collection in RPython |
| 8 |
============================= |
| 9 |
|
| 10 |
.. contents:: |
| 11 |
|
| 12 |
|
| 13 |
Introduction |
| 14 |
============ |
| 15 |
|
| 16 |
The overview and description of our garbage collection strategy and |
| 17 |
framework can be found in the `EU-report on this topic`_. Please refer |
| 18 |
to that file for an old, but still more or less accurate, description. |
| 19 |
The present document describes the specific garbage collectors that we |
| 20 |
wrote in our framework. |
| 21 |
|
| 22 |
.. _EU-report on this topic: https://foss.heptapod.net/pypy/extradoc/-/tree/branch/extradoc/eu-report/D07.1_Massive_Parallelism_and_Translation_Aspects-2007-02-28.pdf |
| 23 |
|
| 24 |
|
| 25 |
Garbage collectors currently written for the GC framework |
| 26 |
========================================================= |
| 27 |
|
| 28 |
Reminder: to select which GC you want to include in a translated |
| 29 |
RPython program, use the ``--gc=NAME`` option of ``translate.py``. |
| 30 |
For more details, see the `overview of command line options for |
| 31 |
translation`_. |
| 32 |
|
| 33 |
The following overview is written in chronological order, so the "best" |
| 34 |
GC (which is the default when translating) is the last one below. |
| 35 |
|
| 36 |
.. _overview of command line options for translation: config/commandline.html#translation |
| 37 |
|
| 38 |
Mark and Sweep |
| 39 |
-------------- |
| 40 |
|
| 41 |
Classical Mark and Sweep collector. Also contained a lot of experimental |
| 42 |
and half-unmaintained features. Was removed. |
| 43 |
|
| 44 |
Semispace copying collector |
| 45 |
--------------------------- |
| 46 |
|
| 47 |
Two arenas of equal size, with only one arena in use and getting filled |
| 48 |
with new objects. When the arena is full, the live objects are copied |
| 49 |
into the other arena using Cheney's algorithm. The old arena is then |
| 50 |
cleared. See :source:`rpython/memory/gc/semispace.py`. |
| 51 |
|
| 52 |
On Unix the clearing is done by reading ``/dev/zero`` into the arena, |
| 53 |
which is extremely memory efficient at least on Linux: it lets the |
| 54 |
kernel free the RAM that the old arena used and replace it all with |
| 55 |
allocated-on-demand memory. |
| 56 |
|
| 57 |
The size of each semispace starts at 8MB but grows as needed when the |
| 58 |
amount of objects alive grows. |
| 59 |
|
| 60 |
Generational GC |
| 61 |
--------------- |
| 62 |
|
| 63 |
This is a two-generations GC. See :source:`rpython/memory/gc/generation.py`. |
| 64 |
|
| 65 |
It is implemented as a subclass of the Semispace copying collector. It |
| 66 |
adds a nursery, which is a chunk of the current semispace. Its size is |
| 67 |
computed to be half the size of the CPU Level 2 cache. Allocations fill |
| 68 |
the nursery, and when it is full, it is collected and the objects still |
| 69 |
alive are moved to the rest of the current semispace. |
| 70 |
|
| 71 |
The idea is that it is very common for objects to die soon after they |
| 72 |
are created. Generational GCs help a lot in this case, particularly if |
| 73 |
the amount of live objects really manipulated by the program fits in the |
| 74 |
Level 2 cache. Moreover, the semispaces fill up much more slowly, |
| 75 |
making full collections less frequent. |
| 76 |
|
| 77 |
Hybrid GC |
| 78 |
--------- |
| 79 |
|
| 80 |
This is a three-generations GC. |
| 81 |
|
| 82 |
It is implemented as a subclass of the Generational GC. The Hybrid GC |
| 83 |
can handle both objects that are inside and objects that are outside the |
| 84 |
semispaces ("external"). The external objects are not moving and |
| 85 |
collected in a mark-and-sweep fashion. Large objects are allocated as |
| 86 |
external objects to avoid costly moves. Small objects that survive for |
| 87 |
a long enough time (several semispace collections) are also made |
| 88 |
external so that they stop moving. |
| 89 |
|
| 90 |
This is coupled with a segregation of the objects in three generations. |
| 91 |
Each generation is collected much less often than the previous one. The |
| 92 |
division of the generations is slightly more complicated than just |
| 93 |
nursery / semispace / external; see the diagram at the start of the |
| 94 |
source code, in :source:`rpython/memory/gc/hybrid.py`. |
| 95 |
|
| 96 |
Mark & Compact GC |
| 97 |
----------------- |
| 98 |
|
| 99 |
Killed in trunk. The following documentation is for historical purposes |
| 100 |
only. |
| 101 |
|
| 102 |
Inspired, at least partially, by Squeak's garbage collector, this is a |
| 103 |
single-arena GC in which collection compacts the objects in-place. The |
| 104 |
main point of this GC is to save as much memory as possible (to be not |
| 105 |
worse than the Semispace), but without the peaks of double memory usage |
| 106 |
during collection. |
| 107 |
|
| 108 |
Unlike the Semispace GC, collection requires a number of passes over the |
| 109 |
data. This makes collection quite slower. Future improvements could be |
| 110 |
to add a nursery to Mark & Compact in order to mitigate this issue. |
| 111 |
|
| 112 |
During a collection, we reuse the space in-place if it is still large |
| 113 |
enough. If not, we need to allocate a new, larger space, and move the |
| 114 |
objects there; however, this move is done chunk by chunk, and chunks are |
| 115 |
cleared (i.e. returned to the OS) as soon as they have been moved away. |
| 116 |
This means that (from the point of view of the OS) a collection will |
| 117 |
never cause an important temporary growth of total memory usage. |
| 118 |
|
| 119 |
More precisely, a collection is triggered when the space contains more |
| 120 |
than N*M bytes, where N is the number of bytes alive after the previous |
| 121 |
collection and M is a constant factor, by default 1.5. This guarantees |
| 122 |
that the total memory usage of the program never exceeds 1.5 times the |
| 123 |
total size of its live objects. |
| 124 |
|
| 125 |
The objects themselves are quite compact: they are allocated next to |
| 126 |
each other in the heap, separated by a GC header of only one word (4 |
| 127 |
bytes on 32-bit platforms) and possibly followed by up to 3 bytes of |
| 128 |
padding for non-word-sized objects (e.g. strings). There is a small |
| 129 |
extra memory usage during collection: an array containing 2 bytes per |
| 130 |
surviving object is needed to make a backup of (half of) the surviving |
| 131 |
objects' header, in order to let the collector store temporary relation |
| 132 |
information in the regular headers. |
| 133 |
|
| 134 |
Minimark GC |
| 135 |
----------- |
| 136 |
|
| 137 |
This is a simplification and rewrite of the ideas from the Hybrid GC. |
| 138 |
It uses a nursery for the young objects, and mark-and-sweep for the old |
| 139 |
objects. This is a moving GC, but objects may only move once (from |
| 140 |
the nursery to the old stage). |
| 141 |
|
| 142 |
The main difference with the Hybrid GC is that the mark-and-sweep |
| 143 |
objects (the "old stage") are directly handled by the GC's custom |
| 144 |
allocator, instead of being handled by malloc() calls. The gain is that |
| 145 |
it is then possible, during a major collection, to walk through all old |
| 146 |
generation objects without needing to store a list of pointers to them. |
| 147 |
So as a first approximation, when compared to the Hybrid GC, the |
| 148 |
Minimark GC saves one word of memory per old object. |
| 149 |
|
| 150 |
There are :ref:`a number of environment variables |
| 151 |
<minimark-environment-variables>` that can be tweaked to influence the |
| 152 |
GC. (Their default value should be ok for most usages.) |
| 153 |
|
| 154 |
In more detail: |
| 155 |
|
| 156 |
- The small newly malloced objects are allocated in the nursery (case 1). |
| 157 |
All objects living in the nursery are "young". |
| 158 |
|
| 159 |
- The big objects are always handled directly by the system malloc(). |
| 160 |
But the big newly malloced objects are still "young" when they are |
| 161 |
allocated (case 2), even though they don't live in the nursery. |
| 162 |
|
| 163 |
- When the nursery is full, we do a minor collection, i.e. we find |
| 164 |
which "young" objects are still alive (from cases 1 and 2). The |
| 165 |
"young" flag is then removed. The surviving case 1 objects are moved |
| 166 |
to the old stage. The dying case 2 objects are immediately freed. |
| 167 |
|
| 168 |
- The old stage is an area of memory containing old (small) objects. It |
| 169 |
is handled by :source:`rpython/memory/gc/minimarkpage.py`. It is organized |
| 170 |
as "arenas" of 256KB or 512KB, subdivided into "pages" of 4KB or 8KB. |
| 171 |
Each page can either be free, or contain small objects of all the same |
| 172 |
size. Furthermore at any point in time each object location can be |
| 173 |
either allocated or freed. The basic design comes from ``obmalloc.c`` |
| 174 |
from CPython (which itself comes from the same source as the Linux |
| 175 |
system malloc()). |
| 176 |
|
| 177 |
- New objects are added to the old stage at every minor collection. |
| 178 |
Immediately after a minor collection, when we reach some threshold, we |
| 179 |
trigger a major collection. This is the mark-and-sweep step. It walks |
| 180 |
over *all* objects (mark), and then frees some fraction of them (sweep). |
| 181 |
This means that the only time when we want to free objects is while |
| 182 |
walking over all of them; we never ask to free an object given just its |
| 183 |
address. This allows some simplifications and memory savings when |
| 184 |
compared to ``obmalloc.c``. |
| 185 |
|
| 186 |
- As with all generational collectors, this GC needs a write barrier to |
| 187 |
record which old objects have a reference to young objects. |
| 188 |
|
| 189 |
- Additionally, we found out that it is useful to handle the case of |
| 190 |
big arrays specially: when we allocate a big array (with the system |
| 191 |
malloc()), we reserve a small number of bytes before. When the array |
| 192 |
grows old, we use the extra bytes as a set of bits. Each bit |
| 193 |
represents 128 entries in the array. Whenever the write barrier is |
| 194 |
called to record a reference from the Nth entry of the array to some |
| 195 |
young object, we set the bit number ``(N/128)`` to 1. This can |
| 196 |
considerably speed up minor collections, because we then only have to |
| 197 |
scan 128 entries of the array instead of all of them. |
| 198 |
|
| 199 |
- As usual, we need special care about weak references, and objects with |
| 200 |
finalizers. Weak references are allocated in the nursery, and if they |
| 201 |
survive they move to the old stage, as usual for all objects; the |
| 202 |
difference is that the reference they contain must either follow the |
| 203 |
object, or be set to NULL if the object dies. And the objects with |
| 204 |
finalizers, considered rare enough, are immediately allocated old to |
| 205 |
simplify the design. In particular their ``__del__`` method can only |
| 206 |
be called just after a major collection. |
| 207 |
|
| 208 |
- The objects move once only, so we can use a trick to implement id() |
| 209 |
and hash(). If the object is not in the nursery, it won't move any |
| 210 |
more, so its id() and hash() are the object's address, cast to an |
| 211 |
integer. If the object is in the nursery, and we ask for its id() |
| 212 |
or its hash(), then we pre-reserve a location in the old stage, and |
| 213 |
return the address of that location. If the object survives the |
| 214 |
next minor collection, we move it there, and so its id() and hash() |
| 215 |
are preserved. If the object dies then the pre-reserved location |
| 216 |
becomes free garbage, to be collected at the next major collection. |
| 217 |
|
| 218 |
The exact name of this GC is either `minimark` or `incminimark`. The |
| 219 |
latter is a version that does major collections incrementally (i.e. one |
| 220 |
major collection is split along some number of minor collections, rather |
| 221 |
than being done all at once after a specific minor collection). The |
| 222 |
default is `incminimark`, as it seems to have a very minimal impact on |
| 223 |
performance and memory usage at the benefit of avoiding the long pauses |
| 224 |
of `minimark`. |