Handling java.lang.OutOfMemoryErrorEdit

While working on an ANTLR lexer for recognizing RFC 3986-compliant URIs I ran into the following error:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

This page reveals how to increase the heap limits; in the following example I pass use the -Xms switch to set the starting heap size to 32 MB, and the -Xmx switch to set the maximum heap size to 512 MB:

java -Xms32m -Xmx512m org.antlr.Tool RFC3986.g

It turns out that no amount of memory I can throw at ANTLR allows it to successfully complete, and I suspect the likely cause is something in my grammar provoking an infinite loop. My post to the ANTLR mailing list asking about how to troubleshoot this further can be found here.

So even though the -Xms and -Xmx switches didn’t help me in this case, I think they’re important things to have in your Java toolbox.