caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jim Pryor <lists+caml@jimpryor.net>
To: caml users <caml-list@inria.fr>
Subject: Re: [Caml-list] memory leak in toplevel? or, how to implement sizeof
Date: Fri, 7 Jan 2011 09:14:26 -0500	[thread overview]
Message-ID: <20110107141426.GF12229@vaio.hsd1.pa.comcast.net> (raw)
In-Reply-To: <1294326332.17561.3.camel@nyc-qws-062.delacy.com> <7577E373-EB25-4A7E-90C1-30491F27B4F9@inria.fr>

On Thu, Jan 06, 2011 at 10:05:32AM -0500, Pascal Zimmer wrote:
> Because the overhead parameter can vary based on which version of OCaml
> you are running, what the Gc module does, etc, I usually implement this
> kind of measurements in this way:
> 
> # let sizeof maker arg =
>     let first = Gc.((stat()).minor_words) in
>     for i = 1 to 1000000 do
>       ignore (maker arg)
>     done;
>     let second = Gc.((stat()).minor_words) in
>     (second -. first) /. 1e6;;
> 
> # sizeof (fun x -> [x]) 1;;
> - : float = 3.000022
> 
> The overhead gets amortized enough that is becomes negligible (you only
> have to rely on the fact that the loop does not do any allocation).

Thanks, that's a nice and sensible shortcut.


On Fri, Jan 07, 2011 at 01:25:42PM +0100, Damien Doligez wrote:
> The overhead is exactly the heap space used by the value returned by
> Gc.stat.  In your case it is 23 words, not 22.

Yes, that's right, the Gc.stat on my machine uses 23 words of heap, and of course we should let OCaml calculate the size for us, because as you say it will
differ on different architectures.

> 
> >    # sizeof (fun x -> x) 10;;
> >    - : int * int = (1, 10)
> > 
> > That is, one word for the int.
> 
> The int is not allocated in the heap, so the result should be 0.

There's a design choice here: we have to decide whether sizeof counts only the allocated heap space or also the pointer to it. Initially I also found it more natural to count only the allocated heap space, as you're proposing. But then when testing on embedded blocks I talked myself into thinking the other choice worked more smoothly. Looking at it again, though, I think I had just confused myself and you're right, counting only the heap space is best.

Thanks for the feedback, both of you.

-- 
Jim Pryor
profjim@jimpryor.net

  reply	other threads:[~2011-01-07 14:12 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-01-06 13:05 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 [this message]
2011-01-07 14:22           ` dmitry grebeniuk
2011-01-06 15:05     ` Pascal Zimmer

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=20110107141426.GF12229@vaio.hsd1.pa.comcast.net \
    --to=lists+caml@jimpryor.net \
    --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).