Java

GWT Compiler Is Running Out of Memory

As part of evaluating an open-source framework which uses Google Web Toolkit I ran into some of the familiar issues I already had a few times earlier with the GWT (GWT 2.4 or 2.5) Maven plugin compilation running out of memory. You know, when you see…

…at the start:

[INFO] #
[INFO] # There is insufficient memory for the Java Runtime Environment to continue.
[INFO] # Native memory allocation (malloc) failed to allocate 32744 bytes for ChunkPool::allocate

…or during compilation:

[INFO]    Compiling 12 permutations
[INFO]       Compiling permutation 1...
[INFO]       Process output
[INFO]          Error occurred during initialization of VM
[INFO]          Could not reserve enough space for object heap

This post is basically a compilation of the answers found online, and a quick reminder for myself and others who are running into similar problems with the fantastic GWT compiler :-).

1. First of all, check if you actually need to compile everything! Skip some browsers or locales – saving all kinds of permutations – if you don’t need it for development. Use the tips about how to speed up the GWT compiler and GWT compilation performance. There’s also an existing FAQ about GWT Debugging and Compiling.If you have control of your machine, cut down on other running applications and processes while you’re compiling.

2. Pass along some more memory

…to Maven, e.g.:

MAVEN_OPTS=-Xms1024m -Xmx1024m -XX:MaxPermSize=256m

…and/or to the gwt-maven-plugin, e.g.:

<configuration>
  <extraJvmArgs>-Xms768m -Xmx768m</extraJvmArgs>
</configuration>

You’ll have to play a bit with the values, depending on the machine you’re working on and the actually available working RAM.

Add some permgen space, which is used when GWT compiles Java into Javascript. E.g.

-XX:MaxPermSize=1024m

3. Try lowering the number of workers to a number explicitly below your available cores/cpu’s. If this number isn’t defined explicitly, the gwt-maven-plugin will set these to the number of available cores/cpu’s. Less workers = less parallellisation = less memory overhead.

<configuration>
  <extraJvmArgs>-Xms768m -Xmx768m</extraJvmArgs>
  <localWorkers>3</localWorkers>
</configuration>

4. Try having the “workers” work in threads, instead of spawning new processes.

<configuration>
  <extraJvmArgs>-Xms768m -Xmx768m -Dgwt.jjs.permutationWorkerFactory=com.google.gwt.dev.ThreadedPermutationWorkerFactory</extraJvmArgs>
  <localWorkers>3</localWorkers>
</configuration>

Hopefully this will save someone some valuable time finding solutions. If you have other options, feel free to add them in the comments.