Browse thread
Re: Strange memory leak
- Hao-yang Wang
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Hao-yang Wang <hywang@p...> |
| Subject: | Re: Strange memory leak |
After I posted the last message, I think I have found out what's wrong. In the function 'loopy', when it calls into 'zfilter f zz', the variable zz keeps holding the head of the lazy list, thus prevents GC from reclaiming the memory. Still, I don't know how to rewrite loopy to plug this leak. Any help? Thanks, Hao-yang Wang >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