From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: weis Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id JAA24779 for caml-redistribution; Mon, 29 Dec 1997 09:34:10 +0100 (MET) Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id OAA10358 for ; Sat, 27 Dec 1997 14:37:54 +0100 (MET) Received: from tobago.inria.fr (tobago.inria.fr [128.93.8.21]) by concorde.inria.fr (8.8.7/8.8.5) with ESMTP id OAA28133 for ; Sat, 27 Dec 1997 14:37:54 +0100 (MET) Received: (from doligez@localhost) by tobago.inria.fr (8.6.10/8.6.6) id OAA10772 for caml-list@inria.fr; Sat, 27 Dec 1997 14:37:52 +0100 Date: Sat, 27 Dec 1997 14:37:52 +0100 From: Damien Doligez Message-Id: <199712271337.OAA10772@tobago.inria.fr> To: caml-list@inria.fr Subject: Re: What exactly can be GC'ed? Sender: weis >From: Ching-Tsun Chou ># let x = String.make 3 'a' ;; >val x : string = "aaa" ># let w = Weak.create 1 ;; >val w : '_a Weak.t = ># Weak.set w 0 (Some (x)) ;; >- : unit = () ># Weak.get w 0 ;; >- : string option = Some "aaa" ># let x = "bbb" ;; >val x : string = "bbb" ># Gc.full_major () ;; >- : unit = () ># Weak.get w 0 ;; >- : string option = Some "aaa" >It seems to me that since x has been re-bound to "bbb", its old value >"aaa" is no longer reachable and hence can be GC'ed. But clearly I >was wrong. Where did I go wrong? What exactly can be GC'ed? Caml is a language with static binding. The new definition of x does not replace the old one in any way, except in the table that maps names to slots in the table of globals. When you define the second x, the first one only loses its name. It still exists as a global variable. Assume that you insert "let f () = x;;" anywhere between the two bindings of x. Then the old value is still reachable through f. The compiler does not recycle globals table entries because it would add useless complexity to the system (to determine whether an entry is still in use or not), and it only makes sense under the toplevel system, which is mostly intended for debugging anyway. -- Damien