Stack and Heap in JAVA

JAVA divides 5 pieces of space in the memory for data storage when the program is running. They are: 1: Register. 2: Local method area. 3: Method area. 4: Stack. 5: heap.

Basically, the two concepts of stack and heap are very important. If you don't understand it clearly, you don't need to learn it later.

The following is the learning record and experience of stack and heap these days. Get some records. As I learn new things in the future, I will add them gradually.

 

First, let’s talk about the most basic points

Basic data types and local variables are stored in the stack memory and disappear when they are used up.
The instantiated objects and arrays created by new are stored in the heap memory, and are automatically eliminated from time to time by the garbage collection mechanism after they are used up.

 

 

Second, first clarify the above two points, the following example is better to understand


Example 1

main()
  int x=1;
show ()
  int x=2

The variable int x=1 is defined in the main function main(), and the variable int x=1 is defined in the show() function. Finally, the show() function is executed.


Steps to perform the above procedure:

Step 1 - The main() function is the program entry. The JVM executes it first, and opens up a space in the stack memory to store the int type variable x and attach the value 1 at the same time.
Step 2 - JVM executes the show() function, and opens up a new space in the stack memory to store the int type variable x and attach the value 2 at the same time.
     At this time, the main space and the show space coexist and run at the same time without affecting each other.
Step 3 - After show() is executed, the variable x is released immediately and the space disappears. But the main() function space still exists, and the variable x in main still exists and is not affected.

The schematic diagram is as follows:

 

——————————————————————————————————————————————————————————————————————

Example 2

main()
  int[] x=new int[3];
  x[0]=20

The main function main() defines an array x, the element type is int, and the number of elements is 3.

Step 1 of the above program execution steps
- execute int[] x=new int[3];
  hide the following branches
  JVM executes the main() function, opens up a space in the stack memory, and stores the x variable (the x variable is a local variable) .
  At the same time, a space is also opened in the heap memory to store the new int[3] array, and the heap memory will automatically store the first address value, such as 0x0045.
  The address value of the array in the stack memory will be attached to x, so that x also has an address value. So, x points to (references) this array. At this point, all elements are unvalued, but all have a default initialization value of 0.

Step 2 - Execute x[0]=20
  to append 20 to the array element [0] in heap memory. In this way, the three elements of the array have values ​​20,0,0

The diagram is as follows:

——————————————————————————————————————————————————————————————————————


Example 3
main()
  int[] x=new int[3];
  x[0]=20
  x=null;

The above steps perform steps
1 and 2 - exactly the same as in Example 2, omitted.

Step 3 - Execute x=null;
  null represents a null value, that is, the reference array memory address 0x0045 of x is deleted, and it no longer points to the array in the stack memory. At this time, the array in the heap is no longer used by x, that is, it is regarded as garbage, and the JVM will start the garbage collection mechanism and automatically delete it from time to time.

The diagram is as follows

——————————————————————————————————————————————————————————————————

示例4
main()
  int[] x=new int[3];
  int[] y=x;
  y[1]=100
  x=null;

Perform the steps above

Step 1 - Same as Step 1 of Example 2, omitted.
Step 2 - Execute int[] y=x,
  define a new array variable memory y in the stack memory, and attach the value of x to y at the same time. So, y also points to the same array in heap memory.
Step 3 - Execute y[1]=100
  to append 20 to the array element [0] in heap memory. In this way, the values ​​of the three elements of the array are 0, 100, 0 respectively
. Step 4 - If x=null
  is executed, the variable x no longer points to the array in the stack memory. However, the variable y still points to, so the array doesn't disappear.

The diagram is as follows

——————————————————————————————————————————————————————————————————

Example 5

Car c=new Car;
c.color=”blue”;
Car c1=new Car;
c1.num=5;

 Although both objects refer to new Car, they are two different objects. Every time new, a different entity is generated

 

——————————————————————————————————————————————————————————————————

Example 6

Car c=new Car;
c.num=5;
Car c1=c;
c1.color=”green”;
c.run();

Car c1=c, this sentence is equivalent to copying the object, and the memory address value of the two objects is the same. Therefore, pointing to the same entity, modifying the attributes of c1 is equivalent to changing the attributes of c.

 

 

Third, the characteristics of stack and heap

stack:

The basic type variables defined in the function and the reference variables of the object are allocated in the stack memory of the function.
The stack memory features, as soon as the data is executed, the variable will be released immediately, saving memory space.
The data in the stack memory has no default initialization value and needs to be set manually.

 

heap:

Heap memory is used to store objects and arrays created by new.
All entities in heap memory have memory address values.
Entities in heap memory are used to encapsulate data, which have default initialization values.
When the entity in the heap memory is no longer pointed to, the JVM starts the garbage collection mechanism and automatically clears it, which is also one of the performances of JAVA superior to C++ (in C++, the programmer needs to be manually cleared).

 

Note:

What are local variables: variables defined in functions, variables defined on parameters in functions, variables defined inside for loops

 

Reprinted from: https://www.cnblogs.com/iblieve618/p/6380328.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324613559&siteId=291194637