`

cpu字长、操作系统字长和jvm中各数据类型占用的字节数关系

阅读更多

        cpu字长是指cpu同时参与运算的二进制位数,现在主流的pc的机器字长都是64位的。机器字长直接决定着机器可寻址的虚拟空间地址大小。

 

The word size of a computer generally indicates the largest integer it can process in a single instruction, and the size of a memory address, which is usually, but not necessarily the same as the integer size.

         操作系统字长要看操作系统的位数,跟机器字长并不一定一致,比如64位机器可以安装32位操作系统。虽然64位cpu的可寻址理论达2^64 bytes,但是由于系统限制,32为系统只能访问最大2^32 bytes的虚拟地址空间。另外,64位os上可以安装32位软件,如64位windows。

         jvm的字长,我找了下jvm specification e7,并没有找到jvm定义字长,但是在《深入Java虚拟机》这本老经典里头,看到作者写到:

The basic unit of size for data values in the Java virtual machine is the word--a fixed size chosen by the designer of each Java virtual machine implementation. The word size must be large enough to hold a value of type byte, short, int, char, float, returnAddress, or reference. Two words must be large enough to hold a value of type long or double. An implementation designer must therefore choose a word size that is at least 32 bits, but otherwise can pick whatever word size will yield the most efficient implementation. The word size is often chosen to be the size of a native pointer on the host platform.

The specification of many of the Java virtual machine's runtime data areas are based upon this abstract concept of a word. For example, two sections of a Java stack frame--the local variables and operand stack-- are defined in terms of words. These areas can contain values of any of the virtual machine's data types. When placed into the local variables or operand stack, a value occupies either one or two words.

As they run, Java programs cannot determine the word size of their host virtual machine implementation. The word size does not affect the behavior of a program. It is only an internal attribute of a virtual machine implementation.

        也就是说,jvm基本的数据单位是字,这个字的字长,spec中是没有规定的,但是必须足够持有byte、short、int、char、float、returnAddress和reference,而两个字的字长足以持有long和double。spec里边规定这些类型最大32位,如int的(2.3 Primitive Types and Values):

int, whose values are 32-bit signed two's-complement integers, and whose default value is zero

        一般字长选择都根据底层主机平台的指针长度来选择,而指针长度是由cpu运行模式的寻址位数决定的,所以64位的机器上,运行64位OS,安装64位jvm的话,对象地址可能是64bit的,当然这根实际使用内存大小jvm参数设置相关

        而这里提到,基于零基压缩,分配给jvm的内存必须控制在4g到32g之间,因为对象头是最小8个字节,普通寻址是地址指向字节,如果我们知道jvm里对象大小都是8字节对齐,那么一个地址可以指向8字节的首地址,也就是寻址能力扩大八倍,当然这里是指hotspot vm。而为啥最小4g呢?因为小于4g就不需要压缩了。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics