From: Philippe Wang <mail@philippewang.info>
To: Guillaume Yziquel <guillaume.yziquel@gmx.ch>
Cc: ygrek <ygrekheretix@gmail.com>, caml-list@inria.fr
Subject: Re: [Caml-list] A question about GC.
Date: Mon, 13 Jun 2011 15:26:05 +0200 [thread overview]
Message-ID: <BANLkTik+ep7ZoG80-1m8nYAVWv9E7eewEw@mail.gmail.com> (raw)
In-Reply-To: <20110613121936.GA637@localhost>
On Mon, Jun 13, 2011 at 2:19 PM, Guillaume Yziquel
<guillaume.yziquel@gmx.ch> wrote:
> Le Monday 13 Jun 2011 ą 14:34:07 (+0300), ygrek a écrit :
>> On Mon, 13 Jun 2011 12:37:46 +0200
>> Philippe Wang <mail@philippewang.info> wrote:
>>
>> > As far as I know, when OCaml's GC is not working as expected, it's
>> > (almost) always because there are blocks allocated outside OCaml's
>> > heap (a.k.a. custom blocks).
>>
>> Custom blocks are allocated on ocaml heap.
>
> No, not always. Nothing stops you from putting custom blocks outside of
> the ocaml heap. It might even sometimes make sense. Though usually
> you're fine allocating custom blocks on the ocaml heap with a pointer to
> out of heap data.
>
>> > How big are custom blocks? The bigger they are, the worst the behavior
>> > tends to be.
>>
>> Why?
>
> Because the GC relies on the two last arguments of caml_alloc_custom to
> properly evaluate the memory impact of allocating a custom block
> together with the data it refers to. It's easy to get it wrong for large
> data, and thus miscalibrate the GC on these blocks.
>
> However, the fact that grey values seem to accumulate doesn't seem to
> fit with such an simple explanation. But maybe Philippe is thinking
> about something else when talking about "big custom blocks allocated
> outside of the ocaml heap".
Let's consider this simple scenario:
let tmp = new_very_large_data_outside_ocaml_heap () in
let result = some_stuff(tmp) in
result
=> if tmp is not useful anymore, you will probably have to wait a long
time before tmp is fully collected by the GC.
That's because finalize functions are not called very often (as far
as I remember, so tell me if I'm wrong) notably not during a minor
collection, while finalize functions are those which actually free
those large data allocated outside OCaml's heap. So even if a minor
collection (or several) is (are) triggered, memory is not collected
until a major collection.
This should explain why in some scenarios, even if C-interface is
written "perfectly", there is some "kind of memory-leak" (which is not
actual memory leak but just some delay on deallocations, which might
cause the program to fail).
The only way I know to deallocate quicker custom data is to trigger
the major collection more often. The only way to trigger the major
collection more often without calling Gc.something(), is to write the
program differently with a lot of small allocations ("a lot" means
"enough", here). Two ways: allocate dummy data (this is not really
good), allocate useful data (this is better). Allocating more data can
happen when programming with threads! When some computation is done
outside OCaml's heap, it's particularly relevant to use OCaml threads.
But this means at least one thread is computing "normal OCaml".
N.B. Well, maybe I'm not addressing the problem at all. Actually, what
I'm saying here is mostly based on my experiment with mlgmp (an
interface to use GMP in OCaml) about 5 years ago.
--
Philippe Wang
mail@philippewang.info
next prev parent reply other threads:[~2011-06-13 13:26 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-06-13 0:35 Yoonseok Ko
2011-06-13 9:40 ` Guillaume Yziquel
2011-06-13 10:37 ` Philippe Wang
2011-06-13 11:34 ` ygrek
2011-06-13 12:19 ` Guillaume Yziquel
2011-06-13 13:26 ` Philippe Wang [this message]
2011-06-13 12:21 ` Gerd Stolpmann
2011-06-13 12:26 ` Guillaume Yziquel
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=BANLkTik+ep7ZoG80-1m8nYAVWv9E7eewEw@mail.gmail.com \
--to=mail@philippewang.info \
--cc=caml-list@inria.fr \
--cc=guillaume.yziquel@gmx.ch \
--cc=ygrekheretix@gmail.com \
/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