Java 内存分配——Thinking in Java 4th 读书笔记
做開(kāi)發(fā)多年,一直忙于項(xiàng)目,從沒(méi)好好的整理知識(shí),從現(xiàn)在開(kāi)始,盡量每周多抽時(shí)間整理知識(shí),分享在博客,在接下來(lái)的博客中,我將為大家分享我讀《Java編程思想4th》英文版讀書(shū)筆記,一來(lái)便于知識(shí)的梳理,二來(lái)分享給需要的朋友,評(píng)價(jià)很高的一本書(shū),推薦大家閱讀,因?yàn)闀?shū)的邊幅比較長(zhǎng),如果不想閱讀整本書(shū)歡迎來(lái)我的博客,我會(huì)定期分享書(shū)的重點(diǎn)內(nèi)容,以翻譯的形式展現(xiàn)。
There are five different places to store data:?
存儲(chǔ)數(shù)據(jù)的地方有5個(gè)
1. Registers. This is the fastest storage because it exists in a place different from that of other storage: inside the?
1.寄存器。這是最快的存儲(chǔ),因?yàn)樗嬖诘奈恢门c其他的存儲(chǔ)方式有別:它位于處理器中。
processor. However, the number of registers is severely limited, so registers are allocated as they are needed. You?
然而,寄存器的數(shù)量是嚴(yán)格限制的,所以寄存器是當(dāng)它需要時(shí)才分配的。
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.棧。它位于通用隨機(jī)存儲(chǔ)器區(qū)域,但可以通過(guò)它的棧指針從處理器那里獲得直接的支持。
via its stack pointer. The stack pointer is moved down to create new memory and moved up to release that memory.?
棧指針向下移動(dòng)是創(chuàng)建新的存儲(chǔ)空間,向上移動(dòng)是釋放存儲(chǔ)空間。
This is an extremely fast and efficient way to allocate storage, second only to registers. The Java system must know,?
這是一種十分快速并且有效的方法去分配內(nèi)存,僅次于寄存器 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
while it is creating the program, the exact lifetime of all the items that are stored on the stack. This constraint places?
當(dāng)創(chuàng)建一個(gè)程序時(shí),Java系統(tǒng)必須知道存儲(chǔ)在棧上所有項(xiàng)的準(zhǔn)確生命周期。 ? ? ? ? ? ? ? ? ?
limits on the flexibility of your programs, so while some Java storage exists on the stack—in particular, object?
這個(gè)約束一定程度上限制了你的程序的靈活性,所以當(dāng)一些Java存儲(chǔ)在棧上時(shí),特別是對(duì)象的引用——Java對(duì)象本身
references—Java objects themselves are not placed on the stack. ?
并沒(méi)有存放在堆棧上
3. The heap. This is a general-purpose pool of memory (also in the RAM area) where all Java objects live. The?
3.堆。 這是通用目的的存儲(chǔ)池(同樣是在隨機(jī)存儲(chǔ)區(qū)域),也就是Java對(duì)象存儲(chǔ)的地方,
nice thing about the heap is that, unlike the stack, the compiler doesn’t need to know how long that storage must?
不像棧那樣,堆的一個(gè)好處是,編譯器并不需要知道數(shù)據(jù)在堆上存儲(chǔ)的時(shí)間。
stay on the heap. Thus, there’s a great deal of flexibility in using storage on the heap. Whenever you need an object,?
因此,使用堆來(lái)存儲(chǔ)擁有極大的靈活性。 ? ?當(dāng)你需要一個(gè)對(duì)象(不是指你想相處的對(duì)象,開(kāi)個(gè)小玩笑),
you simply write the code to create it by using new, and the storage is allocated on the heap when that code is?
你只需要簡(jiǎn)單的通過(guò)new 來(lái)創(chuàng)建它, 并且當(dāng)代碼運(yùn)行的時(shí)候內(nèi)存才會(huì)在堆上分配。
executed. Of course there’s a price you pay for this flexibility: It may take more time to allocate and clean up heap?
當(dāng)然,擁有靈活性是有代價(jià)的,它可能比棧花更多的時(shí)間去分配和清理堆內(nèi)存。
storage than stack storage . ?
4. Constant storage. Constant values are often placed directly in the program code, which is safe since?
4.常量存儲(chǔ)。 ?常量值經(jīng)常被放在程序代碼里, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 這是安全的
they can never change. Sometimes constants are cordoned off by themselves so that they can be optionally placed in?
因?yàn)樗鼈冇肋h(yuǎn)不會(huì)改變。 ?有時(shí)候常量被它們自己隔離 以至于它們能被選擇性的放在只讀存儲(chǔ)區(qū),在嵌入式系統(tǒng)。
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.非隨機(jī)存儲(chǔ)器存儲(chǔ)。如果數(shù)據(jù)存在于程序之外,它可以存在而程序沒(méi)有跑的時(shí)候,
running, outside the control of the program. The two primary examples of this are streamed objects, in which objects?
不受程序的控制。 ?有兩個(gè)重要的栗子是流對(duì)象, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 也就是對(duì)象被轉(zhuǎn)換成
are turned into streams of bytes, generally to be sent to another machine, and persistent objects, in which the objects?
字節(jié)流,通常被發(fā)送到另一臺(tái)機(jī)器, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?還有一個(gè)栗子是持久性對(duì)象,
are placed on disk so they will hold their state even when the program is terminated. The trick with these types of?
它們被放在磁盤(pán)中,所以盡管程序終止了它們也會(huì)保持它們的狀態(tài)。
storage is turning the objects into something that can exist on the other medium, and yet can be resurrected into a?
這些類(lèi)型存儲(chǔ)的技巧是將對(duì)象轉(zhuǎn)換成可以存放在其他媒體上的東西。
regular RAMbased object when necessary. Java provides support for lightweight persistence, and mechanisms such as?
并且能夠在需要的時(shí)候重新復(fù)活成隨機(jī)存儲(chǔ)器類(lèi)型的對(duì)象。Java提供了對(duì)輕量級(jí)持久性的支持,
JDBC and Hibernate provide more sophisticated support for storing and retrieving object information in databases.?
JDBC ?和Hibernate ?對(duì)數(shù)據(jù)對(duì)象的存儲(chǔ)和讀取提供了更復(fù)雜的支持
?《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專(zhuān)家共同創(chuàng)作,文字、視頻、音頻交互閱讀
總結(jié)
以上是生活随笔為你收集整理的Java 内存分配——Thinking in Java 4th 读书笔记的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Android 四大组件之——Servi
- 下一篇: Java 面向对象四大特性