How to increase the stack limit in OCaml and also Utop

Hey there. I’m working on building a Klotski Puzzle solver in OCaml. (I took the MOOC a couple years back. I did the other final project, but then I thought I would try this one.) This was not an easy problem to solve! However, my solver DOES WORK! But it works for smaller puzzles. For the actual Klotski Puzzle I’m getting a Stack overflow (Looping Recursion?) error message… but I know my functions work! (They may not be 100% efficient, but they do work!) Now so far I’ve only experimented with my solver in the utop REPL, so maybe things will be better when I actually compile my code… I don’t know for sure. So here’s my question. I know that the Java compiler has the -Xss command-line option so that the programmer can request a larger stack if necessary. Does OCaml (and also Utop?) have similar options? If so, what’s the syntax? I think there’s an option called Gc.set( ) but that’s probably for the garbage collector. There must be a similar option to control the size of the stack in the event that the programmer is working with very memory intensive algorithms. (Let’s be honest. It’s not easy to always implement the most efficient algorithms in the world!) How can I increase my stack in Utop? How can I increase my stack when I use ocamlopt and ocamlc?

I really appreciate the help guys! I know my code works just fine for smaller puzzles. If I can just increase my stack limit, then I have no doubt my functions will work on the larger puzzle as well. I just need to find some way to access MORE MEMORY!!!

Thank you so much!



See the description of the envvar OCAMLRUNPARAM in,
especially the l parameter (stack_limit).

So I guess something like

export OCAMLRUNPARAM=l=8000

before running utop would make your stack ~8 times larger.

Note that, as the Gc module documentation says: this is only for bytecode (for instance the toplevel).

For compiled code the stack size is given (or set) by ulimit -s


This was super helpful! Thank you!!!