14

The following code fails with a NullPointerException in main (map==null). The issue occurs only if I define 2001 or more Enum constants, 2000 work fine.

Why isn't the static code block not executed?

Do we hit any silent limit of the compiler (no warnings, no errors) or JVM?

The compiled class file exceeds 172KB,

import java.util.HashMap;

public enum EnumTest {
    E(1),E(2),...,E(2001);

    private static HashMap<Integer, EnumTest>   map = new HashMap<Integer, EnumTest>();

    static {

        for ( EnumTest f : EnumTest.values() ) {
            map.put( (int) f.id, f );
        }
    }
    short id;

    private EnumTest(int id) {
        this.id = (short) id;
    };

    public short getId() {
        return id;
    }

    public static final EnumTest fromInt(int id) {
        EnumTest e = map.get( id );
        if ( e != null ) {
            return e;
        }
        throw new IllegalArgumentException( "" + id );
    }

    public static void main(String[] args) {
        System.out.println( "size:" + map.size() );
    }
}

Runtime Environment:

java version "1.7.0_01"
Java(TM) SE Runtime Environment (build 1.7.0_01-b08)
Java HotSpot(TM) 64-Bit Server VM (build 21.1-b02, mixed mode)

also happens with:

java version "1.6.0_32" Java(TM) SE Runtime Environment (build
1.6.0_32-b05) Java HotSpot(TM) Client VM (build 20.7-b02, mixed mode, sharing)
starblue
  • 55,348
  • 14
  • 97
  • 151
stacker
  • 68,052
  • 28
  • 140
  • 210
  • 3
    Can I ask why the *biep* you have an enum with 2001 constants? ^^ – brimborium Aug 09 '12 at 12:07
  • 1
    @brimborium Because they denote over 2000 different binary messages from a legacy system, the code is generated. Of course I wouldn't write them by hand. – stacker Aug 09 '12 at 12:11
  • 1
    Sounds like a job for a database, not code generation. – user207421 Aug 09 '12 at 12:15
  • Hm ok. I just don't know if an enum is the best way to deal with that... Why not use a map directly (like you already do, but without the enum wrap around)? btw: Good question anyway! ;) – brimborium Aug 09 '12 at 12:15
  • 1
    The second answer [here](http://stackoverflow.com/questions/1823346/whats-the-limit-to-the-number-of-members-you-can-have-in-a-java-enum) might help. – Thomas Aug 09 '12 at 12:17
  • is this version specific? I do not get error with 2001 – Jayan Aug 09 '12 at 12:18
  • Theoretically, you should be able to have about 64K values, as that's the # of fields allowed in a class (enum values are stored as static fields). – Matt Aug 09 '12 at 12:20
  • Maybe you hit an optimisation that re-orders the initialisation sequence? What happens when you move the map creation into the `static` block? – rsp Aug 09 '12 at 12:23
  • @Jayan Thanks for checking,I added the runtime version to the question. Which Java-version did you use? – stacker Aug 09 '12 at 12:24
  • @rsp I found that moving the instantiation into the static block doesn't make a difference. – stacker Aug 09 '12 at 12:25
  • with a large number like 3034, compilation failed with 'code too large'. – Jayan Aug 09 '12 at 12:27
  • @rsp System.out.println( "size:" + map.size() ); // map == null – stacker Aug 09 '12 at 12:29
  • What happens if you make `map` `final`? – rsp Aug 09 '12 at 12:33
  • @rsp unfourtunately declaring the map as final doesn't help. – stacker Aug 09 '12 at 12:40
  • 1
    I cannot reproduce this on JDK 1.7.0_03. But for enums larger than 25xx I get "code too large" compiler error. Otherwise your code works fine for me. This is mentioned here as well: http://stackoverflow.com/questions/2407912/code-too-large-compilation-error-in-java – omerkudat Aug 09 '12 at 12:51

2 Answers2

14

These kinds of problems come from the fact that some (often compiler-generated) initializer code exceeds 65536 bytes of byte code. A single method can not contain more than that many bytes of bytecode to be executed (due to restrictions in the class file format).

A common source of problems like this are large arrays like this:

byte someBytes = { 1, 2, 3, ..., someBigValue };

The problem here is that such fields are actually initialized with someBigValue assignment statements in a generated initializer (constructor or static initializer).

Enum values are actually initialized in a similar fashion.

Given the following enum class:

public enum Foo {
  CONSTANT(1);

  private Foo(int i) {
  }
}

We look at the output of javap -v and see the following code block:

  static {};
    flags: ACC_STATIC
    Code:
      stack=5, locals=0, args_size=0
         0: new           #4                  // class Foo
         3: dup
         4: ldc           #7                  // String CONSTANT
         6: iconst_0
         7: iconst_1
         8: invokespecial #8                  // Method "<init>":(Ljava/lang/String;II)V
        11: putstatic     #9                  // Field CONSTANT:LFoo;
        14: iconst_1
        15: anewarray     #4                  // class Foo
        18: dup
        19: iconst_0
        20: getstatic     #9                  // Field CONSTANT:LFoo;
        23: aastore
        24: putstatic     #1                  // Field $VALUES:[LFoo;
        27: return

As you can see there are quite a lot of bytecode operations that handle instantiating CONSTANT with the correct values. If you have many such enum values, then the size of that static initializer block can easily exceed 64k bytes of code and thus make the class uncompilable.

A possible workaround is to reduce the size of the initializing code by reducing the number of arguments (for example by calculating the number passed in based on the index of the enum value instead of using an argument). That might just give you enough wiggle room to extend this a bit further.

Alternatively you could try splitting your enum into several enums connected by implementing a common interface. The enums could be grouped by area/intention/category/...:

public interface MessageType {
  int getId();
}

public enum ConnectionMessage implements MessageType {
  INIT_CONNECTION(1),
  LOGIN(2),
  LOGOUT(3),
  CLOSE_CONNECTION(4);

  // getId code, constructor, ...
}

public enum FrobnicationMessage implements MessageType {
  FROBNICATE_FOO(5),
  FROBNICATE_BAR(6),
  DEFROB_FOO(7),
  DEFROB_BAR(8),
  ...

  // getId code, constructor, ...
}

I'm assuming that the enum values are actually referenced somewhere in your code and not just pure value holders, if they only hold values and individual values are not treated differently in your code, then replacing them with a single class instantiated once per data item stored in a central resource is probably the best approach.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
7

I suspect what you should be seeing is an error when you compile

error: code too large

Perhaps your version of the compiler has a bug and doesn't show this.

When I create 2500 enums values it fails with this error but with 2400 enum values it runs correctly.

There is a limit of 64 KB for the byte code of any method and the enums are initialised in the one method for the static initializer block.

The problem is that many byte code instructions use the byte offset as a 16-bit value which places this limitation on the whole method (even if there is no such instruction at the end of a method)

The javac is not smart enough to break up the static initialiser block into multiple sub-methods, but then again having thousands of enums suggest you could do what is required another way.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130