tags:

views:

552

answers:

6

In C, I can allocate a register for a variable, for example:

register int i = 0;

I am aware that Java is an interpreted language, and is many many abstractions away from the CPU.

Is there any mechanism available to even request (and if the architecture doesn't allow it, so what) that my variable remains in a register instead of moving to cache or main memory?

I don't suppose there is any way, but I have been pleasantly surprised before.

Thank you,

+14  A: 

register in C does not put a variable to register. It simply gives the compiler the hint, that it would probably be good to put it into a register.

In Java there is no equivalent.

Johannes Weiß
+6  A: 

If it's used enough in a short space that making it a register int would be worthwhile, then the hotspot compiler should be able to figure that out itself.

In fact, the hotspot compiler should be able to do a better job than the C/C++ compiler, because it has more information to work with. C/C++ compilers have to guess; HotSpot can measure.

Michael Myers
+2  A: 

No, there's no way to request this in Java. However, there are some things that you can do that will prevent a register from being used, such as applying the volatile modifier to a member variable.

erickson
register would only apply to a local variable (or parameter). Java's volatile is not applicable to either. IIRC, what register actually does it prevent a reference been taken to a variable, which Java doesn't do anyway.
Tom Hawtin - tackline
Does HotSpot ever cache the value of a member variable in a register? I thought that `volatile` was needed, in part, to ensure that changes to a (copy of a) value in a register were pushed out to main memory.
erickson
volatile only guarantees that all threads will see the latest version. There's no guarantee that it won't be loaded into a register. That thread visibility is completely left up to the JVM.
Steve Kuo
Right. As I explained in my comment, a register can be used. The point I'm making is that every volatile read or write must include a trip out to main memory. Operations on a non-volatile members (or local variable) don't incur that expense.
erickson
+5  A: 

There's no equivalent in Java. Even in C there is no guarantee that the variable will be stored in a register and compilers are free to ignore it.

In Java, the method will be interpreted until the hotspot JIT heuristically determines that it needs to be compiled. For compiled code it uses a coloring algorithm to assign variables and temporary values to registers, or write to/from RAM in the case of register overflow.

sk
+3  A: 

I am aware that Java is an interpreted language, and is many many abstractions away from the CPU.

You pretty much answered your own question there :-)

But seriously, in general, write your code as clearly and simply as you can, and the JVM will do what it can to treat your code right.

Ben Hardy
A: 

You can create an annotation called @register but the JVM will definitely ignore it. e.g.

@register int i = 0;
Peter Lawrey