Home > Blockchain >  Why are Java objects 8-byte aligned instead of 4-byte aligned
Why are Java objects 8-byte aligned instead of 4-byte aligned

Time:12-05

I was using Jol to look at the memory layout of the object and found an 8-byte alignment field in the object field. What bothered me was that I didn't understand why the 8-byte alignment was not 4-byte

CodePudding user response:

I think I have found the answer. The size of a pointer to a CPU address is 8 bytes. If an object is not aligned to 8 bytes, then it may pollute the cache line

CodePudding user response:

Well, java runs on computers, and nearly every computer that was available when Java was written aligns bytes on 8 bits. Actually a large number of them (when Java was written) aligned on 16 or 32 bits, but you could address (read) just 8 bits from the larger value.

Java tried to be "compact" meaning its programs would take less storage, so it created the JVM op-codes as 8 bit instructions (when they could have used more intructions).

Machines fetch "word" which are not generally "bytes" on modern hardware. It is not possible for a machine to fetch less than a word, which is generally 32 or 64 bits, but it is possible to "read" less than a word (the smallest unit of reading at this time is a byte). This means that every time you read a byte, you really read a word and then only use part of the word. Fortunately the word is "cached" and if you read another byte from the same word, it is extremely rare that you will actually re-fetch the word from system RAM.

While there is a name for 1/2 a byte (it's called a "nybble") it is not clear to me if there was ever a system that addressed on nybble boundaries. Perhaps there were some early microcontrollers in the 70's that used nybbles; but, 8 bit controllers were not hard to find at the time I started using computers in the mid-70's.

I did once work on a system that required 32 bit alignment (reads on 32 bit boundaries) but the program we had was older than the hardware and it would do non-aligned reads all the time. There was a significant hardware inefficiency during the read, as the operating system would raise exceptions for the non-aligned read. We wrote an exception handler that would find the surrounding word and then re-read the word and copy the byte into the appropriate register. This slowed the computer down significantly. When we rewrote some of the code to be "word aligned" it ran about 30% faster. So, any maintained programming language will not be aligned to anything other than what the hardware supports.

  •  Tags:  
  • java
  • Related