We've never documented that direct buffers are page aligned and we've never encountered an application that assumes it either. For jdk7, we would like to change the implementation so that direct buffers are not aligned by default. A new VM option can be added to force page alignment if required. This solution should reduce the memory usage, and also improve the allocation slightly as there is less memory to be be zero'ed (on that, there shouldn't be a need to zero the memory between address and base because it is not accessible via a ByteBuffer).
This RFE suggests four different enhancements:
1. Removal of page alignment.
2. Removal of memory zero-initialization.
3. Removal of the "<< 0" scatterred throught the DirectByteBuffer class.
4. Simplication of assert statements of the form:
assert (a <= b);
int x = (a <= b ? b - a : 0);
assert (a <= b);
int x = b - a;
The first suggestion seems reasonable. I presume that page-alignment was originally required for performance reasons since it would allow us to guarantee a minimum number of pages for any memory allocation (thus reducing page faults). Presumably it would also allow for device I/O where pagealignment is required. After careful consideration, we've decided to remove this alignment. We believe that it is no nessary because as time has progressed, page sizes have become larger; typically exceeding the size of buffer allocation. Also, we havn't found any code that depends or requires page aligment. The specification makes no reference to this implementation detail, so thisshould just be a simple matter of code removal/simplification.
Since buffers must always be initialized with some fixed value for security purposes, the second suggestion can not be implemented. Bug 6535542 (integrated jdk7-b15) modified the specification to require zero-initialization.
The code-snippet referenced in the third suggestion is, as speculated, a result of auto-generated code. The value of $LG_BYTES_PER_VALUE$ for byte is 0. I have verified that the javac compiler optimizes this shift out of the generated byte code. I suppose that we could we could introduce additional complexity in the source to handle this degenerate case, but it hardly seems necessary.
The final suggestion, the simplification of assert/assignment statements is not possible. The replacement code is only equivalent if system assertions are enabled. For performance reasons, this is not the default behaviour of the VM. Furthermore since the buffer classes explicitly state they expect user code to handle synchronization, it is possible that a buffer may be in an incorrect state for at the time when the values for "a" or "b" are retrieved.