caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Adrien <camaradetux@gmail.com>
To: Damien Doligez <damien.doligez@inria.fr>
Cc: Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] Understanding usage by the runtime
Date: Wed, 4 Jan 2012 19:48:38 +0100	[thread overview]
Message-ID: <CAP5QFJk1GSDSizVwyQw+-aX_eWC71vPEBpXUepY4Vv9iYO6=Jw@mail.gmail.com> (raw)
In-Reply-To: <1453C993-CFD2-41AD-9611-A90398E560EC@inria.fr>

On 04/01/2012, Damien Doligez <damien.doligez@inria.fr> wrote:
> On 2012-01-01, at 13:44, Richard W.M. Jones wrote:
>
>> Is compaction disabled?  lablgtk disables it unconditionally by
>> setting the global Gc max_overhead (see also the Gc documentation):
>>
>>  src/gtkMain.ml:
>>    let () = Gc.set {(Gc.get()) with Gc.max_overhead = 1000000}
>
> Anyone who disables compaction should seriously consider switching
> to the first-fit allocation policy:
>
>   let () = Gc.set {(Gc.get ()) with Gc.allocation_policy = 1}
>
> This may slow down allocations a bit, but the theory tells us that
> it completely prevents unbounded fragmentation of the OCaml heap.

I've often wondered what I should do when using lablgtk. It's a pretty
annoying issue and, as far as I understand, OCaml will only return the
memory to the OS upon compactions.

There is however something to do. Quoting lablgtk's README:
> IMPORTANT: Some Gtk data structures are allocated in the Caml heap,
> and their use in signals (Gtk functions internally cally callbacks)
> relies on their address being stable during a function call. For
> this reason automatic compation is disabled in GtkMain. If you need
> it, you may use compaction through Gc.compact where it is safe
> (timeouts, other threads...), but do not enable automatic compaction.

I've never really understood why it worked: I'm surprised the GC would
update addresses stored in the C side of GTK.

If you want to use timeouts, the following should work:
  Glib.Timeout.add ~ms:0 ~callback:(fun () -> Gc.compact (); false)

I guess that Glib.Idle.add would work too.

That guarantees nothing about the time the compaction will run however
and in practice, adding a timeout or an idle and starting a long-running
and uninterruptible computation right after will severely delay the
compaction.

I haven't had the time to try it but it should be possible to pump
glib's event loop by hand in order to trigger the compaction. Another
possibility would be to spawn a thread and use a mutex to wait until the
compaction is done. And in case you're using Lwt, well, I don't know but
I'd expect the callback to be callable whenever threads can be switched.

Maybe that if it were possible to have a callback called each time the
runtime would like to do a compaction, this could be automated.

Regards,
Adrien Nader

  reply	other threads:[~2012-01-04 18:48 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-12-30 23:45 orbitz
2011-12-31  9:11 ` David Baelde
2011-12-31 15:33   ` orbitz
2012-01-01 12:44     ` Richard W.M. Jones
2012-01-04 18:03       ` Damien Doligez
2012-01-04 18:48         ` Adrien [this message]
2012-01-04 19:37           ` John Carr
2012-01-07  5:43       ` orbitz
2012-01-08 18:45         ` Richard W.M. Jones
2012-01-08 19:00           ` Richard W.M. Jones
2012-01-08 22:33             ` Török Edwin
2012-01-09 14:31               ` Richard W.M. Jones
2012-01-09 21:07                 ` Richard W.M. Jones
2012-01-08 22:50           ` orbitz
2012-01-08 23:02             ` Richard W.M. Jones
2012-01-08 23:26               ` orbitz

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='CAP5QFJk1GSDSizVwyQw+-aX_eWC71vPEBpXUepY4Vv9iYO6=Jw@mail.gmail.com' \
    --to=camaradetux@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=damien.doligez@inria.fr \
    /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).