From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id 428BFBC57 for ; Mon, 30 Aug 2010 11:43:49 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ao0BAMcZe0zVhjEYkGdsb2JhbACgVAEBAQEJCQwHEQMfuG2CbYJKBIx9 X-IronPort-AV: E=Sophos;i="4.56,292,1280700000"; d="scan'208";a="56170369" Received: from ihsmtp02voda.lis.interhost.com (HELO ihsmtp02cons.lis.interhost.com) ([213.134.49.24]) by mail3-smtp-sop.national.inria.fr with ESMTP; 30 Aug 2010 11:43:48 +0200 Received: from [172.29.149.150] ([193.136.33.132]) by ihsmtp02cons.lis.interhost.com with Microsoft SMTPSVC(6.0.3790.3959); Mon, 30 Aug 2010 10:43:47 +0100 Message-ID: <4C7B7D4E.4020500@inescporto.pt> Date: Mon, 30 Aug 2010 10:43:42 +0100 From: Hugo Ferreira User-Agent: Thunderbird 2.0.0.24 (X11/20100317) MIME-Version: 1.0 To: caml-list List Subject: Tracking memory usage: GC output not same order as unix top command Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit X-OriginalArrivalTime: 30 Aug 2010 09:43:47.0451 (UTC) FILETIME=[D7F89CB0:01CB4827] X-Spam: no; 0.00; printf:01 printf:01 sprintf:01 sprintf:01 1.0:98 1.0:98 unix:01 unix:01 heap:01 heap:01 functions:01 int:01 int:01 algorithm:01 match:02 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