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

NAME

6       Stdlib.Gc - no description
7

Module

9       Module   Stdlib.Gc
10

Documentation

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