Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Jim Pryor <lists+caml@jimpryor.net>
To: caml-list@inria.fr
Subject: Re: [Caml-list] memory leak in toplevel? or, how to implement sizeof
Date: Thu, 6 Jan 2011 09:59:24 -0500	[thread overview]
Message-ID: <20110106145924.GE12229@vaio.hsd1.pa.comcast.net> (raw)
In-Reply-To: <20110106144406.GD12229@vaio.hsd1.pa.comcast.net>

On Thu, Jan 06, 2011 at 09:44:06AM -0500, Jim Pryor wrote:
> Thanks David. So does this look like a reasonable implementation of
> sizeof:
> 
>     # let sizeof maker arg =
>         let first = Gc.((stat()).minor_words) in
>         let res = maker arg in
>         let second = Gc.((stat()).minor_words) in
>         int_of_float (second -. first) - 23 ( *overhead *), res;;
> 
>     # sizeof (fun x -> [x]) 1;;
>     - : int * int list = (3, [1])
> 
>     # sizeof (fun x -> Some x) 1;;
>     - : int * int option = (2, Some 1)
> 
> It does give the right answers. ([1] is a block containing two non-blocks; Some 1 is a block containing one non-block.)

Actually I think the overhead should be 22. Making it 22 gives these
sensible results:

    # sizeof (fun x -> x) 10;;
    - : int * int = (1, 10)

That is, one word for the int.

    # sizeof (fun x -> (x,()) ) 10;;
    - : int * (int * unit) = (4, (10, ()))

One word for the block pointer, one word for the block
header, one word for each of the two elements of the block.

    # sizeof (fun x -> (x,(x,())) ) 10;;
    - : int * (int * (int * unit)) = (7, (10, (10, ())))

Four words as above, one of them pointing to a new block, which contains
a header and two elements.

-- 
Jim Pryor
profjim@jimpryor.net

  reply	other threads:[~2011-01-06 14:57 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 [this message]
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

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=20110106145924.GE12229@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