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
23       type stat = {
24        minor_words  : float ;  (* Number of words allocated in the minor heap
25       since the program was started.  This number is  accurate  in  byte-code
26       programs,  but  only  an  approximation  in programs compiled to native
27       code. *)
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        major_words : float ;  (* Number of words allocated in the major heap,
32       including the promoted words, since the program was started. *)
33        minor_collections  :  int  ;  (* Number of minor collections since the
34       program was started. *)
35        major_collections : int ;  (* Number of major collection  cycles  com‐
36       pleted since the program was started. *)
37        heap_words : int ;  (* Total size of the major heap, in words. *)
38        heap_chunks  :  int  ;   (* Number of contiguous pieces of memory that
39       make up the major heap. *)
40        live_words : int ;  (* Number of words of live data in the major heap,
41       including the header words. *)
42        live_blocks : int ;  (* Number of live blocks in the major heap. *)
43        free_words : int ;  (* Number of words in the free list. *)
44        free_blocks : int ;  (* Number of blocks in the free list. *)
45        largest_free  :  int ;  (* Size (in words) of the largest block in the
46       free list. *)
47        fragments : int ;  (* Number of wasted  words  due  to  fragmentation.
48       These are 1-words free blocks placed between two live blocks.  They are
49       not available for allocation. *)
50        compactions : int ;  (* Number of heap compactions since  the  program
51       was started. *)
52        top_heap_words  : int ;  (* Maximum size reached by the major heap, in
53       words. *)
54        }
55
56
57       The memory management counters are returned in a stat record.
58
59       The total amount of memory  allocated  by  the  program  since  it  was
60       started  is  (in  words)  minor_words  + major_words - promoted_words .
61       Multiply by the word size (4  on  a  32-bit  machine,  8  on  a  64-bit
62       machine) to get the number of bytes.
63
64
65
66       type control = {
67
68       mutable  minor_heap_size  :  int ;  (* The size (in words) of the minor
69       heap.   Changing  this  parameter  will  trigger  a  minor  collection.
70       Default: 32k. *)
71
72       mutable major_heap_increment : int ;  (* The minimum number of words to
73       add to the major heap when increasing it.  Default: 62k. *)
74
75       mutable space_overhead : int ;  (* The major GC speed is computed  from
76       this  parameter.   This is the memory that will be "wasted" because the
77       GC does not immediatly collect unreachable blocks.  It is expressed  as
78       a  percentage  of the memory used for live data.  The GC will work more
79       (use more CPU time and collect blocks more eagerly)  if  space_overhead
80       is smaller.  Default: 80. *)
81
82       mutable  verbose  :  int  ;   (* This value controls the GC messages on
83       standard error output.  It is a sum of some of the following flags,  to
84       print messages on the corresponding events:
85
86       - 0x001 Start of major GC cycle.
87
88       - 0x002 Minor collection and major GC slice.
89
90       - 0x004 Growing and shrinking of the heap.
91
92       - 0x008 Resizing of stacks and memory manager tables.
93
94       - 0x010 Heap compaction.
95
96       - 0x020 Change of GC parameters.
97
98       - 0x040 Computation of major GC slice size.
99
100       - 0x080 Calling of finalisation functions.
101
102       - 0x100 Bytecode executable search at start-up.
103
104       - 0x200 Computation of compaction triggering condition.  Default: 0.
105        *)
106
107       mutable  max_overhead : int ;  (* Heap compaction is triggered when the
108       estimated amount of "wasted" memory is more than  max_overhead  percent
109       of  the  amount  of  live data.  If max_overhead is set to 0, heap com‐
110       paction is triggered at the end of each major GC cycle (this setting is
111       intended for testing purposes only).  If max_overhead >= 1000000 , com‐
112       paction is never triggered.  Default: 500. *)
113
114       mutable stack_limit : int ;  (* The  maximum  size  of  the  stack  (in
115       words).   This is only relevant to the byte-code runtime, as the native
116       code runtime uses the operating system's stack.  Default: 256k. *)
117        }
118
119
120       The GC parameters are given as  a  control  record.   Note  that  these
121       parameters  can  also be initialised by setting the OCAMLRUNPARAM envi‐
122       ronment variable.  See the documentation of ocamlrun.
123
124
125
126
127       val stat : unit -> stat
128
129       Return the current values of the memory management counters in  a  stat
130       record.  This function examines every heap block to get the statistics.
131
132
133
134
135       val quick_stat : unit -> stat
136
137       Same  as  stat  except  that  live_words  ,  live_blocks , free_words ,
138       free_blocks , largest_free , and fragments are set to 0.  This function
139       is  much  faster  than  stat because it does not need to go through the
140       heap.
141
142
143
144
145       val counters : unit -> float * float * float
146
147       Return (minor_words, promoted_words, major_words) .  This  function  is
148       as fast at quick_stat .
149
150
151
152
153       val get : unit -> control
154
155       Return the current values of the GC parameters in a control record.
156
157
158
159
160       val set : control -> unit
161
162
163       set  r  changes  the  GC parameters according to the control record r .
164       The normal usage is: Gc.set { (Gc.get()) with Gc.verbose = 0x00d }
165
166
167
168
169
170       val minor : unit -> unit
171
172       Trigger a minor collection.
173
174
175
176
177       val major_slice : int -> int
178
179       Do a minor collection and a slice of major collection.  The argument is
180       the  size of the slice, 0 to use the automatically-computed slice size.
181       In all cases, the result is the computed slice size.
182
183
184
185
186       val major : unit -> unit
187
188       Do a minor collection and finish the current major collection cycle.
189
190
191
192
193       val full_major : unit -> unit
194
195       Do a minor collection, finish the current major collection  cycle,  and
196       perform a complete new cycle.  This will collect all currently unreach‐
197       able blocks.
198
199
200
201
202       val compact : unit -> unit
203
204       Perform a full major collection and compact the heap.  Note  that  heap
205       compaction is a lengthy operation.
206
207
208
209
210       val print_stat : Pervasives.out_channel -> unit
211
212       Print  the  current values of the memory management counters (in human-
213       readable form) into the channel argument.
214
215
216
217
218       val allocated_bytes : unit -> float
219
220       Return the total number  of  bytes  allocated  since  the  program  was
221       started.  It is returned as a float to avoid overflow problems with int
222       on 32-bit machines.
223
224
225
226
227       val finalise : ('a -> unit) -> 'a -> unit
228
229
230       finalise f v registers f as a finalisation function for v .  v must  be
231       heap-allocated.   f  will  be  called  with v as argument at some point
232       between the first time v becomes unreachable and the  time  v  is  col‐
233       lected  by  the  GC.   Several functions can be registered for the same
234       value, or even several instances of the same function.   Each  instance
235       will  be  called  once  (or  never,  if the program terminates before v
236       becomes unreachable).
237
238       The GC will call the finalisation functions in the order  of  dealloca‐
239       tion.   When  several  values become unreachable at the same time (i.e.
240       during the same GC cycle), the finalisation functions will be called in
241       the reverse order of the corresponding calls to finalise .  If finalise
242       is called in the same order as the values  are  allocated,  that  means
243       each  value is finalised before the values it depends upon.  Of course,
244       this becomes false if additional dependencies are introduced by assign‐
245       ments.
246
247       Anything  reachable  from the closure of finalisation functions is con‐
248       sidered reachable, so the following code will not work as expected:
249
250       - let v = ... in Gc.finalise (fun x -> ...) v
251
252       Instead you should write:
253
254       - let f = fun x -> ... ;; let v = ... in Gc.finalise f v
255
256       The f function can use all features of  O'Caml,  including  assignments
257       that make the value reachable again.  It can also loop forever (in this
258       case, the other finalisation functions will be called during the execu‐
259       tion  of  f).   It  can  call finalise on v or other values to register
260       other functions or even itself.  It can raise  an  exception;  in  this
261       case  the  exception will interrupt whatever the program was doing when
262       the function was called.
263
264
265       finalise will raise Invalid_argument if v is not heap-allocated.   Some
266       examples  of  values that are not heap-allocated are integers, constant
267       constructors, booleans, the empty  array,  the  empty  list,  the  unit
268       value.   The exact list of what is heap-allocated or not is implementa‐
269       tion-dependent.  Some constant values can be heap-allocated  but  never
270       deallocated  during  the lifetime of the program, for example a list of
271       integer constants; this is also implementation-dependent.   You  should
272       also  be aware that compiler optimisations may duplicate some immutable
273       values, for example floating-point numbers when stored into arrays,  so
274       they  can be finalised and collected while another copy is still in use
275       by the program.
276
277       The results of calling String.make , String.create , Array.make  ,  and
278       Pervasives.ref  are  guaranteed  to  be heap-allocated and non-constant
279       except when the length argument is 0 .
280
281
282
283
284       val finalise_release : unit -> unit
285
286       A finalisation function may call finalise_release to tell the  GC  that
287       it  can  launch  the next finalisation function without waiting for the
288       current one to return.
289
290
291
292       type alarm
293
294
295       An alarm is a piece of data that calls a user function at  the  end  of
296       each  major  GC  cycle.  The following functions are provided to create
297       and delete alarms.
298
299
300
301
302       val create_alarm : (unit -> unit) -> alarm
303
304
305       create_alarm f will arrange for f to be called at the end of each major
306       GC  cycle, starting with the current cycle or the next one.  A value of
307       type alarm is returned that you can use to call delete_alarm .
308
309
310
311
312       val delete_alarm : alarm -> unit
313
314
315       delete_alarm a will stop the calls to the function associated  to  a  .
316       Calling delete_alarm a again has no effect.
317
318
319
320
321
322
323OCamldoc                          2007-05-24                             Gc(3)
Impressum