Reputation: 7381
suppose I have a function
let rec f x =
let a = (* build a huge list, possibly used f on smaller input again here *) in
let b = (* build a huge list, possibly used f on smaller input again too *) in
List.append a b
My question is that after calling f
is a
and b
freed? My program is eating up my memory and I am suspecting whether this is the cause.
Upvotes: 1
Views: 72
Reputation: 35210
yes, it will be freed. OCaml garbage collection usually doesn't leak, and if value is not reachable it will be collected. If it is possible to reuse memory it will be reused.
Note, if you're using really big data structures, then maybe list is not a good choice, especially concatenating lists with concat
is very inefficient.
Upvotes: 1
Reputation: 66793
After f
returns its value, the variables a
and b
can't be accessed. So the value of a
will be garbage collected eventually. For the most obvious implementation of List.append
, the value of b
forms part of the result of the function. So it won't be garbage collected until this result is no longer accessible.
If you have a memory leak you need to look in places that are accessible for the whole computation. One example would be a large global table.
Upvotes: 2