From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 4A24CBB81 for ; Sun, 2 Jan 2005 00:59:37 +0100 (CET) Received: from kraid.nerim.net (smtp-106-saturday.nerim.net [62.4.16.106]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j01NxbeT016431 for ; Sun, 2 Jan 2005 00:59:37 +0100 Received: from localhost (karryall.dnsalias.org [62.4.18.180]) by kraid.nerim.net (Postfix) with ESMTP id C91C141F8F; Sun, 2 Jan 2005 00:59:34 +0100 (CET) Date: Sun, 02 Jan 2005 00:59:35 +0100 (CET) Message-Id: <20050102.005935.74752788.oandrieu@nerim.net> To: jonathan.roewen@gmail.com Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Garbage Collecting From: Olivier Andrieu In-Reply-To: References: X-Mailer: Mew version 4.0.64 on Emacs 21.3 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Miltered: at nez-perce with ID 41D73969.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 garbage:01 andrieu:01 andrieu:01 ijm:01 ocaml:01 printf:01 fib:01 fib:01 printf:01 minor:01 computes:01 allocates:01 runtime:01 ocaml:01 X-Spam-Checker-Version: SpamAssassin 3.0.0 (2004-09-13) on yquem.inria.fr X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.0 X-Spam-Level: > Jonathan Roewen [Sun, 2 Jan 2005]: > > Hi, > > We're trying to figure out why memory doesn't get collected in our > OS by the OCaml GC, and seems like it doesn't want to reclaim any > memory. We also suspect that Gc.allocated_bytes has a memory leak. > > Here is our test case: > (* File mod.ml -- some ``useful'' Caml functions *) > open Printf > > let fib (n:int) =1 + n > > ;; > > let mem = ref 0;; > let do_stuff () = > (* making this loop more increases mem usage *) > for i=1 to 900000 do > fib i; > int_of_float (Gc.allocated_bytes()); (* A *) > done; > mem := int_of_float (Gc.allocated_bytes()) > ;; > > let do_results() = > printf "allocated %d\n" !mem; > Gc.full_major(); (* B1 *) > Gc.compact(); > Gc.major(); > Gc.minor(); (*B2 *) > printf "allocated %d\n" (int_of_float (Gc.allocated_bytes())) > ;; > do_stuff ();; > do_results ();; > > My results: > Test 1) > allocated 57600272 > allocated 57600788 > Test 2) > allocated 272 > allocated 956 > Test 3) > allocated 272 > allocated 744 > > Where test 1 is using the above code; test 2 is with line A commented > out; and test 3 is with both line A and lines B1-B2 commented out. > > As you can see from our tests, that invoking the Gc increases memory, > and Gc.allocated_bytes appears to gobble it up like candy. Well, what do you expect ? Gc.allocated_bytes is a pretty regular-looking function : it computes stuff, using a couple of intermediate values, so it allocates a couple of bytes to store these. Now, if you stick this a loop, number_of_iterations times couple_of_bytes bytes get allocated. This has nothing to do with reclaiming memory actually, Gc.allocated_bytes simply keeps track of how many bytes were requested to the runtime; all those bytes are not "live". > Would appreciate someone shedding some light on this topic, as > writing our OS in OCaml is going to have some major issues if it > won't reclaim memory. I wouldn't worry. There is some anecdotal evidence that the OCaml GC is not a fraud and that it _does_ reclaim memory. -- Olivier