Version française
Home     About     Download     Resources     Contact us    
Browse thread
Strange memory leak
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Hao-yang Wang <hao-yang_wang@f...>
Subject: Strange memory leak
I wrote this bunch of code:
-----------------------------------
type 'a zlist = Znull | Zcons of 'a * 'a zzlist
and 'a zzlist = 'a zlist Lazy.t

let rec zfilter f zz =
    let z = Lazy.force zz in
    match z with
        Znull ->
            Znull
      | Zcons(head, ztail) ->
            if f head then
                z
            else
                zfilter f ztail

let rec build_list n =
    if n > 0 then
        Zcons(n, lazy (build_list (n - 1)))
    else
        Znull

let rec loopy f zz =
    match zfilter f zz with
        Znull ->
            ()
      | Zcons(head, ztail) ->
            loopy f ztail
-----------------------------------
>From the byte-code toplevel, the following expression evaluates fine

  loopy (fun x -> true) (lazy (build_list 1000000));;

while the following expression produces "Fatal error: out of memory."

  loopy (fun x -> false) (lazy (build_list 1000000));;

Why? In particular, the function zfilter looks tail-recursive to me.

I am using ocaml-3.00, with the Macintosh version of top-level.

Thanks,
Hao-yang Wang