cpu字長、作業系統字長和jvm中各資料型別佔用的位元組數關係
cpu字長是指cpu同時參與運算的二進位制位數,現在主流的pc的機器字長都是64位的。機器字長直接決定著機器可定址的虛擬空間地址大小。
Java程式碼- 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虛擬機器》這本老經典裡頭,看到作者寫到:
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
- 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):
Java程式碼- 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就不需要壓縮了。