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  对数据对象的存储和读取提供了更复杂的支持

 

猜你喜欢

转载自blog.csdn.net/Cute_Code/article/details/80275041