Java "too many constants" JVM error

I am developing an application that generates and compiles classes at runtime. This sometimes creates a huge amount of generated code.

In one of our test cases, I get a JVM error message:

TestClass.java:83865: too many constants 

Just this. I saw other messages about a similar error, but in those cases the error message complains about a constant pool. But in this case it is not so.

If this means that the limit in the JVM constant pool has been reached, what does it mean? I mean, what are these constants in the sense of Java code? Class methods Fields? Literals? I have no static or final methods and fields.

Can you give me some results?

EDIT:

Separation of the code into several classes is already in progress. Although it was not for this reason.

I know the limits of the constant pool, my doubt was exactly what was happening. The generated code does not contain more than 10,000 labels + fields.

My doubt is that literals also go to a constant pool or not, as this is the only reason I can raise this number to 65K. It seems so.

+7
source share
4 answers

http://en.wikipedia.org/wiki/Java_class_file#The_constant_pool

The constant pool includes numbers, strings, method names, field names, class names, class and method references ... basically everything.

There can be no more than 65536.

+8
source

Section 5.1 of the JVM specification defines exactly what a constant pool is (mostly references to classes / methods and literals).

+2
source

From: JVM Specification

you can see that classfile.constant_pool_count is of type "u2" which limits it to 65,535 entries

 ClassFile { u4 magic; u2 minor_version; u2 major_version; u2 constant_pool_count; cp_info constant_pool[constant_pool_count-1]; u2 access_flags; u2 this_class; u2 super_class; u2 interfaces_count; u2 interfaces[interfaces_count]; u2 fields_count; field_info fields[fields_count]; u2 methods_count; method_info methods[methods_count]; u2 attributes_count; attribute_info attributes[attributes_count]; } 
+2
source

I do not know if this is very relevant, but for the constants of the array, EVERY field counts to it. I'm not quite sure why (I assume that you are not actually giving a literal, and thus the runtime must enter each value manually, so it is necessary that they all be constants), but it is, and so it is and there is.

I found this problem when I created a large cache as an array literal. Since I did not want to write out 1,000,000 constants manually, I wrote a small program to write a program for me. I wanted to see how fast the program was when everything was cached at startup.

(The problem in question is 1.6.1 from Skile and Revilla's โ€œProgramming Problem,โ€ ISBN 0-387-00163-8)

So, you may have some array somewhere with literals. This is not considered ONE constant. It is considered the constants of array.length. Or array.length + 1 constants.

0
source

All Articles