caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] memory leak in toplevel? or, how to implement sizeof
@ 2011-01-06 13:05 Jim Pryor
  2011-01-06 14:20 ` David Allsopp
  0 siblings, 1 reply; 8+ messages in thread
From: Jim Pryor @ 2011-01-06 13:05 UTC (permalink / raw)
  To: caml-list

Starting up the toplevel fresh, with no .ocamlinit file, I see this:

    $ /usr/bin/ocaml
            Objective Caml version 3.12.0

    # Gc.(compact();counters());;                         
    - : float * float * float = (79217., 32932., 82668.)
    # Gc.(compact();counters());;
    - : float * float * float = (93530., 32932., 85816.)
    # Gc.(compact();counters());;
    - : float * float * float = (107843., 32932., 88964.)

Why do both the minor_words and major_words keep climbing? Do these
indicate the number of words _ever_ allocated, and so the overhead of
doing the garbage collection and compacting has pushed them up? Or do
they indicate the number of words _now_ allocated, so the fact that they
continue to climb indicates something is leaking memory?

Omitting the collection, I still see persisting climbs:

    $ /usr/bin/ocaml
            Objective Caml version 3.12.0

    # Gc.(counters());;
    - : float * float * float = (227355., 69894., 150533.)
    # Gc.(counters());;
    - : float * float * float = (240969., 73385., 154024.)
    # Gc.(counters());;
    - : float * float * float = (254583., 73385., 154024.)


Two questions:

1. Is there anything here to be worried about? A memory leak that needs
to be tracked down? (If so, won't in be in the toplevel or some other
piece of the core system? I haven't loaded any other libraries.) Or is
there nothing to worry about?

2. What bit of the Gc should I poll to find out how much memory I just
allocated? I was trying to write a function like this:

    let sizeof maker =
        let baseline = get_baseline_from_gc () in
        let res = maker () in
        let final = get_final_from_gc () in
        (final -. baseline, res);;

This post at the Jane Street blog
<http://ocaml.janestreet.com/?q=node/30> suggests (among other things)
that:

    # (Gc.stat()).Gc.minor_words;;

is the right way to poll the Gc. However:

    # (Gc.stat()).Gc.minor_words;;
    - : float = 226623.
    # (Gc.stat()).Gc.minor_words;;
    - : float = 229632.
    # (Gc.stat()).Gc.minor_words;;
    - : float = 232641.
    # let a = Some 1;;
    val a : int option = Some 1
    # (Gc.stat()).Gc.minor_words;;
    - : float = 242131.
    # (Gc.stat()).Gc.minor_words;;
    - : float = 245140.

says there are 9 words allocated just for each poll of the Gc, and
then 9490 (!) words allocated for the `Some 1`. Something is amiss.


-- 
Jim Pryor
profjim@jimpryor.net

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2011-01-07 14:22 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-01-06 13:05 [Caml-list] memory leak in toplevel? or, how to implement sizeof Jim Pryor
2011-01-06 14:20 ` David Allsopp
2011-01-06 14:44   ` Jim Pryor
2011-01-06 14:59     ` Jim Pryor
2011-01-07 12:25       ` Damien Doligez
2011-01-07 14:14         ` Jim Pryor
2011-01-07 14:22           ` dmitry grebeniuk
2011-01-06 15:05     ` Pascal Zimmer

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).