Home  >  Article  >  Stack difference

Stack difference

藏色散人
藏色散人Original
2019-06-03 14:07:3027330browse

Stack difference

The difference between heap and stack:

1. The difference between stack space allocation:

1. Stack (operating system): automatically allocated and released by the operating system to store function parameter values, local variable values, etc. Its operation method is similar to the stack in the data structure;

2. Heap (operating system): It is generally allocated and released by the programmer. If the programmer does not release it, it may be recycled by the OS when the program ends. The allocation method is similar. in the linked list.

2. Differences in stack caching methods:

1. The stack uses the first-level cache. They are usually in the storage space when they are called, and immediately after the call is completed. Release;

2. The heap is stored in the second-level cache, and the life cycle is determined by the garbage collection algorithm of the virtual machine (not that it can be recycled once it becomes an orphan object). Therefore, the speed of calling these objects is relatively low.

Heap: In memory, reference data types are stored. The size of reference data types cannot be determined. The heap is actually a storage space in a linked list structure that uses scattered space in the memory. The size of the heap is determined by The size of the reference type is directly determined. Changes in the size of the reference type directly affect the changes in the heap

Stack: It is a value type stored in the memory. The size is 2M. If it exceeds, an error will be reported and the memory will overflow

3. Differences in stack data structure:

Heap (data structure): The heap can be regarded as a tree, such as: heap sort;

Stack (data structure): A first-in, last-out data structure.

Features: first in, last out

For first in, last out, because the underlying implementation of Linklist is a linked list structure, we use Linklist to explore what is first in, last out

The code is as follows:

package com.zking.list;
 
import java.util.LinkedList;
 
public class Linklisttest {
 
public static void main(String args[]) {
LinkedList ll = new LinkedList();
for (int i = 0; i < 5; i++) {
ll.addFirst(i);
}
 
ll.removeFirst();
 
for (Object object : ll) {
System.out.println(object);
}
 
}
 
}

Running result:

Stack difference

According to this simple example, we can see that through In the for loop, the order in which we add data to the collection is 0, 1, 2, 3, 4,

. When the delete method is executed, 4 is deleted, and the final traversal result is 3, 2, 1, 0.

So it reflects the characteristics of the stack: first in, last out. .

The above is the detailed content of Stack difference. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn