caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Rob Hoes <Rob.Hoes@citrix.com>
To: "caml-list@inria.fr" <caml-list@inria.fr>
Subject: [Caml-list] Memory leak in ocaml runtime with backtraces+threads?
Date: Mon, 11 Apr 2016 11:27:10 +0000	[thread overview]
Message-ID: <1D3284E6-F0BD-49C3-AF8E-F5C49549BBFA@citrix.com> (raw)

Hello,

I have noticed that an OCaml program with backtrace recording enabled seems to leak memory each time a new thread is created and completes.

For example, the following program, compiled with the `-g` option, will very quickly eat up all your memory:

let _ =
  Printexc.record_backtrace true;
  let rec loop () =
    let t = Thread.create (fun () ->
      try
        raise Not_found
      with Not_found ->
        ()
    ) () in
    Thread.join t;
    loop ()
  in
  loop ()

I’ve tried this on various compilers (4.02.3, 4.01.0 and 3.12.1), on Linux and Mac, native and bytecode, and see the problem on all of them.

Valgrind reports that the allocation of `caml_backtrace_buffer` in the function `caml_stash_backtrace` in asmrun/backtrace_prim.c is the culprit (https://github.com/ocaml/ocaml/blob/trunk/asmrun/backtrace_prim.c#L98):

  if (caml_backtrace_buffer == NULL) {
    Assert(caml_backtrace_pos == 0);
    caml_backtrace_buffer = malloc(BACKTRACE_BUFFER_SIZE
                                   * sizeof(backtrace_slot));
    if (caml_backtrace_buffer == NULL) return;
  }

The `caml_backtrace_buffer` variable is a global variable initialised to NULL in byterun/backtrace.c. However, it seems to be local to a thread (how does it become thread local?), is set to NULL whenever a thread is created (where?), and a new malloc happens when an exception is raised. It is not freed when the thread completes.

Has anyone seen this before, or have suggestions for how to fix it?

Thanks,
Rob


             reply	other threads:[~2016-04-11 11:29 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-11 11:27 Rob Hoes [this message]
2016-04-11 12:01 ` Gabriel Scherer
2016-04-11 12:48   ` Rob Hoes

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=1D3284E6-F0BD-49C3-AF8E-F5C49549BBFA@citrix.com \
    --to=rob.hoes@citrix.com \
    --cc=caml-list@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).