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?
In C/C++ it is a common practice to arrange field declarations in often-used structs
Share
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