1Stdlib.Gc(3) OCaml library Stdlib.Gc(3)
2
3
4
6 Stdlib.Gc - no description
7
9 Module Stdlib.Gc
10
12 Module Gc
13 : (module Stdlib__gc)
14
15
16
17
18
19
20
21 type stat = {
22 minor_words : float ; (* Number of words allocated in the minor heap
23 since the program was started. This number is accurate in byte-code
24 programs, but only an approximation in programs compiled to native
25 code.
26 *)
27 promoted_words : float ; (* Number of words allocated in the minor
28 heap that survived a minor collection and were moved to the major heap
29 since the program was started.
30 *)
31 major_words : float ; (* Number of words allocated in the major heap,
32 including the promoted words, since the program was started.
33 *)
34 minor_collections : int ; (* Number of minor collections since the
35 program was started.
36 *)
37 major_collections : int ; (* Number of major collection cycles com‐
38 pleted since the program was started.
39 *)
40 heap_words : int ; (* Total size of the major heap, in words.
41 *)
42 heap_chunks : int ; (* Number of contiguous pieces of memory that
43 make up the major heap.
44 *)
45 live_words : int ; (* Number of words of live data in the major heap,
46 including the header words.
47 *)
48 live_blocks : int ; (* Number of live blocks in the major heap.
49 *)
50 free_words : int ; (* Number of words in the free list.
51 *)
52 free_blocks : int ; (* Number of blocks in the free list.
53 *)
54 largest_free : int ; (* Size (in words) of the largest block in the
55 free list.
56 *)
57 fragments : int ; (* Number of wasted words due to fragmentation.
58 These are 1-words free blocks placed between two live blocks. They are
59 not available for allocation.
60 *)
61 compactions : int ; (* Number of heap compactions since the program
62 was started.
63 *)
64 top_heap_words : int ; (* Maximum size reached by the major heap, in
65 words.
66 *)
67 stack_size : int ; (* Current size of the stack, in words.
68
69
70 Since 3.12.0
71 *)
72 }
73
74
75 The memory management counters are returned in a stat record.
76
77 The total amount of memory allocated by the program since it was
78 started is (in words) minor_words + major_words - promoted_words .
79 Multiply by the word size (4 on a 32-bit machine, 8 on a 64-bit
80 machine) to get the number of bytes.
81
82
83 type control = {
84
85 mutable minor_heap_size : int ; (* The size (in words) of the minor
86 heap. Changing this parameter will trigger a minor collection.
87 Default: 256k.
88 *)
89
90 mutable major_heap_increment : int ; (* How much to add to the major
91 heap when increasing it. If this number is less than or equal to 1000,
92 it is a percentage of the current heap size (i.e. setting it to 100
93 will double the heap size at each increase). If it is more than 1000,
94 it is a fixed number of words that will be added to the heap. Default:
95 15.
96 *)
97
98 mutable space_overhead : int ; (* The major GC speed is computed from
99 this parameter. This is the memory that will be "wasted" because the
100 GC does not immediately collect unreachable blocks. It is expressed as
101 a percentage of the memory used for live data. The GC will work more
102 (use more CPU time and collect blocks more eagerly) if space_overhead
103 is smaller. Default: 80.
104 *)
105
106 mutable verbose : int ; (* This value controls the GC messages on
107 standard error output. It is a sum of some of the following flags, to
108 print messages on the corresponding events:
109
110 - 0x001 Start of major GC cycle.
111
112 - 0x002 Minor collection and major GC slice.
113
114 - 0x004 Growing and shrinking of the heap.
115
116 - 0x008 Resizing of stacks and memory manager tables.
117
118 - 0x010 Heap compaction.
119
120 - 0x020 Change of GC parameters.
121
122 - 0x040 Computation of major GC slice size.
123
124 - 0x080 Calling of finalisation functions.
125
126 - 0x100 Bytecode executable and shared library search at start-up.
127
128 - 0x200 Computation of compaction-triggering condition.
129
130 - 0x400 Output GC statistics at program exit. Default: 0.
131
132 *)
133
134 mutable max_overhead : int ; (* Heap compaction is triggered when the
135 estimated amount of "wasted" memory is more than max_overhead percent
136 of the amount of live data. If max_overhead is set to 0, heap com‐
137 paction is triggered at the end of each major GC cycle (this setting is
138 intended for testing purposes only). If max_overhead >= 1000000 , com‐
139 paction is never triggered. If compaction is permanently disabled, it
140 is strongly suggested to set allocation_policy to 1. Default: 500.
141 *)
142
143 mutable stack_limit : int ; (* The maximum size of the stack (in
144 words). This is only relevant to the byte-code runtime, as the native
145 code runtime uses the operating system's stack. Default: 1024k.
146 *)
147
148 mutable allocation_policy : int ; (* The policy used for allocating in
149 the heap. Possible values are 0 and 1. 0 is the next-fit policy,
150 which is quite fast but can result in fragmentation. 1 is the
151 first-fit policy, which can be slower in some cases but can be better
152 for programs with fragmentation problems. Default: 0.
153
154
155 Since 3.11.0
156 *)
157 window_size : int ; (* The size of the window used by the major GC
158 for smoothing out variations in its workload. This is an integer
159 between 1 and 50. Default: 1.
160
161
162 Since 4.03.0
163 *)
164 custom_major_ratio : int ; (* Target ratio of floating garbage to
165 major heap size for out-of-heap memory held by custom values located in
166 the major heap. The GC speed is adjusted to try to use this much memory
167 for dead values that are not yet collected. Expressed as a percentage
168 of major heap size. The default value keeps the out-of-heap floating
169 garbage about the same size as the in-heap overhead. Note: this only
170 applies to values allocated with caml_alloc_custom_mem (e.g. bigar‐
171 rays). Default: 44.
172
173
174 Since 4.08.0
175 *)
176 custom_minor_ratio : int ; (* Bound on floating garbage for
177 out-of-heap memory held by custom values in the minor heap. A minor GC
178 is triggered when this much memory is held by custom values located in
179 the minor heap. Expressed as a percentage of minor heap size. Note:
180 this only applies to values allocated with caml_alloc_custom_mem (e.g.
181 bigarrays). Default: 100.
182
183
184 Since 4.08.0
185 *)
186 custom_minor_max_size : int ; (* Maximum amount of out-of-heap memory
187 for each custom value allocated in the minor heap. When a custom value
188 is allocated on the minor heap and holds more than this many bytes,
189 only this value is counted against custom_minor_ratio and the rest is
190 directly counted against custom_major_ratio . Note: this only applies
191 to values allocated with caml_alloc_custom_mem (e.g. bigarrays).
192 Default: 8192 bytes.
193
194
195 Since 4.08.0
196 *)
197 }
198
199
200 The GC parameters are given as a control record. Note that these
201 parameters can also be initialised by setting the OCAMLRUNPARAM envi‐
202 ronment variable. See the documentation of ocamlrun .
203
204
205
206 val stat : unit -> stat
207
208 Return the current values of the memory management counters in a stat
209 record. This function examines every heap block to get the statistics.
210
211
212
213 val quick_stat : unit -> stat
214
215 Same as stat except that live_words , live_blocks , free_words ,
216 free_blocks , largest_free , and fragments are set to 0. This function
217 is much faster than stat because it does not need to go through the
218 heap.
219
220
221
222 val counters : unit -> float * float * float
223
224 Return (minor_words, promoted_words, major_words) . This function is
225 as fast as quick_stat .
226
227
228
229 val minor_words : unit -> float
230
231 Number of words allocated in the minor heap since the program was
232 started. This number is accurate in byte-code programs, but only an
233 approximation in programs compiled to native code.
234
235 In native code this function does not allocate.
236
237
238 Since 4.04
239
240
241
242 val get : unit -> control
243
244 Return the current values of the GC parameters in a control record.
245
246
247
248 val set : control -> unit
249
250
251 set r changes the GC parameters according to the control record r .
252 The normal usage is: Gc.set { (Gc.get()) with Gc.verbose = 0x00d }
253
254
255
256
257 val minor : unit -> unit
258
259 Trigger a minor collection.
260
261
262
263 val major_slice : int -> int
264
265
266 major_slice n Do a minor collection and a slice of major collection. n
267 is the size of the slice: the GC will do enough work to free (on aver‐
268 age) n words of memory. If n = 0, the GC will try to do enough work to
269 ensure that the next automatic slice has no work to do. This function
270 returns an unspecified integer (currently: 0).
271
272
273
274 val major : unit -> unit
275
276 Do a minor collection and finish the current major collection cycle.
277
278
279
280 val full_major : unit -> unit
281
282 Do a minor collection, finish the current major collection cycle, and
283 perform a complete new cycle. This will collect all currently unreach‐
284 able blocks.
285
286
287
288 val compact : unit -> unit
289
290 Perform a full major collection and compact the heap. Note that heap
291 compaction is a lengthy operation.
292
293
294
295 val print_stat : out_channel -> unit
296
297 Print the current values of the memory management counters (in
298 human-readable form) into the channel argument.
299
300
301
302 val allocated_bytes : unit -> float
303
304 Return the total number of bytes allocated since the program was
305 started. It is returned as a float to avoid overflow problems with int
306 on 32-bit machines.
307
308
309
310 val get_minor_free : unit -> int
311
312 Return the current size of the free space inside the minor heap.
313
314
315 Since 4.03.0
316
317
318
319 val get_bucket : int -> int
320
321
322 get_bucket n returns the current size of the n -th future bucket of the
323 GC smoothing system. The unit is one millionth of a full GC. Raise
324 Invalid_argument if n is negative, return 0 if n is larger than the
325 smoothing window.
326
327
328 Since 4.03.0
329
330
331
332 val get_credit : unit -> int
333
334
335 get_credit () returns the current size of the "work done in advance"
336 counter of the GC smoothing system. The unit is one millionth of a full
337 GC.
338
339
340 Since 4.03.0
341
342
343
344 val huge_fallback_count : unit -> int
345
346 Return the number of times we tried to map huge pages and had to fall
347 back to small pages. This is always 0 if OCAMLRUNPARAM contains H=1 .
348
349
350 Since 4.03.0
351
352
353
354 val finalise : ('a -> unit) -> 'a -> unit
355
356
357 finalise f v registers f as a finalisation function for v . v must be
358 heap-allocated. f will be called with v as argument at some point
359 between the first time v becomes unreachable (including through weak
360 pointers) and the time v is collected by the GC. Several functions can
361 be registered for the same value, or even several instances of the same
362 function. Each instance will be called once (or never, if the program
363 terminates before v becomes unreachable).
364
365 The GC will call the finalisation functions in the order of dealloca‐
366 tion. When several values become unreachable at the same time (i.e.
367 during the same GC cycle), the finalisation functions will be called in
368 the reverse order of the corresponding calls to finalise . If finalise
369 is called in the same order as the values are allocated, that means
370 each value is finalised before the values it depends upon. Of course,
371 this becomes false if additional dependencies are introduced by assign‐
372 ments.
373
374 In the presence of multiple OCaml threads it should be assumed that any
375 particular finaliser may be executed in any of the threads.
376
377 Anything reachable from the closure of finalisation functions is con‐
378 sidered reachable, so the following code will not work as expected:
379
380 - let v = ... in Gc.finalise (fun _ -> ...v...) v
381
382 Instead you should make sure that v is not in the closure of the final‐
383 isation function by writing:
384
385 - let f = fun x -> ... let v = ... in Gc.finalise f v
386
387 The f function can use all features of OCaml, including assignments
388 that make the value reachable again. It can also loop forever (in this
389 case, the other finalisation functions will not be called during the
390 execution of f, unless it calls finalise_release ). It can call
391 finalise on v or other values to register other functions or even
392 itself. It can raise an exception; in this case the exception will
393 interrupt whatever the program was doing when the function was called.
394
395
396 finalise will raise Invalid_argument if v is not guaranteed to be
397 heap-allocated. Some examples of values that are not heap-allocated
398 are integers, constant constructors, booleans, the empty array, the
399 empty list, the unit value. The exact list of what is heap-allocated
400 or not is implementation-dependent. Some constant values can be
401 heap-allocated but never deallocated during the lifetime of the pro‐
402 gram, for example a list of integer constants; this is also implementa‐
403 tion-dependent. Note that values of types float are sometimes allo‐
404 cated and sometimes not, so finalising them is unsafe, and finalise
405 will also raise Invalid_argument for them. Values of type 'a Lazy.t
406 (for any 'a ) are like float in this respect, except that the compiler
407 sometimes optimizes them in a way that prevents finalise from detecting
408 them. In this case, it will not raise Invalid_argument , but you should
409 still avoid calling finalise on lazy values.
410
411 The results of calling String.make , Bytes.make , Bytes.create ,
412 Array.make , and ref are guaranteed to be heap-allocated and non-con‐
413 stant except when the length argument is 0 .
414
415
416
417 val finalise_last : (unit -> unit) -> 'a -> unit
418
419 same as Gc.finalise except the value is not given as argument. So you
420 can't use the given value for the computation of the finalisation func‐
421 tion. The benefit is that the function is called after the value is
422 unreachable for the last time instead of the first time. So contrary to
423 Gc.finalise the value will never be reachable again or used again. In
424 particular every weak pointer and ephemeron that contained this value
425 as key or data is unset before running the finalisation function. More‐
426 over the finalisation functions attached with Gc.finalise are always
427 called before the finalisation functions attached with Gc.finalise_last
428 .
429
430
431 Since 4.04
432
433
434
435 val finalise_release : unit -> unit
436
437 A finalisation function may call finalise_release to tell the GC that
438 it can launch the next finalisation function without waiting for the
439 current one to return.
440
441
442 type alarm
443
444
445 An alarm is a piece of data that calls a user function at the end of
446 each major GC cycle. The following functions are provided to create
447 and delete alarms.
448
449
450
451 val create_alarm : (unit -> unit) -> alarm
452
453
454 create_alarm f will arrange for f to be called at the end of each major
455 GC cycle, starting with the current cycle or the next one. A value of
456 type alarm is returned that you can use to call delete_alarm .
457
458
459
460 val delete_alarm : alarm -> unit
461
462
463 delete_alarm a will stop the calls to the function associated to a .
464 Calling delete_alarm a again has no effect.
465
466
467
468
469
470OCamldoc 2019-07-30 Stdlib.Gc(3)