Skip to content

Memory leak when using threads #11289

@toots

Description

@toots

This bug has been tested with OCaml 4.13.0. I am in the process of testing it with 5.0.0 (on a m1 mac)

The following program:

let rec f () =
  let th = Thread.create (fun () ->
    let b = Bytes.make (10*4096) ' ' in
    Printf.printf "New Thread with %d bytes\n%!" (Bytes.length b);
    Unix.sleepf 0.1) ()
  in
  Thread.join th;
  f ()

let () = f ()

Seems to leave the memory allocated in the thread for too long. In my test, the program quickly rose to ~60Mo of RAM within minutes:
mem_usage

The same program with a call to full_major does not exhibit the issue:

let rec f () =
  let th = Thread.create (fun () ->
    let b = Bytes.make (10*4096) ' ' in
    Printf.printf "New Thread with %d bytes\n%!" (Bytes.length b);
    Unix.sleepf 0.1) ()
  in
  Thread.join th;
  Gc.full_major();
  f ()

let () = f ()

mem_usage_full_major

This seems to indicate that the GC is underestimating the amount of memory waiting to be cleaned up from the thread's allocations.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions