forked from dlang/dlang.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgarbage.dd
387 lines (298 loc) · 11.6 KB
/
garbage.dd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
Ddoc
$(SPEC_S Garbage Collection,
$(P D is a fully garbage collected language. That means that it is never
necessary
to free memory. Just allocate as needed, and the garbage collector will
periodically return all unused memory to the pool of available memory.
)
$(P C and C++ programmers accustomed to explicitly managing memory
allocation and
deallocation will likely be skeptical of the benefits and efficacy of
garbage collection. Experience both with new projects written with
garbage collection in mind, and converting existing projects to garbage
collection shows that:
)
$(UL
$(LI Garbage collected programs are often faster. This is
counterintuitive, but the reasons are:
$(UL
$(LI Reference counting is a common solution to solve explicit
memory allocation problems. The code to implement the increment and
decrement operations whenever assignments are made is one source
of slowdown. Hiding it behind smart pointer classes doesn't help
the speed. (Reference counting methods are not a general solution
anyway, as circular references never get deleted.)
)
$(LI Destructors are used to deallocate resources acquired by an object.
For most classes, this resource is allocated memory.
With garbage collection, most destructors then become empty and
can be discarded entirely.
)
$(LI All those destructors freeing memory can become significant when
objects are allocated on the stack. For each one, some mechanism must
be established so that if an exception happens, the destructors all
get called in each frame to release any memory they hold. If the
destructors become irrelevant, then there's no need to set up special
stack frames to handle exceptions, and the code runs faster.
)
$(LI Garbage collection kicks in only when memory gets tight. When
memory is not tight, the program runs at full speed and does not
spend any time tracing and freeing memory.
)
$(LI Garbage collected programs do not suffer from gradual deterioration
due to an accumulation of memory leaks.
)
)
)
$(LI Garbage collectors reclaim unused memory, therefore they do not suffer
from "memory leaks" which can cause long running applications to gradually
consume more and more memory until they bring down the system. GC programs
have longer term stability.
)
$(LI Garbage collected programs have fewer hard-to-find pointer bugs. This
is because there are no dangling references to freed memory. There is no
code to explicitly manage memory, hence no bugs in such code.
)
$(LI Garbage collected programs are faster to develop and debug, because
there's no need for developing, debugging, testing, or maintaining the
explicit deallocation code.
)
)
$(P Garbage collection is not a panacea. There are some downsides:
)
$(UL
$(LI It is not easily predictable when a collection gets run, so the
program can arbitrarily pause.
)
$(LI The time it takes for a collection to run is not bounded. While in
practice it is very quick, this cannot be guaranteed.
)
$(LI All threads other than the collector thread must be halted while
the collection is in progress.
)
$(LI Garbage collectors can keep around some memory that an explicit
deallocator would not.
)
$(LI Garbage collection should be implemented as a basic operating
system
kernel service. But since it is not, garbage collecting programs must
carry around with them the garbage collection implementation. While this
can be a shared library, it is still there.
)
)
$(P These constraints are addressed by techniques outlined
in $(DPLLINK memory.html, Memory Management).
)
$(H2 How Garbage Collection Works)
$(P The GC works by:)
$(OL
$(LI Stopping all other threads than the thread currently trying to
allocate GC memory.)
$(LI $(SINGLEQUOTE Hijacking) the current thread for GC work.)
$(LI Scanning all $(SINGLEQUOTE root) memory ranges for pointers into
GC allocated memory.)
$(LI Recursively scanning all allocated memory pointed to by
roots looking for more pointers into GC allocated memory.)
$(LI Freeing all GC allocated memory that has no active pointers
to it and do not need destructors to run.)
$(LI Queueing all unreachable memory that needs destructors to run.)
$(LI Resuming all other threads.)
$(LI Running destructors for all queued memory.)
$(LI Freeing any remaining unreachable memory.)
$(LI Returning the current thread to whatever work it was doing.)
)
$(H2 Interfacing Garbage Collected Objects With Foreign Code)
$(P The garbage collector looks for roots in:)
$(OL
$(LI the static data segment)
$(LI the stacks and register contents of each thread)
$(LI the TLS (thread-local storage) areas of each thread)
$(LI any roots added by core.memory.GC.addRoot() or core.memory.GC.addRange())
)
$(P If the only pointer to an object
is held outside of these areas, then the collector will miss it and free the
memory.
)
$(P To avoid this from happening, either)
$(UL
$(LI maintain a pointer to the object in an area the collector does scan
for pointers;)
$(LI add a root where a pointer to the object is stored using core.memory.GC.addRoot()
or core.memory.GC.addRange().)
$(LI reallocate and copy the object using the foreign code's storage
allocator
or using the C runtime library's malloc/free.
)
)
$(H2 Pointers and the Garbage Collector)
$(P Pointers in D can be broadly divided into two categories: Those that
point to garbage collected memory, and those that do not. Examples
of the latter are pointers created by calls to C's malloc(), pointers
received from C library routines, pointers to static data,
pointers to objects on the stack, etc. For those pointers, anything
that is legal in C can be done with them.
)
$(P For garbage collected pointers and references, however, there are
some
restrictions. These restrictions are minor, but they are intended
to enable the maximum flexibility in garbage collector design.
)
$(P Undefined behavior:)
$(UL
$(LI Do not xor pointers with other values, like the
xor pointer linked list trick used in C.
)
$(LI Do not use the xor trick to swap two pointer values.
)
$(LI Do not store pointers into non-pointer variables using casts and
other tricks.
------
void* p;
...
int x = cast(int)p; // error: undefined behavior
------
The garbage collector does not scan non-pointer fields for GC pointers.
)
$(LI Do not take advantage of alignment of pointers to store bit flags
in the low order bits:
------
p = cast(void*)(cast(int)p | 1); // error: undefined behavior
------
)
$(LI Do not store into pointers values that may point into the
garbage collected heap:
------
p = cast(void*)12345678; // error: undefined behavior
------
A copying garbage collector may change this value.
)
$(LI Do not store magic values into pointers, other than $(D null).
)
$(LI Do not write pointer values out to disk and read them back in
again.
)
$(LI Do not use pointer values to compute a hash function. A copying
garbage collector can arbitrarily move objects around in memory,
thus invalidating
the computed hash value.
)
$(LI Do not depend on the ordering of pointers:
------
if (p1 < p2) // error: undefined behavior
...
------
since, again, the garbage collector can move objects around in
memory.
)
$(LI Do not add or subtract an offset to a pointer such that the result
points outside of the bounds of the garbage collected object originally
allocated.
------
char* p = new char[10];
char* q = p + 6; // ok
q = p + 11; // error: undefined behavior
q = p - 1; // error: undefined behavior
------
)
$(LI Do not misalign pointers if those pointers may
point into the GC heap, such as:
------
struct Foo {
align (1):
byte b;
char* p; // misaligned pointer
}
------
Misaligned pointers may be used if the underlying hardware
supports them $(B and) the pointer is never used to point
into the GC heap.
)
$(LI Do not use byte-by-byte memory copies to copy pointer values.
This may result in intermediate conditions where there is
not a valid pointer, and if the gc pauses the thread in such a
condition, it can corrupt memory.
Most implementations of $(D memcpy()) will work since the
internal implementation of it does the copy in aligned chunks
greater than or equal to the pointer size, but since this kind of
implementation is not guaranteed by the C standard, use
$(D memcpy()) only with extreme caution.
)
$(LI Do not have pointers in a struct instance that point back
to the same instance. The trouble with this is if the instance
gets moved in memory, the pointer will point back to where it
came from, with likely disastrous results.
)
)
$(P Things that are reliable and can be done:)
$(UL
$(LI Use a union to share storage with a pointer:
------
union U { void* ptr; int value }
------
)
$(LI A pointer to the start of a garbage collected object need not
be maintained if a pointer to the interior of the object exists.
------
char[] p = new char[10];
char[] q = p[3..6];
// q is enough to hold on to the object, don't need to keep
// p as well.
------
)
)
$(P One can avoid using pointers anyway for most tasks. D provides
features
rendering most explicit pointer uses obsolete, such as reference
objects,
dynamic arrays, and garbage collection. Pointers
are provided in order to interface successfully with C APIs and for
some low level work.
)
$(H2 Working with the Garbage Collector)
$(P Garbage collection doesn't solve every memory deallocation problem.
For
example, if a pointer to a large data structure is kept, the garbage
collector cannot reclaim it, even if it is never referred to again. To
eliminate this problem, it is good practice to set a reference or
pointer to an object to null when no longer needed.
)
$(P This advice applies only to static references or references embedded
inside other objects. There is not much point for such stored on the
stack to be nulled because new stack frames are initialized anyway.
)
$(H2 Object Pinning and a Moving Garbage Collector)
$(P Although D does not currently use a moving garbage collector, by following
the rules listed above one can be implemented. No special action is required
to pin objects. A moving collector will only move objects for which there
are no ambiguous references, and for which it can update those references.
All other objects will be automatically pinned.
)
$(H2 D Operations That Involve the Garbage Collector)
$(P Some sections of code may need to avoid using the garbage collector.
The following constructs may allocate memory using the garbage collector:
)
$(UL
$(LI $(GLINK2 expression, NewExpression))
$(LI Array appending)
$(LI Array concatenation)
$(LI Array literals (except when used to initialize static data))
$(LI Associative array literals)
$(LI Any insertion, removal, or lookups in an associative array)
$(LI Extracting keys or values from an associative array)
$(LI Taking the address of (i.e. making a delegate to) a nested function that
accesses variables in an outer scope)
$(LI A function literal that accesses variables in an outer scope)
$(LI An $(GLINK2 expression, AssertExpression) that fails its condition)
)
$(H2 References)
$(UL
$(LI $(XLINK2 http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29, Wikipedia))
$(LI $(XLINK2 http://www.iecc.com/gclist/GC-faq.html, GC FAQ))
$(LI $(XLINK2 ftp://ftp.cs.utexas.edu/pub/garbage/gcsurvey.ps, Uniprocessor Garbage Collector Techniques))
$(LI $(AMAZONLINK 0471941484, Garbage Collection : Algorithms for Automatic Dynamic Memory Management))
)
)
Macros:
TITLE=Garbage Collection
WIKI=Garbage
CATEGORY_SPEC=$0