In data structures, heaps and stacks can be said to be the two most basic data structures, and what are the similarities and differences between stack memory space and heap memory space in Java, and what is the relationship to the stack in the data structure?
One, Java heap storage space
Heap memory (heap storage space) is assigned to a class of objects (object) or JRE at Java Runtime. As soon as we create an object, the heap will definitely allocate a chunk of storage space to the object. The Java garbage collection we know is done on heap storage to free objects that don't have any references to themselves. Any object allocated in the heap has global access and can be referenced from anywhere in the application.
Second, Java stack storage space
The Java stack storage space is used for thread execution. The stack space contains special variables such as short life cycles and references to other objects in the heap. The stack storage space here satisfies the order of LIFO. When a function is called, a new storage space is allocated to the stack, which holds the basic data of the function ("Java experience summarizes a" Java basic type and wrapper type resolution) and references to other objects in the function. Once the function execution is finished, the storage space is freed for use by the next function.
Stack storage space is much smaller than heap storage space
Iii. examples
Memory.java
1 public class Memory {2 3 publicly static void main (string[] args) {//Line 1 4 int. I=1;//Line 2 5 Obje CT obj = new Object (); Line 3 6 Memory mem = new Memory (),//Line 4 7 mem.foo (obj), line 5 8 }//Line 9 9 Private void foo (Object param) {//Line 611 String str = param.tostring (),////line 712 System.out.println (str); 13
}//Line 814 15}
Shows how stack storage spaces store basic types, objects, and references to variables
Program execution Process:
- Once we start running the program, it loads all the runtime classes into the heap storage space. When the program runs to the first line of the main () function, Java runtime allocates stack storage space for the main function thread.
- We created the base data type on the second line, so it will be stored in the stack storage space of the main function thread;
- Because we created the object in the third line, it is created in the heap, and a reference to it is saved in the stack space. Similarly, line fourth creates a memory object.
- When we call the Foo () function in line fifth of Main (), a space is allocated at the top of the stack space for use by the Foo () function. Because Java is a value pass (Java is passed as a value instead of a reference pass), a new reference is created on line sixth of the Foo function that points to the object in the heap
- A string is created on line 7th, which is placed in the string pool of heap space (string Poll), and a reference to it is saved in the stack space.
- In line 8th, the Foo function is completed and its stack space is freed
- In line 9th The main function is finished, and the space allocated to the main function in the stack is freed. The program also executes in this line, so the Java runtime (Java Runtime) frees up all memory space and terminates the execution of the program.
Iv. stack similarities and differences
- The heap storage space can be used by any part of the application, while the stack storage space is only used by the corresponding execution thread
- Once an object is created, it is allocated a storage space in the heap and a reference to it is kept in the stack space. Only the basic data types and reference variables that point to the objects in the heap are saved in the stack space
- The objects stored in the heap can be accessed globally, but the variables stored in the stack cannot be accessed by other threads (functions)
- Memory management in the stack takes the advanced back-out (LIFO) approach, but memory management in the heap is more complex because the objects in the heap can be used globally. The storage space is divided into young-generation,old-generation and so on, this I will summarize after
- Stack storage is a short life cycle, but heap storage is much longer
- Use-XMS and-xmx to indicate the heap initial and maximum space of the JVM, using-XSS to define the stack space size
- When the stack space is full, the Java runtime throws Java.lang.StackOverFlowError, but the heap is full and throws a Java.lang.OutOfMemoryError:Java heap space error
- Stack space is very small compared to heap space, and because the stack uses the simplest advanced post-out (LIFO) principle, it is much faster than the heap.
Reference: https://www.cnblogs.com/xltcjylove/p/4502859.html
Java heap memory vs. stack memory