1. 程式人生 > >Java 記憶體分配——Thinking in Java 4th 讀書筆記

Java 記憶體分配——Thinking in Java 4th 讀書筆記

做開發多年,一直忙於專案,從沒好好的整理知識,從現在開始,儘量每週多抽時間整理知識,分享在部落格,在接下來的部落格中,我將為大家分享我讀《Java程式設計思想4th》英文版讀書筆記,一來便於知識的梳理,二來分享給需要的朋友,評價很高的一本書,推薦大家閱讀,因為書的邊幅比較長,如果不想閱讀整本書歡迎來我的部落格,我會定期分享書的重點內容,以翻譯的形式展現。

There are five different places to store data: 

儲存資料的地方有5個

1. Registers. This is the fastest storage because it exists in a place different from that of other storage: inside the 

1.暫存器這是最快的儲存,因為它存在的位置與其他的儲存方式有別:它位於處理器中。

processor. However, the number of registers is severely limited, so registers are allocated as they are needed. You 

然而,暫存器的數量是嚴格限制的,所以暫存器是當它需要時才分配的。

don’t have direct control, nor do you see any evidence in your programs that registers even exist (C & C++, on the 

你既不能直接去控制,也看不到暫存器在你程式中存在的跡象。(然而,C 和C++允許你去建議編譯器去分配暫存器)

other hand, allow you to suggest register allocation to the compiler).  


2. The stack. This lives in the general random-access memory (RAM) area, but has direct support from the processor 

2.棧。它位於通用隨機儲存器區域,但可以通過它的棧指標從處理器那裡獲得直接的支援。

via its stack pointer. The stack pointer is moved down to create new memory and moved up to release that memory. 

棧指標向下移動是建立新的儲存空間,向上移動是釋放儲存空間。

This is an extremely fast and efficient way to allocate storage, second only to registers. The Java system must know, 

這是一種十分快速並且有效的方法去分配記憶體,僅次於暫存器                                              

while it is creating the program, the exact lifetime of all the items that are stored on the stack. This constraint places 

當建立一個程式時,Java系統必須知道儲存在棧上所有項的準確生命週期。                  

limits on the flexibility of your programs, so while some Java storage exists on the stack—in particular, object 

這個約束一定程度上限制了你的程式的靈活性,所以當一些Java儲存在棧上時,特別是物件的引用——Java物件本身

references—Java objects themselves are not placed on the stack.  

並沒有存放在堆疊上


3. The heap. This is a general-purpose pool of memory (also in the RAM area) where all Java objects live. The 

3.堆。 這是通用目的的儲存池(同樣是在隨機儲存區域),也就是Java物件儲存的地方,

nice thing about the heap is that, unlike the stack, the compiler doesn’t need to know how long that storage must 

不像棧那樣,堆的一個好處是,編譯器並不需要知道資料在堆上儲存的時間。

stay on the heap. Thus, there’s a great deal of flexibility in using storage on the heap. Whenever you need an object, 

因此,使用堆來儲存擁有極大的靈活性。    當你需要一個物件(不是指你想相處的物件,開個小玩笑),

you simply write the code to create it by using new, and the storage is allocated on the heap when that code is 

你只需要簡單的通過new 來建立它, 並且當代碼執行的時候記憶體才會在堆上分配。

executed. Of course there’s a price you pay for this flexibility: It may take more time to allocate and clean up heap 

當然,擁有靈活性是有代價的,它可能比棧花更多的時間去分配和清理堆記憶體。

storage than stack storage .  


4. Constant storage. Constant values are often placed directly in the program code, which is safe since 

4.常量儲存。  常量值經常被放在程式程式碼裡,                                                               這是安全的

they can never change. Sometimes constants are cordoned off by themselves so that they can be optionally placed in 

因為它們永遠不會改變。  有時候常量被它們自己隔離 以至於它們能被選擇性的放在只讀儲存區,在嵌入式系統。

read-only memory (ROM), in embedded systems



 

 5. Non-RAM storage. If data lives completely outside a program, it can exist while the program is not 

5.非隨機儲存器儲存。如果資料存在於程式之外,它可以存在而程式沒有跑的時候,

running, outside the control of the program. The two primary examples of this are streamed objects, in which objects 

不受程式的控制。  有兩個重要的栗子是流物件,                                                                                     也就是物件被轉換成

are turned into streams of bytes, generally to be sent to another machine, and persistent objects, in which the objects 

位元組流,通常被髮送到另一臺機器,                                        還有一個栗子是永續性物件,

are placed on disk so they will hold their state even when the program is terminated. The trick with these types of 

它們被放在磁碟中,所以儘管程式終止了它們也會保持它們的狀態。

storage is turning the objects into something that can exist on the other medium, and yet can be resurrected into a 

這些型別儲存的技巧是將物件轉換成可以存放在其他媒體上的東西。

regular RAMbased object when necessary. Java provides support for lightweight persistence, and mechanisms such as 

並且能夠在需要的時候重新復活成隨機儲存器型別的物件。Java提供了對輕量級永續性的支援,

JDBC and Hibernate provide more sophisticated support for storing and retrieving object information in databases. 

JDBC  和Hibernate  對資料物件的儲存和讀取提供了更復雜的支援