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