1Gc(3)                            OCaml library                           Gc(3)
2
3
4

NAME

6       Gc - Memory management control and statistics; finalised values.
7

Module

9       Module   Gc
10

Documentation

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