caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Tracking memory usage: GC output not same order as unix top command
@ 2010-08-30  9:43 Hugo Ferreira
  2010-08-30 10:02 ` [Caml-list] " Richard Jones
  2010-08-30 14:12 ` Pietro Abate
  0 siblings, 2 replies; 7+ messages in thread
From: Hugo Ferreira @ 2010-08-30  9:43 UTC (permalink / raw)
  To: caml-list List

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






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

end of thread, other threads:[~2010-08-30 14:17 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-08-30  9:43 Tracking memory usage: GC output not same order as unix top command Hugo Ferreira
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

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