My code is:
if (frameRGBABuffer == null) {
frameRGBABuffer = ByteBuffer.allocateDirect(cameraHeight * cameraWidth * 4)
.order(ByteOrder.nativeOrder());
}
Log.d("tag",frameRGBABuffer.array().length)
My camera resolution is 1280×720, thus frameRGBABuffer
should allocate 3686400 bytes of space.
But it is strange that the length of frameRGBABuffer.array()
is 3686407. Why does it have extra 7 bytes spaces?
By the way, frameRGBABuffer.array() throws no exceptions and returns a byte[] with datas
It seems that Android allocate 7 extra spaces to handle the alignment. The source code is :
MemoryRef(int capacity) {
VMRuntime runtime = VMRuntime.getRuntime();
buffer = (byte[]) runtime.newNonMovableArray(byte.class, capacity + 7);
allocatedAddress = runtime.addressOf(buffer);
// Offset is set to handle the alignment: http://b/16449607
offset = (int) (((allocatedAddress + 7) & ~(long) 7) - allocatedAddress);
isAccessible = true;
isFreed = false;
}