Won't an app based on immutable data structures run out of memory?
immutability, immutable.js, out-of-memory, ramda.js
Solution
If new versions of a data structure is created by structural sharing, that means that every new version needs to have a pointer to the previous version in order for it to be able to share anything.
This is not correct in general. The new version would have pointers to subparts of the previous version. So it shares a fraction ( often almost all) of the data of the older version.
For example, Ocaml's maps (represented and implemented by some self-balancing binary search tree variant of red-black trees) are immutable: see documentation of Map. But if you add (or remove) some binding to (from) a map, you get a new map sharing most (but not all) of its internal nodes with the old one.
So the garbage collector would eventually "delete" those old internal nodes which are not relevant to the current "state".
BTW web programming (and web navigation) is related to continuations and continuation-passing style. See e.g. Byrd's Web Programming with Continuations and several papers by C.Queinnec.
Read also more about monads in functional programming.
Problem
Never mind redux or something - I am solely asking about Immutable.JS, Ramda, etc. If new versions of a data structure is created by structural sharing, that means that every new version needs to have a pointer to the previous version in order for it to be able to share anything. That again means that older versions of a structure cannot be garbage collected, meaning again, that in an app where you have state, this state will use a monotonically increasing amount of memory. If this is the case, then that data structure will at some point have used all available memory, if it keeps getting modified. Am I missing something here? I can see that for many (most) use cases on the web (in a browser), this won't be a problem, as you are probably just changing a tiny part of the structure each time, and you will probably leave the page or reload it way before you use all memory, but for long running processes this should pose a problem. Right? Riiight?