On stack and heap in Java

People often say that stack stack and stack are two different places in memory. What kind of data exists in the stack and what kind of data exists in the heap?

Here we talk about stack and heap in Java

First, write the conclusion in the front, and then verify it with an example.

The following types of data are stored in the Java stack. The English word corresponding to the stack is stack

Basic type

Reference type variable

method

The advantage of stack is that the access speed is faster than heap, second only to register, and stack data can be shared. However, the disadvantage is that the data size and lifetime in the stack must be determined and lack of flexibility.

The stack mainly stores some basic types of variables (int, short, long, byte, float, double, Boolean, char) and object handles.

Stack has a very important particularity, that is, the data in the stack can be shared.

The following types of data are stored in the Java heap. The English word corresponding to the heap is heap

Instance object

Some basic types of variables (8 kinds) and object reference variables defined in the function are allocated in the stack memory of the function stack. When a variable is defined in a code block, Java allocates memory space for the variable in the stack. When the scope of the variable is exceeded, Java will automatically release the memory space allocated for the variable, which can be used for other purposes immediately.

Heap heap memory is used to store objects and arrays created by new. The memory allocated in the heap is managed by the Java virtual machine automatic garbage collector. After an array or object is generated in the heap, a special variable can also be defined in the stack. The value of this variable is equal to the first address of the array or object in the heap memory. This special variable in the stack becomes the reference variable of the array or object, and then the reference variable in the stack memory can be used in the program to access the array or object in the heap, A reference variable is equivalent to an alias or code for an array or object.

Reference variables are ordinary variables. When defined, memory is allocated in the stack, and reference variables are released outside the scope of the program. The array & object itself is allocated in the heap. Even if the program runs outside the code block where the statement generating the array and object using new is located, the heap memory occupied by the array and object itself will not be released. The array and object become garbage and can no longer be used, but still occupy memory when there is no reference variable pointing to it, It is released by the garbage collector at an uncertain time. This is also the main reason why Java occupies more memory. In fact, the variables in the stack point to the variables in the heap memory, which is the pointer in java!

 class Person {

int age;

}

public class LearnHeap {

public static void main(String args[]){

    int a=10;
    Person person = new Person();
    person.age =20;

    change(a,person);
    Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("a="+ a+",and person.age = "+person.age);

}

static void change(int a1,Person person){

    a1 = 11;
    person.age= 21;
    Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("a1="+ a1+",and age1 = "+person);

}

}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>