cost of Java "assert" when disabled?
Dmitry.Samersoff at oracle.com
Fri Feb 17 02:49:09 PST 2012
asserts causes new object allocation, stackmaptable and exception table
int value = 10;
assert false : value;
0: bipush 10
3: getstatic #2; //Field $assertionsDisabled:Z
6: ifne 18
9: new #3; //class java/lang/AssertionError
#4; //Method java/lang/AssertionError."<init>":(I)V
cause that openFile() will never be executed if assertions
So you have to write:
bool value = MyClass.openFile();
assert false : System.err.println("False");
will not ever compile, because System.err.println is void.
so you have to write
assert false : "False";
and then rely on C1/C2 to eliminate useless code.
** IMHO, counting all above it's better to write your own function
MyAssert doing exactly what you need rather than use build-in one.
** Sidestepping to conditional compilation - I vote for it
with both hands.
On 2012-02-17 00:42, Srinivas Ramakrishna wrote:
> A Java language newbie question:
> Does anyone have any ballpark numbers on the cost (and its scaling) of
> peppering assert's in your Java code, but with asserts disabled (-da) ?
> In other words, is the disabled cost so vanishingly small that we need
> not think twice when using them, or should one be
> careful because the cost is non-negligible and can affect (bytecode)
> footprint or rutime performace even when switched off?
> thanks for any advice.
> -- ramki
Java Hotspot development team, SPB04
* There will come soft rains ...
More information about the hotspot-dev