tags:

views:

59

answers:

2

In C/C++ it is a common practice to arrange field declarations in often-used structs so that fields of various sizes are packed tightly together, and aligned to word boundaries whenever possible. Code readability is sacrificed for machine efficiency. Is the same practice worthwhile for Java? Does real-world Java compilers/VMs re-arrange them internally for alignment?

+1  A: 

If you have the resources to run a virtual machine, you'll probably won't need to care about such small details. At least in a typical server side Java application, the approach generally is to code for clarity and maintainability, and trust the compiler and the VM to take care of efficiency. Within reason, of course.

e.p.
+1  A: 

Found the answer myself for the HotSpot (Sun) JVM:

"Object Packing

Object packing functionality has been added to minimize the wasted space between data types of different sizes. This is primarily a benefit in 64-bit environments, but offers a small advantage even in 32-bit VMs.

.... (example field declarations)

Now, the fields are reordered to look like this:

.... (reordered field declarations)

In this example, no memory space is wasted. "

from http://java.sun.com/products/hotspot/docs/whitepaper/Java_Hotspot_v1.4.1/Java_HSpot_WP_v1.4.1_1002_2.html

bchen