caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re: Strange memory leak
@ 2001-02-09 23:41 Hao-yang Wang
  0 siblings, 0 replies; 2+ messages in thread
From: Hao-yang Wang @ 2001-02-09 23:41 UTC (permalink / raw)
  To: caml-list

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



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Strange memory leak
@ 2001-02-09 21:08 Hao-yang Wang
  0 siblings, 0 replies; 2+ messages in thread
From: Hao-yang Wang @ 2001-02-09 21:08 UTC (permalink / raw)
  To: caml-list

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



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2001-02-10 21:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-02-09 23:41 Strange memory leak Hao-yang Wang
  -- strict thread matches above, loose matches on Subject: below --
2001-02-09 21:08 Hao-yang Wang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).