1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Objective Caml *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
1997-10-14 06:17:48 -07:00
|
|
|
(* Damien Doligez, projet Para, INRIA Rocquencourt *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
2001-12-07 05:41:02 -08:00
|
|
|
(* under the terms of the GNU Library General Public License, with *)
|
|
|
|
(* the special exception on linking described in file ../LICENSE. *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
2001-10-26 15:37:14 -07:00
|
|
|
(** Memory management control and statistics; finalised values. *)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2001-12-03 14:01:28 -08:00
|
|
|
type stat =
|
2005-10-25 11:34:07 -07:00
|
|
|
{ minor_words : float;
|
2001-10-26 15:37:14 -07:00
|
|
|
(** Number of words allocated in the minor heap since
|
2002-02-08 11:34:49 -08:00
|
|
|
the program was started. This number is accurate in
|
|
|
|
byte-code programs, but only an approximation in programs
|
|
|
|
compiled to native code. *)
|
2002-02-01 04:23:58 -08:00
|
|
|
|
2001-12-03 14:01:28 -08:00
|
|
|
promoted_words : float;
|
2001-10-26 15:37:14 -07:00
|
|
|
(** Number of words allocated in the minor heap that
|
|
|
|
survived a minor collection and were moved to the major heap
|
|
|
|
since the program was started. *)
|
2002-02-01 04:23:58 -08:00
|
|
|
|
2001-12-03 14:01:28 -08:00
|
|
|
major_words : float;
|
2001-10-26 15:37:14 -07:00
|
|
|
(** Number of words allocated in the major heap, including
|
|
|
|
the promoted words, since the program was started. *)
|
2002-02-01 04:23:58 -08:00
|
|
|
|
2001-12-03 14:01:28 -08:00
|
|
|
minor_collections : int;
|
2001-10-26 15:37:14 -07:00
|
|
|
(** Number of minor collections since the program was started. *)
|
2002-02-01 04:23:58 -08:00
|
|
|
|
2001-12-03 14:01:28 -08:00
|
|
|
major_collections : int;
|
2002-02-08 11:34:49 -08:00
|
|
|
(** Number of major collection cycles completed since the program
|
|
|
|
was started. *)
|
2002-02-01 04:23:58 -08:00
|
|
|
|
2001-12-03 14:01:28 -08:00
|
|
|
heap_words : int;
|
2001-10-26 15:37:14 -07:00
|
|
|
(** Total size of the major heap, in words. *)
|
2002-02-01 04:23:58 -08:00
|
|
|
|
2001-12-03 14:01:28 -08:00
|
|
|
heap_chunks : int;
|
2002-02-08 11:34:49 -08:00
|
|
|
(** Number of contiguous pieces of memory that make up the major heap. *)
|
2002-02-01 04:23:58 -08:00
|
|
|
|
2001-12-03 14:01:28 -08:00
|
|
|
live_words : int;
|
2002-01-18 07:13:26 -08:00
|
|
|
(** Number of words of live data in the major heap, including the header
|
|
|
|
words. *)
|
2002-02-01 04:23:58 -08:00
|
|
|
|
2001-12-03 14:01:28 -08:00
|
|
|
live_blocks : int;
|
2001-10-26 15:37:14 -07:00
|
|
|
(** Number of live blocks in the major heap. *)
|
2002-02-01 04:23:58 -08:00
|
|
|
|
2001-12-03 14:01:28 -08:00
|
|
|
free_words : int;
|
2001-10-26 15:37:14 -07:00
|
|
|
(** Number of words in the free list. *)
|
2002-02-01 04:23:58 -08:00
|
|
|
|
2001-12-03 14:01:28 -08:00
|
|
|
free_blocks : int;
|
2001-10-26 15:37:14 -07:00
|
|
|
(** Number of blocks in the free list. *)
|
2002-02-01 04:23:58 -08:00
|
|
|
|
2001-12-03 14:01:28 -08:00
|
|
|
largest_free : int;
|
2001-10-26 15:37:14 -07:00
|
|
|
(** Size (in words) of the largest block in the free list. *)
|
2002-02-01 04:23:58 -08:00
|
|
|
|
2001-12-03 14:01:28 -08:00
|
|
|
fragments : int;
|
2001-10-26 15:37:14 -07:00
|
|
|
(** Number of wasted words due to fragmentation. These are
|
|
|
|
1-words free blocks placed between two live blocks. They
|
2002-01-20 09:27:09 -08:00
|
|
|
are not available for allocation. *)
|
2002-02-01 04:23:58 -08:00
|
|
|
|
2001-12-03 14:01:28 -08:00
|
|
|
compactions : int;
|
2002-02-01 04:23:58 -08:00
|
|
|
(** Number of heap compactions since the program was started. *)
|
|
|
|
|
|
|
|
top_heap_words : int;
|
|
|
|
(** Maximum size reached by the major heap, in words. *)
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
2001-12-03 14:01:28 -08:00
|
|
|
(** The memory management counters are returned in a [stat] record.
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2001-12-03 14:01:28 -08:00
|
|
|
The total amount of memory allocated by the program since it was started
|
|
|
|
is (in words) [minor_words + major_words - promoted_words]. Multiply by
|
|
|
|
the word size (4 on a 32-bit machine, 8 on a 64-bit machine) to get
|
|
|
|
the number of bytes.
|
|
|
|
*)
|
|
|
|
|
|
|
|
type control =
|
|
|
|
{ mutable minor_heap_size : int;
|
2001-10-26 15:37:14 -07:00
|
|
|
(** The size (in words) of the minor heap. Changing
|
|
|
|
this parameter will trigger a minor collection. Default: 32k. *)
|
|
|
|
|
2001-12-03 14:01:28 -08:00
|
|
|
mutable major_heap_increment : int;
|
2001-10-26 15:37:14 -07:00
|
|
|
(** The minimum number of words to add to the
|
2008-02-29 04:56:15 -08:00
|
|
|
major heap when increasing it. Default: 124k. *)
|
2001-10-26 15:37:14 -07:00
|
|
|
|
2001-12-03 14:01:28 -08:00
|
|
|
mutable space_overhead : int;
|
2001-10-26 15:37:14 -07:00
|
|
|
(** The major GC speed is computed from this parameter.
|
|
|
|
This is the memory that will be "wasted" because the GC does not
|
|
|
|
immediatly collect unreachable blocks. It is expressed as a
|
|
|
|
percentage of the memory used for live data.
|
|
|
|
The GC will work more (use more CPU time and collect
|
|
|
|
blocks more eagerly) if [space_overhead] is smaller.
|
2002-08-01 07:14:10 -07:00
|
|
|
Default: 80. *)
|
2001-10-26 15:37:14 -07:00
|
|
|
|
2001-12-03 14:01:28 -08:00
|
|
|
mutable verbose : int;
|
2001-10-26 15:37:14 -07:00
|
|
|
(** This value controls the GC messages on standard error output.
|
|
|
|
It is a sum of some of the following flags, to print messages
|
|
|
|
on the corresponding events:
|
2002-05-28 09:57:31 -07:00
|
|
|
- [0x001] Start of major GC cycle.
|
|
|
|
- [0x002] Minor collection and major GC slice.
|
|
|
|
- [0x004] Growing and shrinking of the heap.
|
|
|
|
- [0x008] Resizing of stacks and memory manager tables.
|
|
|
|
- [0x010] Heap compaction.
|
|
|
|
- [0x020] Change of GC parameters.
|
|
|
|
- [0x040] Computation of major GC slice size.
|
|
|
|
- [0x080] Calling of finalisation functions.
|
2001-10-26 15:37:14 -07:00
|
|
|
- [0x100] Bytecode executable search at start-up.
|
2002-05-28 09:57:31 -07:00
|
|
|
- [0x200] Computation of compaction triggering condition.
|
2001-10-26 15:37:14 -07:00
|
|
|
Default: 0. *)
|
|
|
|
|
2001-12-03 14:01:28 -08:00
|
|
|
mutable max_overhead : int;
|
2001-10-26 15:37:14 -07:00
|
|
|
(** Heap compaction is triggered when the estimated amount
|
2002-05-28 09:57:31 -07:00
|
|
|
of "wasted" memory is more than [max_overhead] percent of the
|
|
|
|
amount of live data. If [max_overhead] is set to 0, heap
|
2001-10-26 15:37:14 -07:00
|
|
|
compaction is triggered at the end of each major GC cycle
|
|
|
|
(this setting is intended for testing purposes only).
|
|
|
|
If [max_overhead >= 1000000], compaction is never triggered.
|
2002-05-28 09:57:31 -07:00
|
|
|
Default: 500. *)
|
2001-10-26 15:37:14 -07:00
|
|
|
|
2001-12-03 14:01:28 -08:00
|
|
|
mutable stack_limit : int;
|
2001-10-26 15:37:14 -07:00
|
|
|
(** The maximum size of the stack (in words). This is only
|
|
|
|
relevant to the byte-code runtime, as the native code runtime
|
2008-02-29 06:21:22 -08:00
|
|
|
uses the operating system's stack. Default: 256k. *)
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
2004-08-17 05:32:02 -07:00
|
|
|
(** The GC parameters are given as a [control] record. Note that
|
|
|
|
these parameters can also be initialised by setting the
|
|
|
|
OCAMLRUNPARAM environment variable. See the documentation of
|
|
|
|
ocamlrun. *)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
external stat : unit -> stat = "caml_gc_stat"
|
2001-10-26 15:37:14 -07:00
|
|
|
(** Return the current values of the memory management counters in a
|
2004-01-30 06:29:04 -08:00
|
|
|
[stat] record. This function examines every heap block to get the
|
|
|
|
statistics. *)
|
|
|
|
|
|
|
|
external quick_stat : unit -> stat = "caml_gc_quick_stat"
|
|
|
|
(** Same as [stat] except that [live_words], [live_blocks], [free_words],
|
|
|
|
[free_blocks], [largest_free], and [fragments] are set to 0. This
|
|
|
|
function is much faster than [stat] because it does not need to go
|
|
|
|
through the heap. *)
|
2001-10-26 15:37:14 -07:00
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
external counters : unit -> float * float * float = "caml_gc_counters"
|
2004-01-30 06:29:04 -08:00
|
|
|
(** Return [(minor_words, promoted_words, major_words)]. This function
|
|
|
|
is as fast at [quick_stat]. *)
|
2001-10-26 15:37:14 -07:00
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
external get : unit -> control = "caml_gc_get"
|
2002-02-08 11:34:49 -08:00
|
|
|
(** Return the current values of the GC parameters in a [control] record. *)
|
2001-10-26 15:37:14 -07:00
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
external set : control -> unit = "caml_gc_set"
|
2002-02-08 11:34:49 -08:00
|
|
|
(** [set r] changes the GC parameters according to the [control] record [r].
|
|
|
|
The normal usage is: [Gc.set { (Gc.get()) with Gc.verbose = 0x00d }] *)
|
2001-10-26 15:37:14 -07:00
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
external minor : unit -> unit = "caml_gc_minor"
|
2001-12-03 14:01:28 -08:00
|
|
|
(** Trigger a minor collection. *)
|
2001-10-26 15:37:14 -07:00
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
external major_slice : int -> int = "caml_gc_major_slice";;
|
2002-02-05 09:11:33 -08:00
|
|
|
(** Do a minor collection and a slice of major collection. The argument
|
|
|
|
is the size of the slice, 0 to use the automatically-computed
|
|
|
|
slice size. In all cases, the result is the computed slice size. *)
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
external major : unit -> unit = "caml_gc_major"
|
2002-02-05 09:11:33 -08:00
|
|
|
(** Do a minor collection and finish the current major collection cycle. *)
|
2001-10-26 15:37:14 -07:00
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
external full_major : unit -> unit = "caml_gc_full_major"
|
2002-02-05 09:11:33 -08:00
|
|
|
(** Do a minor collection, finish the current major collection cycle,
|
|
|
|
and perform a complete new cycle. This will collect all currently
|
|
|
|
unreachable blocks. *)
|
2001-10-26 15:37:14 -07:00
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
external compact : unit -> unit = "caml_gc_compaction"
|
2001-10-26 15:37:14 -07:00
|
|
|
(** Perform a full major collection and compact the heap. Note that heap
|
|
|
|
compaction is a lengthy operation. *)
|
1999-11-29 12:02:33 -08:00
|
|
|
|
2001-12-03 14:01:28 -08:00
|
|
|
val print_stat : out_channel -> unit
|
2001-10-26 15:37:14 -07:00
|
|
|
(** Print the current values of the memory management counters (in
|
|
|
|
human-readable form) into the channel argument. *)
|
1999-11-29 12:02:33 -08:00
|
|
|
|
2001-12-03 14:01:28 -08:00
|
|
|
val allocated_bytes : unit -> float
|
2001-10-26 15:37:14 -07:00
|
|
|
(** Return the total number of bytes allocated since the program was
|
|
|
|
started. It is returned as a [float] to avoid overflow problems
|
|
|
|
with [int] on 32-bit machines. *)
|
2000-01-07 08:51:58 -08:00
|
|
|
|
2001-12-03 14:01:28 -08:00
|
|
|
val finalise : ('a -> unit) -> 'a -> unit
|
2002-02-08 11:34:49 -08:00
|
|
|
(** [finalise f v] registers [f] as a finalisation function for [v].
|
2001-10-26 15:37:14 -07:00
|
|
|
[v] must be heap-allocated. [f] will be called with [v] as
|
|
|
|
argument at some point between the first time [v] becomes unreachable
|
|
|
|
and the time [v] is collected by the GC. Several functions can
|
|
|
|
be registered for the same value, or even several instances of the
|
|
|
|
same function. Each instance will be called once (or never,
|
2002-02-08 11:34:49 -08:00
|
|
|
if the program terminates before [v] becomes unreachable).
|
2001-10-26 15:37:14 -07:00
|
|
|
|
2004-06-14 06:27:42 -07:00
|
|
|
The GC will call the finalisation functions in the order of
|
|
|
|
deallocation. When several values become unreachable at the
|
|
|
|
same time (i.e. during the same GC cycle), the finalisation
|
|
|
|
functions will be called in the reverse order of the corresponding
|
|
|
|
calls to [finalise]. If [finalise] is called in the same order
|
|
|
|
as the values are allocated, that means each value is finalised
|
|
|
|
before the values it depends upon. Of course, this becomes
|
|
|
|
false if additional dependencies are introduced by assignments.
|
2001-10-26 15:37:14 -07:00
|
|
|
|
|
|
|
Anything reachable from the closure of finalisation functions
|
2002-02-08 11:34:49 -08:00
|
|
|
is considered reachable, so the following code will not work
|
|
|
|
as expected:
|
2001-10-26 15:37:14 -07:00
|
|
|
- [ let v = ... in Gc.finalise (fun x -> ...) v ]
|
|
|
|
|
|
|
|
Instead you should write:
|
|
|
|
- [ let f = fun x -> ... ;; let v = ... in Gc.finalise f v ]
|
2005-10-25 11:34:07 -07:00
|
|
|
|
2001-02-05 06:59:24 -08:00
|
|
|
|
2001-10-26 15:37:14 -07:00
|
|
|
The [f] function can use all features of O'Caml, including
|
2002-02-08 11:34:49 -08:00
|
|
|
assignments that make the value reachable again. It can also
|
|
|
|
loop forever (in this case, the other
|
2001-10-26 15:37:14 -07:00
|
|
|
finalisation functions will be called during the execution of f).
|
2002-02-08 11:34:49 -08:00
|
|
|
It can call [finalise] on [v] or other values to register other
|
2001-10-26 15:37:14 -07:00
|
|
|
functions or even itself. It can raise an exception; in this case
|
|
|
|
the exception will interrupt whatever the program was doing when
|
|
|
|
the function was called.
|
|
|
|
|
2005-10-25 11:34:07 -07:00
|
|
|
|
2002-02-08 11:34:49 -08:00
|
|
|
[finalise] will raise [Invalid_argument] if [v] is not
|
2001-10-26 15:37:14 -07:00
|
|
|
heap-allocated. Some examples of values that are not
|
|
|
|
heap-allocated are integers, constant constructors, booleans,
|
|
|
|
the empty array, the empty list, the unit value. The exact list
|
|
|
|
of what is heap-allocated or not is implementation-dependent.
|
|
|
|
Some constant values can be heap-allocated but never deallocated
|
|
|
|
during the lifetime of the program, for example a list of integer
|
|
|
|
constants; this is also implementation-dependent.
|
2002-02-08 11:34:49 -08:00
|
|
|
You should also be aware that compiler optimisations may duplicate
|
2002-01-20 09:27:09 -08:00
|
|
|
some immutable values, for example floating-point numbers when
|
2001-10-26 15:37:14 -07:00
|
|
|
stored into arrays, so they can be finalised and collected while
|
|
|
|
another copy is still in use by the program.
|
|
|
|
|
2005-10-25 11:34:07 -07:00
|
|
|
|
2002-02-08 11:34:49 -08:00
|
|
|
The results of calling {!String.make}, {!String.create},
|
|
|
|
{!Array.make}, and {!Pervasives.ref} are guaranteed to be
|
2004-06-14 06:27:42 -07:00
|
|
|
heap-allocated and non-constant except when the length argument is [0].
|
2001-10-26 15:37:14 -07:00
|
|
|
*)
|
2001-02-05 06:59:24 -08:00
|
|
|
|
2004-06-14 06:27:42 -07:00
|
|
|
val finalise_release : unit -> unit;;
|
|
|
|
(** A finalisation function may call [finalise_release] to tell the
|
|
|
|
GC that it can launch the next finalisation function without waiting
|
|
|
|
for the current one to return. *)
|
|
|
|
|
2001-12-03 14:01:28 -08:00
|
|
|
type alarm
|
2001-10-26 15:37:14 -07:00
|
|
|
(** An alarm is a piece of data that calls a user function at the end of
|
|
|
|
each major GC cycle. The following functions are provided to create
|
|
|
|
and delete alarms. *)
|
2001-02-05 06:59:24 -08:00
|
|
|
|
2001-12-03 14:01:28 -08:00
|
|
|
val create_alarm : (unit -> unit) -> alarm
|
2002-02-08 11:34:49 -08:00
|
|
|
(** [create_alarm f] will arrange for [f] to be called at the end of each
|
|
|
|
major GC cycle, starting with the current cycle or the next one.
|
|
|
|
A value of type [alarm] is returned that you can
|
|
|
|
use to call [delete_alarm]. *)
|
2001-02-05 06:59:24 -08:00
|
|
|
|
2001-12-03 14:01:28 -08:00
|
|
|
val delete_alarm : alarm -> unit
|
2001-10-26 15:37:14 -07:00
|
|
|
(** [delete_alarm a] will stop the calls to the function associated
|
|
|
|
to [a]. Calling [delete_alarm a] again has no effect. *)
|