[Usage] Java interview questions to explain the memory stack (stack), Heap (heap) and static storage area

: Java interview questions Explain the use of the memory stack (stack), Heap (heap) and static storage area

Heap area: dedicated to save the instance (created new objects and arrays) object, in fact, only the object instance stored attribute value, the attribute type and the type of the object itself and other markers not (method objects is stored instructions stored in the Stack)

1.存储的全部是对象,每个对象都包含一个与之对应的class的信息。(class的目的是得到操作指令) 2.jvm只有一个堆区(heap)被所有线程共享,堆中不存放基本类型和对象引用,只存放对象本身. 3.一般由程序员分配释放, 若程序员不释放,程序结束时可能由OS回收 。 

 

Stack area: good distribution in the object instance after the Heap, needs to be saved in the Stack Heap a 4-byte memory address used to locate the position of the object instance in the Heap, easy to locate the object instance.

1.每个线程包含一个栈区,栈中只保存基础数据类型的对象和自定义对象的引用(不是对象),对象都存放在堆区中
2.每个栈中的数据(原始类型和对象引用)都是私有的,其他栈不能访问。 3.栈分为3个部分:基本类型变量区、执行环境上下文、操作指令区(存放操作指令)。 4.由编译器自动分配释放 ,存放函数的参数值,局部变量的值等. 

 

Static Area / method area:

1.方法区又叫静态区,跟堆一样,被所有的线程共享。方法区包含所有的class和static变量。
2.方法区中包含的都是在整个程序中永远唯一的元素,如class,static变量。 3.全局变量和静态变量的存储是放在一块的,初始化的全局变量和静态变量在一块区域, 未初始化的全局变量和未初始化的静态变量在相邻的另一块区域。 

 


Attachment:

Heap and stack is the key to the program is running, it should be understood clearly.
Here Insert Picture Description
Stack the unit is running, and when the heap is a unit of storage.

Heap memory is the object. The stack is stored basic data types and references to objects in a heap. The size of an object is not estimated, or can be dynamically changed, but the stack, only one object corresponds to the reference of a 4btye (stack separation benefits).

Why should distinguish between heap and stack out of it? The stack can not store data?

First, from the point of view of software design, stack represents processing logic, and data representing the heap. This separation, so that the processing logic clearer. The idea of divide and rule. This isolation, modular thinking is reflected in all aspects of software design.
Second, the isolated stack and the stack, the stack contents so that a plurality of stacks may be shared (to be understood as a plurality of threads to access the same object). This sharing benefits are many. On the one hand this provides an efficient sharing of data interaction (such as: shared memory), on the other hand, heap cache can be shared and constant access to all of the stack, saving space.
Third, because of the need runtime stack, such as saving the context of the system is running, we need to divide the segment. Since the stack can only grow up, so will be limits on the ability to stack storage content. Heaped different objects in the heap can be increased dynamically as needed, stack and heap split, so that it becomes possible to grow dynamically, a record only the corresponding address in the stack to stack.
Fourth, it is the perfect combination of object-oriented heap and stack. In fact, object-oriented programming with the way the previous structure of the program does not make any difference in the implementation. However, the introduction of object-oriented, so that thinking approach to the problem has changed, but more natural way of thinking is close. When we open the object, you will find that the object's properties is actually the data stored on the heap; and the behavior of the object (method), is to run the logic on the stack. When we write object, in fact that is the preparation of the data structure, logic also written to process data. Have to admit, object-oriented design, really beautiful.

Guess you like

Origin www.cnblogs.com/insist-bin/p/11109423.html