Loading ...
Sorry, an error occurred while loading the content.

Re: "ocaml_beginners"::[] sizeof in OCaml

Expand Messages
  • Nicolas Cannasse
    ... You should use the undocumented Obj module. (sample code, untested) let rec size_of x = let t = Obj.repr x in if Obj.is_int t then 1 (* 4 bytes *) else
    Message 1 of 1 , Aug 2, 2002
    • 0 Attachment
      > I need to find the memory size of any arbitrary OCaml object of any
      > type: int, record, list etc. I am currently using 'Marshal.to_string'
      > to flatten the data structure to a string and then using
      > 'Marshal.total_size' to get the size. Is there a faster way to do this
      > than flattening the data structure?

      You should use the 'undocumented' Obj module.

      (sample code, untested)

      let rec size_of x =
      let t = Obj.repr x in
      if Obj.is_int t then 1 (* 4 bytes *) else Obj.size t + (add_all size_of
      (Obj.field t n))
    Your message has been successfully submitted and would be delivered to recipients shortly.