Home  >  Article  >  Java  >  What is the difference between heap and stack

What is the difference between heap and stack

青灯夜游
青灯夜游Original
2022-11-22 16:12:509520browse

Difference: 1. The heap space is generally allocated and released by the programmer; while the stack space is automatically allocated and released by the operating system. 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; while the stack uses the first-level cache, which is usually in the storage space when it is called, and is released immediately after the call is completed. 3. The data structures are different. Heap can be regarded as a tree, while stack is a first-in, last-out data structure.

What is the difference between heap and stack

The operating environment of this tutorial: windows7 system, java8 version, DELL G3 computer.

The concepts of heap and stack:

Stacks are two data structures. A stack is a data structure in which data items are arranged in order. Data items can only be inserted and deleted at one end (called the top of the stack). In microcontroller applications, the stack is a special storage area whose main function is to temporarily store data and addresses, and is usually used to protect breakpoints and scenes. Key points: Heap, queue priority, first in first out (FIFO—first in first out). Stack, first in, last out (FILO—First-In/Last-Out).

The difference between heap and stack:

1. Difference in 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.

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.

Stack and heap in Java:

Stack and heap are places used by Java to store data in Ram. Unlike C, Java automatically manages the stack and heap, and programmers cannot directly set the stack or heap.
Some basic types of variables and object reference variables defined in the function are allocated in the stack memory of the function. When a variable is defined in a block of code, Java allocates memory space for the variable on the stack. When the scope of the variable is exceeded, Java will automatically release the memory space allocated for the variable, and the memory space can be immediately used. Use it for other purposes.
Heap memory is used to store objects and arrays created by new. The memory allocated in the heap is managed by the automatic garbage collector of the Java virtual machine. After an array or object is generated in the heap, you can also define a special variable in the stack so that the value of the variable in the stack is equal to the first address of the array or object in the heap memory. The variable in the stack becomes A reference variable to an array or object. A reference variable is equivalent to giving a name to an array or object. You can then use the reference variable in the stack to access the array or object in the heap in the program.

Allocation of variables in memory in Java:

1. Class variables (statically modified variables): When the program is loaded, the system will open it in the heap The memory addresses in the heap are stored on the stack for high-speed access. The lifetime of a static variable – lasts until the entire “system” is shut down.

 2. Instance variables: When you use the java keyword new, the system allocates space in the heap that is not necessarily continuous to variables (such as class instances), and then based on scattered heap memory addresses , converted into a long string of numbers through a hash algorithm to represent the "physical location" of this variable in the heap. Life cycle of instance variables – When the reference to an instance variable is lost, it will be included in the recyclable “list” by the GC (garbage collector), but the memory in the heap will not be released immediately.

 3. Local variables: Local variables are declared in a method or a certain code segment (such as a for loop). When it is executed, memory is allocated on the stack. Once the local variable goes out of scope, , the memory is released immediately.

This involves Java memory issues, you can refer to: Java’s memory mechanism

Recommended tutorial: "java tutorial"

The above is the detailed content of What is the difference between heap and stack. 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