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