Java 运行时数据存储的地方

时间:2022-04-26 17:41:54
Where storage lives
It’s useful to visualize some aspects of how things are laid out while the program is running—
in particular how memory is arranged. There are five different places to store data:

1.Registers:cpu的寄存器中

 不同其它的存储, 这是一个更快的存储,因为它存在一个不同的地方:在cpu中.然而,寄存器

的个数被严格限制,因此寄存器当他们需要时才分配,你不能直接控制,你也没有看见任何证据,证明寄存器在你的程序中。

    This is the fastest storage because it exists in a place different from that of
other storage: inside the 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 other hand, allow you to suggest register allocation to the compiler).

  
2. The stack  堆栈

      这个通常存在RAM区,cpu通过stack指针访问它。堆栈指针向下建立新内存,向上释放内存。

这是一个及其快和有效的方法区分配存储,仅次于寄存器。java系统必须知道,当他建立一个程序时,存储在

堆栈中所有的项目准确的生命周期。一些java存储存在堆栈上(具体的数据是对象的references ---java 对象本身是并不子堆栈上)。

       This lives in the general random-access memory (RAM) area, but has
direct support from the processor 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 limits on the flexibility of your
programs, so while some Java storage exists on the stack—in particular, object
references—Java objects themselves are not placed on the stack.
3.The heap 堆(存对象)

       这是一个通用的内存池(同时也在RAM区),而所有的java对象存在的地方。不像堆栈,关于堆好处是:编译器不需要知道数据必须呆在堆上多久时间。因此,在堆上,使用存储器有很大的灵活性。无论什么你需要一个对象,通过使用new去建立,当代码运行时,数据被分配在堆上。当然,你要为这个灵活性付出代价,这个需要花很多时间去分配和删除堆的数据比堆栈。

        This is a general-purpose pool of memory (also in the RAM area) where all
Java objects live. The 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 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 (if you
even could create objects on the stack in Java, as you can in C++).
4. Constant storage:常量值存储

     常量值经常被直接放置在程序代码中,这个是安全的,因为他们从不被改变。有时常量自己隔离,因此他们有选择的放置在ROM,在嵌入系统中。

       Constant values are often placed directly in the program code,
which is safe since 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.2

5. Non-RAM storage. non-RAM 存储

         如果数据完全在一个程序外面,当程序并不运行时,它能存在,在程序的控制之外。

两个主要的例子:1>流对象,在流对象中,所有的对象被转换成字节流,通常被送到另一个机器中,

                                      流对象一般放缓存中后,然后送出,本机就不存在了。

                                  2>持久对象,在持久对象里,对象被放置在磁盘上,因此,当程序中断时,他们将持有对象状态。

这种存储方式的诀窍在于将对象转换为某种东西,这种东西存在其它的媒介中,但必要时可以恢复为常规RAM对象。

java提供了lightweight persistence,比如JDBC和Hibernate 提供了更多的复杂的支持,提供存储和恢复在数据库的对象信息。

        If data lives completely outside a program, it can exist while the  program is not running, outside the control of the program. The two primary  examples of this arestreamed objects, in which objects are turned into streams of  bytes, generally to be sent to another machine, andpersistent 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 RAM based object when necessary. Java provides support for lightweight persistence, and mechanisms such as JDBC and Hibernate provide more sophisticated support for  storing and retrieving object information in databases.