堆和栈区别(Difference between heap and stack)

Java程序在运行时都要开辟空间,任何软件在运行时都要在内存中开辟空间,Java虚拟机运行时也是要开辟空间的。JVM运行时在内存中开辟一片内存区域,启动时在自己的内存区域中进行更细致的划分,因为虚拟机中每一片内存处理的方式都不同,所以要单独进行管理。

JVM内存的划分有五片:

  1. 寄存器;

  2. 本地方法区;

  3. 方法区;

  4. 栈内存;

  5. 堆内存。

我们重点来说一下堆和栈:

栈内存:

  栈内存首先是一片内存区域,存储的都是局部变量,凡是定义在方法中的都是局部变量(方法外的是全局变量),for循环内部定义的也是局部变量,是先加载函数才能进行局部变量的定义,所以方法先进栈,然后再定义变量,变量有自己的作用域,一旦离开作用域,变量就会被释放。栈内存的更新速度很快,因为局部变量的生命周期都很短。

堆内存:

  存储的是数组和对象(其实数组就是对象),凡是new建立的都是在堆中,堆中存放的都是实体(对象),实体用于封装数据,而且是封装多个(实体的多个属性),如果一个数据消失,这个实体也没有消失,还可以用,所以堆是不会随时释放的,但是栈不一样,栈里存放的都是单个变量,变量被释放了,那就没有了。堆里的实体虽然不会被释放,但是会被当成垃圾,Java有垃圾回收机制不定时的收取。

堆和栈的区别主要有五大点,分别是:

  1、申请方式的不同。栈由系统自动分配,而堆是人为申请开辟;

  2、申请大小的不同。栈获得的空间较小,而堆获得的空间较大;

  3、申请效率的不同。栈由系统自动分配,速度较快,而堆一般速度比较慢;

  4、存储内容的不同。栈是线性存储结构, 存储的都是局部变量和函数参数,凡是定义在方法中的都是局部变量. 而堆是树状存储结构(子节点都大于(小于)父节点的完全二叉树), 存储的是实体对象.

  5、底层不同。栈是连续的空间,而堆是不连续的空间。

————————

Java programs should open up space when running, any software should open up space in memory when running, and Java virtual machine should also open up space when running. When the JVM runs, it opens up a memory area in the memory. When it starts, it makes more detailed division in its own memory area. Because the processing methods of each memory in the virtual machine are different, it should be managed separately.

JVM memory is divided into five pieces:

1. Register;

2. Local method area;

3. Method area;

4. Stack memory;

5. Heap memory.

Let’s focus on heap and stack:

Stack memory:

Stack memory first is a memory area, which stores local variables. All defined in the method are local variables (global variables are outside the method). The internal definition of the for loop is also local variables. The local variables can be defined only after the function is loaded first. The method first defines the stack, and then defines the variables. The variables have their own scope. Once they leave the scope, the variables will be released. The update speed of stack memory is very fast, because the life cycle of local variables is very short.

Heap memory:

Arrays and objects are stored (in fact, arrays are objects). Everything created by new is in the heap. All stored in the heap are entities (objects). Entities are used to encapsulate data and multiple (multiple attributes of entities). If a data disappears, the entity does not disappear and can still be used, so the heap will not be released at any time, but the stack is different. All stored in the stack are single variables, If the variable is released, it’s gone. Although the entities in the heap will not be released, they will be treated as garbage. Java has a garbage collection mechanism to collect them from time to time.

There are five main differences between heap and stack:

1. Different application methods. The stack is automatically allocated by the system, and the heap is opened by human application;

2. Different application sizes. The space obtained by stack is smaller, while the space obtained by heap is larger;

3. Different application efficiency. The stack is automatically allocated by the system, which is fast, while the heap speed is generally slow;

4. Different storage contents. Stack is a linear storage structure, which stores local variables and function parameters. All defined in methods are local variables Heap is a tree storage structure (a complete binary tree in which the child nodes are greater than (less than) the parent node) and stores entity objects

5. The bottom layer is different. Stack is a continuous space, while heap is a discontinuous space.