Heap and Stack in Java

Source: Internet
Author: User

First, stack

The advantage of the stack is that the access speed is faster than the heap, next to the register directly in the CPU, when the scope of the variable is exceeded, Java automatically frees up the allocated memory space for the variable, and the memory space can be used for other purposes immediately. However, the disadvantage is that the size and lifetime of the data in the stack must be deterministic and inflexible. Stack data can be shared.

Which data is stored in the stack?

Variables for the base data type (int, short, long, byte, float, double, Boolean, char) are stored in the stack.

The reference to the object is stored in the stack.

What does stack data sharing mean exactly?

int a = 3; int b = 3;
The compiler processes int a = 3 First, it creates a reference to a variable in the stack, and then looks for an address with a literal value of 3, finds an address that holds the literal value of 3, and then points A to the address of 3. then the int b = 3 is processed, and after the reference variable of B is created, B is pointed directly to the address of 3 because there are already 3 literals in the stack. In this case, A and B both point to 3. That is to share 3 of this stack data.

In addition, a special string, although the string type is not a simple basic data type, but when the string a= "ABC" syntax to define a string variable, the data will be stored in the stack, so there will be the above mentioned data sharing characteristics.

Second, the heap

The advantage of the heap is that the memory size can be allocated dynamically, and the lifetime does not have to tell the compiler beforehand. Heap data becomes garbage when no reference variable points to it, is no longer used, but still occupies memory and is released by the garbage collector at a later indeterminate time. However, the disadvantage is that the access speed is slower due to the dynamic allocation of memory at run time.

Which data are stored in the heap?

Heap memory is used to hold objects and arrays created by new, that is, the heap is primarily used to store objects. The storage space for all objects in Java is allocated in the heap, but the reference to this object is allocated on the stack, that is, allocating memory from two places when an object is built, memory allocated in the heap actually establishes the object, and the memory allocated on the stack is just a pointer to the heap object (reference) Only.

Heap and Stack in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.