caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Hugo Ferreira <hmf@inescporto.pt>
To: caml-list List <caml-list@yquem.inria.fr>
Subject: Tracking memory usage: GC output not same order as unix top command
Date: Mon, 30 Aug 2010 10:43:42 +0100	[thread overview]
Message-ID: <4C7B7D4E.4020500@inescporto.pt> (raw)

Hello,

I have a problem trying to identify where my algorithm consumes
so much memory. I have created a set of functions (see end of message)
that uses the GC module to obtain the memory used in the heap and print
that out to determine where memory is being consumed.

I use the "Gc.quick_stat ()" and measure memory consumption via
the "stat.Gc.heap_words * 8" value (64 bit machine). The output
shows memory usage below the 100M mark, however the unix command
"top" shows usage in the order of Gigabytes (at least 4.8). This
memory consumption grows gradually.

I would like to know:
1) Can I use "Gc.quick_stat" and "stat.Gc.heap_words" to measure
    total memory consumption?
2) Is my use of the GC module as shown below ok?
   For example:
   let h1 = Gc_mem.get_quick_stat () in
   let _ = do_something....
   let h2 = Gc_mem.get_quick_stat () in
   let dh = Gc_mem.delta_heap_used h1 h2 in
   let h1 = Gc_mem.string_of_stat_heap_used h1 in
   let h2 = Gc_mem.string_of_stat_heap_used h2 in
   let dh = Gc_mem.string_of_heap_used dh in
   Printf.printf "GC: get_maximal' (0) heap_before(%s) ; heap_after(%s) 
; heap_delta(%s)\n%!" h1 h2 dh


TIA,
Hugo F.


type multiplier =
   | U of int
   | K of float
   | M of float
   | G of float

let convert_units heap_words =
   let heap_words_k = (float_of_int heap_words) /. 1024.0 in
   if heap_words_k <= 1.0
   then U(heap_words)
   else let heap_words_m = heap_words_k /. 1024.0 in
        if heap_words_m <= 1.0
        then K(heap_words_k)
        else let heap_words_g = heap_words_m /. 1024.0 in
             if heap_words_g <= 1.0
             then M(heap_words_m)
             else G(heap_words_g)


let get_quick_stat unit = Gc.quick_stat ()

let delta_heap_used stat_1 stat_2 =
   let heap_words_1 = stat_1.Gc.heap_words * 8 in (* 64 bit machine *)
   let heap_words_2 = stat_2.Gc.heap_words * 8 in (* 64 bit machine *)
   let heap_words = abs ( heap_words_2 - heap_words_1 ) in
   heap_words

let string_of_heap_used heap_words =
   let value = convert_units heap_words in
   match value with
   | U(i) -> Printf.sprintf "%d" i
   | K(i) -> Printf.sprintf "%.3fK" i
   | M(i) -> Printf.sprintf "%.3fM" i
   | G(i) -> Printf.sprintf "%.3fG" i

let string_of_stat_heap_used stat =
   let heap_used = stat.Gc.heap_words * 8 in (* 64 bit machine *)
   string_of_heap_used heap_used






             reply	other threads:[~2010-08-30  9:43 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-08-30  9:43 Hugo Ferreira [this message]
2010-08-30 10:02 ` [Caml-list] " Richard Jones
2010-08-30 10:06   ` Richard Jones
2010-08-30 10:33     ` Hugo Ferreira
2010-08-30 13:53       ` Richard Jones
2010-08-30 14:12 ` Pietro Abate
2010-08-30 14:17   ` Hugo Ferreira

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=4C7B7D4E.4020500@inescporto.pt \
    --to=hmf@inescporto.pt \
    --cc=caml-list@yquem.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).