Re: OCaml Memory Management Best Practices

"Gerd Stolpmann [email protected] [ocaml_beginners]" <[email protected]>
Newsgroups gmane.comp.lang.ocaml.beginners
Message-ID <[email protected]>
Am Samstag, den 27.06.2015, 17:06 +0000 schrieb Kenneth Miller
[email protected] [ocaml_beginners]:
>   
> For operations like List.map, I would imagine that the what is
> actually occurring, is a new list of equal size is being constructed
> where an operation on the old map is happening for every element. I
> would like to know what the real implications are for the garbage
> collector, and how I can minimize the performance and memory impact.

You cannot do anything, and you need not.

>  Things like List.append calculate a new list by constructing a copy.
> That's incredibly inefficient to me, both in the sense that each
> element afterward (time) needs to be copied, and in that more memory
> (space) is needed.
> 
> 
> In addition, is there a set of advice points that can be given to make
> sure that the garbage collector will appropriately see what it needs
> to in order to free items? In java, everything is reference counted,

No, this is not true. Java, like OCaml, uses at least a mark-and-sweep
collector (of course, this depends on the JVM used, and there is
normally even a generational collector). Reference-counting is a bad
choice for any higher-level language because it cannot detect cyclic
pointer chains, and these remain uncollected.

>  so if you want an object to be freed and you're maintaining it in a
> tree the lack of use of that object is better treated by going to the
> tree and updating the node that references it to mark it null. In this
> way, the garbage collector can collect the item that you were pointing
> to.

There is no need to do this (breaking cycles) in Java, and no need to do
this in OCaml.

> OCaml is a different animal though, and it doesn't use reference
> counting. There doesn't seem to me to be any simple go-to practice in
> order to ensure that the GC will in fact collect an item that I know
> at the moment because it's operation is so different.

The OCaml collector is fully automatic like the one in Java. Cycles are
collected when the whole cycle becomes unreachable.

Gerd




[Non-text portions of this message have been removed]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.