-2

I have one generated class which get this error. Inside this class, there is one huge static block (5000+ lines). I broke the block into several smaller static blocks but still got this error. Why is that

Edit Code looks like:

private static final Map<Object, Object> nameMap = Maps.newHashMap();
static{
    nameMap.put(xxx);
    .... 5000 similar lines
    nameMap.put(xxx);
}
Edmond
  • 614
  • 2
  • 11
  • 26
  • We'd be able to help much more if we could see the offending code. – Dennis Meng Sep 10 '13 at 21:44
  • 1
    [SSCCE](http://www.sscce.org/) please? – Xynariz Sep 10 '13 at 21:45
  • look at compilier options or split up in more classes – lordkain Sep 10 '13 at 21:48
  • 1
    I guess the operand stack is over 64Ki, but splitting into different methods should fix this. Unless there is a very good reason for this class to contain 5K lines, you *seriously* need to redesign. – Maarten Bodewes Sep 10 '13 at 21:53
  • @owlstead, As I mentioned. It's a generated class. I agree that splitting into different methods should fix this. My question is that I have a large static block(not method), and I've splitted it into several smaller static method but why I'm still getting this error? – Edmond Sep 10 '13 at 21:58
  • There is a max size limit of 64K (bytecode size) for a Java method. There is sort of an exclusion to this limit for the `static` block, but numerous ways you can trip over it. I forget all of the ways, but there are several places where a 64K limit is imbedded in the architecture. (Possibly it's the number of elements in the constant pool, or it's physical size.) – Hot Licks Sep 10 '13 at 22:13
  • @HotLicks Added the source of the 64Ki bytecode "size limit" to the question that this question duped. – Maarten Bodewes Sep 10 '13 at 22:50

1 Answers1

3

If it is just data you will need to read the data in from a resource.

Arrange for your data file to be in the same location as the class file and use something like this:

class Primes {

    private static final ArrayList<Integer> NUMBERS = new ArrayList<>();
    private static final String NUMBER_RESOURCE_NAME = "numbers.txt";

    static {
        try (InputStream in = Primes.class.getResourceAsStream(NUMBER_RESOURCE_NAME);
                InputStreamReader isr = new InputStreamReader(in);
                BufferedReader br = new BufferedReader(isr)) {
            for (String line; (line = br.readLine()) != null;) {
                String[] numberStrings = line.split(",");
                for (String numberString : numberStrings) {
                    if (numberString.trim().length() > 0) {
                        NUMBERS.add(Integer.valueOf(numberString));
                    }
                }
            }
        } catch (NumberFormatException | IOException e) {
            throw new IllegalStateException("Loading of static numbers failed", e);
        }
    }
}

I use this to read a comma separated list of 1000 prime numbers.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
  • 1
    Are you posting this to the correct question? – Maarten Bodewes Sep 10 '13 at 21:54
  • 2
    @owlstead - If it is just data then this should fix the problem. There seems to be no indication of the nature/content of the *huge static block* mentioned. Title does mention *array size*. – OldCurmudgeon Sep 10 '13 at 21:57
  • @owlstead The spotty problem description really does sound like the OP is trying to cram a large dataset into a static initializer for some reason. I can't think of any other vaguely legitimate reason for such a giant code block. – chrylis -cautiouslyoptimistic- Sep 10 '13 at 21:58
  • @owlstead Ayup, it's a data structure, and "don't do that" seems like an entirely reasonable answer to a question of how to deal with a class with so much constant data it's overflowing the symbol table. – chrylis -cautiouslyoptimistic- Sep 10 '13 at 22:12
  • @chrylis OK, fixed the issues with the code, hope that OldCurmudgeon will like the changes. Removed -1 vote. Did not see the added code block to the question before the answer... – Maarten Bodewes Sep 10 '13 at 22:34
  • @owlstead - thanks for the code tweaks - hate the Java shouty constants but that's probably just me - I hated the shouty C #defines too. – OldCurmudgeon Sep 10 '13 at 22:40
  • @OldCurmudgeon Even if Java is my favourite language, I hate tons of its features. But experience taught me to keep to the defaults, as I am just this guy. And I've been proven incorrect time and time again when I tried to deviate :P My code is never just mine... – Maarten Bodewes Sep 10 '13 at 22:43