Java Hack: Double Brace Initialization
refactory.org
Java Hack: Double Brace Initialization
1–10 of 22 posts
Re: Java Hack: Double Brace Initialization
#2Re: Java Hack: Double Brace Initialization
#3Also, 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
#4Watch 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 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
#5Watch 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…
I like this trick though, it will help me with my seaside clone in java :)
Re: Java Hack: Double Brace Initialization
#6Watch 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.
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
#7 List list = Arrays.asList(1,2,3,4,5);Re: Java Hack: Double Brace Initialization
#8Watch 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…
Re: Java Hack: Double Brace Initialization
#9Earlier 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.
Re: Java Hack: Double Brace Initialization
#10Earlier 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 :)
There is something to be said for not baking things into the language.