Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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: 2001-02-10 (21:27)
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 ->
      | Zcons(head, ztail) ->
            if f head then
                zfilter f ztail

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

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.

Hao-yang Wang