Too many constants error when compiling using the Java backend (2)
The Java compiler gives the following error: “Too many constants, the constant pool for Main would exceed 65536 entries”. I bumped into this problem when compiling the query-optimization branch of WebDSL. In revision 5375 of that branch I worked around the problem by moving some static code (without meta variables) from a bstm* block to a Java class and calling the moved code from the bstm* block instead. Specifically I moved code from “src/org/webdsl/dsl/to-java-servlet/hibernate-util.str” to java-runtime/src/org/webdsl/tools/Utils.java inside a new handleSchemaCreateUpdate method.
The large amount of constants seem to come from lists that contain only constant values. Stratego creates constants inside the Main class for all the values and sub-lists of the constant list, even when the sub-lists are only used inside the constant list. This means that
[ "a", "b", "c" ]
will create the following constants (every line is a constant)
[] "c" [ "c" | [] ] "b" [ "b" | [ "c" | [] ] ] "a" [ "a" | [ "b" | [ "c" | [] ] ] ]
In the generated Main.java this will likely look like:
constNil0 = (IStrategoTerm)termFactory.makeList(Term.NO_TERMS); const657 = termFactory.makeString("c"); constCons157 = (IStrategoTerm)termFactory.makeListCons(const657, (IStrategoList)constNil0); const658 = termFactory.makeString("b"); constCons158 = (IStrategoTerm)termFactory.makeListCons(const658, (IStrategoList)constCons157); const659 = termFactory.makeString("a"); constCons159 = (IStrategoTerm)termFactory.makeListCons(const659, (IStrategoList)constCons158);
So for a single small constant list, already 7 constants are generated, even if only the last one is used elsewhere. Since bstm* blocks are also lists, they will generate a lot of constants if they contain a lot of constant code.
Submitted by Christoffer Gersen on 21 September 2012 at 11:58
Issue Log
I also get this error.
`if which ecj >/dev/null; then echo ecj; else echo javac; fi` -J-Xmx512m -J-Xms100m -J-server -J-XX:+UseParallelGC -source 5 -target 5 -nowarn -cp /nix/store/jc4gx3fs25wp5d6ml0hb4hgfwadppkcf-strc-java-0.17.92pre24455/share/strc-java/strategoxt.jar:src-gen:. src-gen/org/webdsl/webdslc/Main.java -d bin ---------- 1. ERROR in /home/elmer/newLocation/src/src-gen/org/webdsl/to_java_servlet/Main.java (at line 23) @SuppressWarnings("all") public class Main ^^^^ Too many constants, the constant pool for Main would exceed 65536 entries ---------- 1 problem (1 error)make[1]: *** [webdsl.jar] Error 255 make[1]: Leaving directory `/home/elmer/newLocation/src' make: *** [all-recursive] Error 1 sudo rm -r /WEBDSLINSTALL sudo make install Making install in src make[1]: Entering directory `/home/elmer/newLocation/src' mkdir -p bin `if which ecj >/dev/null; then echo ecj; else echo javac; fi` -J-Xmx512m -J-Xms100m -J-server -J-XX:+UseParallelGC -source 5 -target 5 -nowarn -cp /nix/store/jc4gx3fs25wp5d6ml0hb4hgfwadppkcf-strc-java-0.17.92pre24455/share/strc-java/strategoxt.jar:src-gen:. src-gen/org/webdsl/webdslc/Main.java -d bin src-gen/org/webdsl/to_java_servlet/Main.java:23: too many constants @SuppressWarnings("all") public class Main ^ Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf(Arrays.java:2894) at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:117) at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:407) at java.lang.StringBuilder.append(StringBuilder.java:136) at sun.net.www.protocol.jar.Handler.parseURL(Handler.java:92) at java.net.URL.<init>(URL.java:614) at java.net.URL.<init>(URL.java:482) at sun.misc.URLClassPath$JarLoader.checkResource(URLClassPath.java:688) at sun.misc.URLClassPath$JarLoader.getResource(URLClassPath.java:773) at sun.misc.URLClassPath.getResource(URLClassPath.java:185) at java.net.URLClassLoader$1.run(URLClassLoader.java:209) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:205) at java.lang.ClassLoader.loadClass(ClassLoader.java:321) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294) at java.lang.ClassLoader.loadClass(ClassLoader.java:266) at java.util.ResourceBundle$Control.newBundle(ResourceBundle.java:2401) at java.util.ResourceBundle.loadBundle(ResourceBundle.java:1424) at java.util.ResourceBundle.findBundle(ResourceBundle.java:1383) at java.util.ResourceBundle.findBundle(ResourceBundle.java:1310) at java.util.ResourceBundle.findBundle(ResourceBundle.java:1310) at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1252) at java.util.ResourceBundle.getBundle(ResourceBundle.java:733) at com.sun.tools.javac.util.Messages.add(Messages.java:80) at com.sun.tools.javac.util.Messages.<init>(Messages.java:66) at com.sun.tools.javac.main.Main.getLocalizedString(Main.java:481) at com.sun.tools.javac.main.Main.resourceMessage(Main.java:455) at com.sun.tools.javac.main.Main.compile(Main.java:398) at com.sun.tools.javac.main.Main.compile(Main.java:308) at com.sun.tools.javac.main.Main.compile(Main.java:299) at com.sun.tools.javac.Main.compile(Main.java:82) at com.sun.tools.javac.Main.main(Main.java:67) make[1]: *** [webdsl.jar] Error 1
Log in to post comments