Version française
Home     About     Download     Resources     Contact us    
Browse thread
Tracking memory usage: GC output not same order as unix top command
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Hugo Ferreira <hmf@i...>
Subject: Tracking memory usage: GC output not same order as unix top command
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