Java Heap and Stack

Source: Internet
Author: User

Heap (Stack) (FIFO):

The heap is a run-time data area from which the object of the class allocates space. These pairs are created through directives such as new, NewArray, Anewarray, and Multianewarray , and they do not require program code to be explicitly released. Heap is responsible for garbage collection (GC), the advantage of the heap is the ability to dynamically allocate memory size, the lifetime does not have to tell the compiler beforehand, because it is at runtime to allocate memory dynamically, Java garbage collector will automatically take away these no longer used data. However, the disadvantage is that the access speed is slower due to the dynamic allocation of memory at run time .

Stack (stack) (FILO):

Stack accesses faster than the heap, second only to registers. Data in the stack can be shared. The base data type and object handle are mainly stored in the stack. The disadvantage is that the data size and life cycle in the stack are deterministic and inflexible.

The biggest feature of the stack is data sharing. That is, what is often referred to as " there is a point, none is created ". It is important to note that the data share of the stack is different from the data share of the object reference to the same object, and there is no effect between the sharing of the previous stack, that is, modifying the value of one variable does not affect the value of the other variable. The object's cause is affected by other objects. The stack mainly contains some basic types of variables (, int, short, long, byte, float, double, Boolean, char) and object handle.

As can be seen, garbage collection GC is for heap heap, and stack because itself is filo (first in the last out). Advanced after out, can be automatically released. So that you can see that the new one was created and put into the heap heap!

The Heap is also divided into young, old, permanet.

Java Heap and Stack

Related Article

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.