caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gabriel Scherer <gabriel.scherer@gmail.com>
To: Rob Hoes <Rob.Hoes@citrix.com>
Cc: "caml-list@inria.fr" <caml-list@inria.fr>
Subject: Re: [Caml-list] Memory leak in ocaml runtime with backtraces+threads?
Date: Mon, 11 Apr 2016 08:01:37 -0400	[thread overview]
Message-ID: <CAPFanBE5Uhv-JeQMWjhpkUvUP5hLtnAG0d+w2jrq7TTYPCYAtA@mail.gmail.com> (raw)
In-Reply-To: <1D3284E6-F0BD-49C3-AF8E-F5C49549BBFA@citrix.com>

I did not previously know of that problem, sorry.

Could you submit a bugreport on the Mantis bugtracker?
  http://caml.inria.fr/mantis/


On Mon, Apr 11, 2016 at 7:27 AM, Rob Hoes <Rob.Hoes@citrix.com> wrote:
> 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
>
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs

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

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-11 11:27 Rob Hoes
2016-04-11 12:01 ` Gabriel Scherer [this message]
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=CAPFanBE5Uhv-JeQMWjhpkUvUP5hLtnAG0d+w2jrq7TTYPCYAtA@mail.gmail.com \
    --to=gabriel.scherer@gmail.com \
    --cc=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).