Why do Java objects have to be a multiple of 8?

后端 未结 3 766
说谎
说谎 2020-12-09 09:20

I know that Java uses padding; objects have to be a multiple of 8 bytes. However, I dont see the purpose of it. What is it used for? What exactly is its main purpose?

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-09 10:00

    Its purpose is alignment, which allows for faster memory access at the cost of some space. If data is unaligned, then the processor needs to do some shifts to access it after loading the memory.

    Additionally, garbage collection is simplified (and sped up) the larger the size of the smallest allocation unit.

    It's unlikely that Java has a requirement of 8 bytes (except on 64-bit systems), but since 32-bit architectures were the norm when Java was created it's possible that 4-byte alignment is required in the Java standard.

提交回复
热议问题