caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Rob Hoes <Rob.Hoes@citrix.com>
To: Gabriel Scherer <gabriel.scherer@gmail.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 12:48:00 +0000	[thread overview]
Message-ID: <29636324-04D3-483F-B49B-850E59428A95@citrix.com> (raw)
In-Reply-To: <CAPFanBE5Uhv-JeQMWjhpkUvUP5hLtnAG0d+w2jrq7TTYPCYAtA@mail.gmail.com>

Sure: http://caml.inria.fr/mantis/view.php?id=7220

Thanks,
Rob

> On 11 Apr 2016, at 13:01, Gabriel Scherer <gabriel.scherer@gmail.com> wrote:
> 
> 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:50 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
2016-04-11 12:48   ` Rob Hoes [this message]

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=29636324-04D3-483F-B49B-850E59428A95@citrix.com \
    --to=rob.hoes@citrix.com \
    --cc=caml-list@inria.fr \
    --cc=gabriel.scherer@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;
as well as URLs for NNTP newsgroup(s).