Live data from Hacker News

Java Hack: Double Brace Initialization

refactory.org

1–10 of 22 posts

Re: Java Hack: Double Brace Initialization

#3
Watch out - double-brace initialization is cool looking, but it's orders of magnitude slower than regular initialization as it must generate an anonymous class.

Also, the generated class is put into the permgen space, which is not garbage collected. The permgen is pretty small by default - and if you fill it up, your system is hosed.

I use them all the time in unit tests, but never in production code.

Re: Java Hack: Double Brace Initialization

#4
post #3

Watch out - double-brace initialization is cool looking, but it's orders of magnitude slower than regular initialization as it must generate an anonymous class. Also, the generated class is put into the permgen space, which is not garbage collected. The permgen is pretty small by default - and if you fill it up, your system is hosed. I use them all the time in unit tests, but never in production code.

Interesting. Even in Perl we cache and garbage-collect anon classes.

(The caching is for the very common case of creating a subclass with optional roles applied:

    my $class = Some::Meta::Class->create_anon_class(
        superclasses => ['Foo'],
        roles        => [qw/Commonly Applied Traits/],
    );
There is almost no speed hit the second time you create an instance of "Foo" with "Commonly", "Applied", and "Traits" applied. Not sure this would be useful for Java, although we can achieve the same effect as in the article.)

Re: Java Hack: Double Brace Initialization

#5
post #4
post #3

Watch out - double-brace initialization is cool looking, but it's orders of magnitude slower than regular initialization as it must generate an anonymous class. Also, the generated class is put into the permgen space, which is not garbage collected. The permgen is pretty small by default - and if you fill it up, your system is hosed. I use them all the time in unit tests, but never in production code.

Interesting. Even in Perl we cache and garbage-collect anon classes. (The caching is for the very common case of creating a subclass with optional roles applied: my $class = Some::Meta::Class->create_anon_class( superclasses => ['Foo'], roles => [qw/Commonly Applied Traits/], ); There is almost no speed hit the second time you create an instance of "Foo" with "Commonly", "Applied", and "Traits" applied. Not sure this…

perl has a more stable vm than java :)

I like this trick though, it will help me with my seaside clone in java :)

Re: Java Hack: Double Brace Initialization

#6
post #3

Watch out - double-brace initialization is cool looking, but it's orders of magnitude slower than regular initialization as it must generate an anonymous class. Also, the generated class is put into the permgen space, which is not garbage collected. The permgen is pretty small by default - and if you fill it up, your system is hosed. I use them all the time in unit tests, but never in production code.

The compiler generates the anonymous class. To the runtime, isn't it just a class with a name given to it by the compiler, not a developer (question mark...my key broke today)

It's true that it uses up permgen though, but if it only adds an initializer we're not talking about much space.

Edit:

I did an experiment. This is the code:

     new ArrayList() {
       {}
     };
     new ArrayList();
Here is the bytecode:

     0  new Main$1 [16]
     3  invokespecial Main$1() [18]
     6  new java.util.ArrayList [19]
     9  invokespecial java.util.ArrayList() [21]
And, the class:

    class Main$1 extends java.util.ArrayList {

    // Method descriptor #6 ()V
    // Stack: 1, Locals: 1
    Main$1();
      0  aload_0 [this]
      1  invokespecial java.util.ArrayList() [8]
      4  return
    Line numbers:
      [pc: 0, line: 10]
      [pc: 4, line: 1]
    Local variable table:
      [pc: 0, pc: 5] local: this index: 0 type: new Main(){}
Just a regular old subclass.

Re: Java Hack: Double Brace Initialization

#8
post #6
post #3

Watch out - double-brace initialization is cool looking, but it's orders of magnitude slower than regular initialization as it must generate an anonymous class. Also, the generated class is put into the permgen space, which is not garbage collected. The permgen is pretty small by default - and if you fill it up, your system is hosed. I use them all the time in unit tests, but never in production code.

The compiler generates the anonymous class. To the runtime, isn't it just a class with a name given to it by the compiler, not a developer (question mark...my key broke today) It's true that it uses up permgen though, but if it only adds an initializer we're not talking about much space. Edit: I did an experiment. This is the code: new ArrayList () { {} }; new ArrayList (); Here is the bytecode: 0 new Main$1 [16] 3 i…

You can always copy and paste a question mark (?). I don't recommend coding that way, I once tried coding with the left quarter of the keyboard broken, not fun.

Re: Java Hack: Double Brace Initialization

#9
post #8
post #6

Earlier quoted context omitted.

The compiler generates the anonymous class. To the runtime, isn't it just a class with a name given to it by the compiler, not a developer (question mark...my key broke today) It's true that it uses up permgen though, but if it only adds an initializer we're not talking about much space. Edit: I did an experiment. This is the code: new ArrayList () { {} }; new ArrayList (); Here is the bytecode: 0 new Main$1 [16] 3 i…

You can always copy and paste a question mark (?). I don't recommend coding that way, I once tried coding with the left quarter of the keyboard broken, not fun.

alt + keypad 63 works too (at least on Windows)

Re: Java Hack: Double Brace Initialization

#10
post #4

Earlier quoted context omitted.

Interesting. Even in Perl we cache and garbage-collect anon classes. (The caching is for the very common case of creating a subclass with optional roles applied: my $class = Some::Meta::Class->create_anon_class( superclasses => ['Foo'], roles => [qw/Commonly Applied Traits/], ); There is almost no speed hit the second time you create an instance of "Foo" with "Commonly", "Applied", and "Traits" applied. Not sure this…

perl has a more stable vm than java :) I like this trick though, it will help me with my seaside clone in java :)

FWIW, this is all done in "user space". The metaclass instance is attached to every instance that it creates. When all the instances go away, there are no more references to the metaclass, and it gets reclaimed. The destructor handles deleting the symbol table entries that were added, and that's that.

There is something to be said for not baking things into the language.

Post reply on HN