caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Damien Doligez <damien.doligez@inria.fr>
To: Thomas Fischbacher <t.fischbacher@soton.ac.uk>
Cc: OCaML Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Weird GC behaviour
Date: Wed, 28 Sep 2011 13:19:11 +0200	[thread overview]
Message-ID: <2F6B5C48-88B0-4717-87FD-BD044B3D36E4@inria.fr> (raw)
In-Reply-To: <4E820844.7050705@soton.ac.uk>

Hi Thomas,

On 2011-09-27, at 19:30, Thomas Fischbacher wrote:

> For Rtag, inlining get_rtag does not make a difference, but for Ftag,
> inlining the get_ftag call breaks things both with the interpreter and
> compiler, but in different ways.

That's the key.

>  if works
>    then (get_ftag "funny",get_rtag "funny")
>    else (Ftag "funny", Rtag (ref "funny"))
>  in

In this expression, you have four interesting subexpressions.  Three of
them involve calling some function with "funny" as argument, and these
functions allocate values that point to the "funny" string.  These values
are heap-allocated and behave as you expect.

The fourth one is (Ftag "funny").  This is a constant expression and its
evaluation does not allocate in the heap.  Since it's constant, it's
allocated once by the compiler, and every time you execute this code
it's the same value that is returned.

In byte-code it's still allocated in the heap, but at load-time, and
a reference to it is hidden in the closure of the enclosing function.
Hence, it never gets deallocated:

> $ ocaml gc_bug.ml
> Removing ref 'funny'
> ht_str entries: 1 ht_ref entries: 0

You don't get the "Removing str 'funny'" message because the value
stays alive throughout program execution.

In native code, it's even more optimized: the value is "allocated" at
compile time as constant data outside the heap.  Hence, you can't even
register a finalizer for it:

> $ ocamlopt -o gc_bug gc_bug.ml
> 
> $ ./gc_bug
> Fatal error: exception Invalid_argument("Gc.finalise")


Only heap data can be finalized.

So you have to be aware of the difference between (get_ftag "funny") and
(Ftag "funny") because they don't have exactly the same semantics: a call
to get_ftag is guaranteed to allocate a new block each time.

-- Damien


  reply	other threads:[~2011-09-28 11:19 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-27 17:30 Thomas Fischbacher
2011-09-28 11:19 ` Damien Doligez [this message]
2011-09-28 11:53   ` Thomas Fischbacher
2011-09-28 12:12     ` Gabriel Scherer
2011-09-28 13:07     ` John Carr
2011-09-28 15:30     ` Damien Doligez
2011-09-28 23:32       ` Philippe Wang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2F6B5C48-88B0-4717-87FD-BD044B3D36E4@inria.fr \
    --to=damien.doligez@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=t.fischbacher@soton.ac.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).