What is a heap
Heap memory is a form of Java memory that is used to store objects and arrays in Java, and when we new an object or create an array, it opens up a space in the heap memory for storage.
What are the characteristics of heap memory?
(1): First-out, after-the-back (for example, supermarket line with Bullets magazine).
(2): Heap is reserved for dynamic allocation of memory space, you can dynamically allocate memory size, the lifetime does not have to tell the compiler beforehand, because it is at runtime to allocate memory dynamically, but the disadvantage is that because the runtime to allocate memory dynamically, the access speed is slow. Usually released by the programmer, if the programmer does not release, the program ends may be collected by GC. Each application usually has only one heap.
For heaps, data item locations are not in a fixed order. You can insert and delete in any order
What is a stack
The stack is the memory space that is reserved for the execution thread, and is mainly used to execute the program. For example: basic types of variables and reference variables for objects
What are the features of stack memory?
(1): Advanced after, LIFO. (for example, something placed in a cask, first put in, and then taken out).
(2): The access speed is faster than the heap, second only to the register, the stack data can be shared, but the disadvantage is that the existence of the stack data size and lifetime must be deterministic, lack of flexibility.
When you create a variable on the stack, it expands and is automatically recycled. For the stack, the new data item in the stack is placed at the top of the other data, and you can only remove the topmost data (not overrun) when you remove it.
Heap and Stack in Java