Difference between heap, stack, queue, stack

for example:

In Android Activity, we define the internal class, initialize the internal class object in the main Activity, after exiting the Activity, the variables of the initialized internal object are cleared, but the object still exists, at this time, we only need to define the static object to realize the variable This is the difference between the heap and the stack!


Queues are first-in, first-out: like a road, there is one entrance and one exit, and the one that goes in first gets out first.

   The queue is first-in-first-out, delete operations are performed at the head of the queue, and insertion operations are performed at the end of the queue.

The stack is like a box, which is placed on the top, so last in, first out.

The stack is first in, last out, and insert and delete operations are performed on the top of the stack

Heaps, unlike them, don't have either FIFO or FIFO.

*************************************************************************************************************************************
Each thread in the process has its own stack, which is an address area reserved when a thread is created. Our "stack memory" is here. As for the "heap" memory, I personally think that when it is not defined with new, the heap should be the free space that is not "reserved" and not "committed". The function of new is to reserve (and commit) an address range in these free spaces.
Stack (Stack) is a storage area created by the operating system when a process or thread (in an operating system that supports multi-threading, a thread) is created for this thread. The size of the stack required. In programming, such as C/C++, all local variables are allocated memory space from the stack. In fact, it is not an allocation. It is only used from the top of the stack. When exiting a function, just modify the stack pointer. Destroy the contents of the stack, so it is the fastest. 
Heap is a process that an application requests the operating system to allocate to its own memory when it is running. Generally, it is the process of applying/giving. C/C++ uses malloc/New to request to allocate Heap, and free/delete to destroy memory. Due to the memory allocation managed from the operating system, it takes time to allocate and destroy, so the efficiency of using the heap is much lower! But the advantage of the heap is that it can do a lot, and C/C++ does not initialize the allocated Heap. 
In Java, except for simple types (int, char, etc.), memory is allocated in the heap, which is one of the main reasons for slow programs. But unlike C/C++, the heap memory allocated in Java is automatically initialized. In Java, all objects (including int's wrapper Integer) are allocated in the heap, but the reference to this object is allocated in the Stack. That is to say, when creating an object, memory is allocated from both places. The memory allocated in Heap actually creates the object, while the memory allocated in Stack is just a pointer (reference) to the heap object.

**********************************************************************************************************************************
The heap allocates memory space of a certain size when the program is running, not when the program is compiled. That is, memory is allocated dynamically, and there is no difference between accessing it and accessing general memory. {The heap refers to the dynamic memory that the program runs, and the stack refers to a method of using the heap (ie, first-in, last-out). }

********************************************************************************************************************************
栈是先进后出的,但是于堆而言却没有这个特性,两者都是存放临时数据的地方。 对于堆,我们可以随心所欲的进行增加变量和删除变量,不要遵循什么次序,只要你喜欢。、
********************************************************************************************************************************


从变量存储方面来看堆和栈的区别:

 

*程序的局部变量存在于(栈)中,全局变量存在于(静态区 )中,动态申请数据存在于( 堆)中;

 

*这样说比较准确,所谓的栈其实是由寄存器ebp和esp指向的一片内存空间(ebp指向栈底,esp指向栈顶),原则上是由高地址向低地址生长的一片空间,会保存一些临时的数据,比如一个函数中的临时变量以及返回地址,数据的出入是先进后出,后进先出.

 

*全局变量实际上是存在一个(一般来说正常的编译器)可读可写的内存空间,这个空间是在你写程序编译好的空间地址(由编译器决定),是固定的.

 

*堆是由操作系统管理的一片空间,事先是没有在进程空间里分配的(比如你在没有分配堆的时候就访问堆空间会报一个内存访问错误),一般是由程序动态的分配出来,一旦分配了以后,一般需要程序去释放自己的堆空间.

 

*************************************************************************************************

 

*堆,用于保存new 和malloc这些自定义的内存变量;

*全局静态区用于保存全局变量和静态变量;

*字符常量区,用于保存字符串;

*代码区,用于保存程序的二进制代码;


从变量存储方面来看堆和栈的区别:

 

*程序的局部变量存在于(栈)中,全局变量存在于(静态区 )中,动态申请数据存在于( 堆)中;

 

*这样说比较准确,所谓的栈其实是由寄存器ebp和esp指向的一片内存空间(ebp指向栈底,esp指向栈顶),原则上是由高地址向低地址生长的一片空间,会保存一些临时的数据,比如一个函数中的临时变量以及返回地址,数据的出入是先进后出,后进先出.

 

*全局变量实际上是存在一个(一般来说正常的编译器)可读可写的内存空间,这个空间是在你写程序编译好的空间地址(由编译器决定),是固定的.

 

*堆是由操作系统管理的一片空间,事先是没有在进程空间里分配的(比如你在没有分配堆的时候就访问堆空间会报一个内存访问错误),一般是由程序动态的分配出来,一旦分配了以后,一般需要程序去释放自己的堆空间.

 

*************************************************************************************************

 

*堆,用于保存new 和malloc这些自定义的内存变量;

*全局静态区用于保存全局变量和静态变量;

*字符常量区,用于保存字符串;

*代码区,用于保存程序的二进制代码;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325528942&siteId=291194637