为什么Java对象必须是8的倍数? [英] Why do Java objects have to be a multiple of 8?

查看:2707
本文介绍了为什么Java对象必须是8的倍数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道Java使用填充;对象必须是8个字节的倍数。但是,我没有看到它的目的。它是干什么用的?它的主要目的究竟是什么?

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?

推荐答案

其目的是 alignment ,允许以某些空间为代价更快地访问内存。如果数据未对齐,那么处理器需要在加载内存后进行一些调整才能访问它。

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.

Java不太可能需要8个字节(64位系统除外),但由于32位架构是常态在创建Java时,Java标准中可能需要4字节对齐。

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.

这篇关于为什么Java对象必须是8的倍数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆