Stack and heap differences

Source: Internet
Author: User

First of all clear stack,heap Chinese translation: stack-stack, heap-heap. In Chinese, the stack can be translated as "stacks", so I looked directly at the words in the computer terminology that were at the beginning of the heap and stack: Heap Storage: Heapstorage heap Storage allocation: Heapstorage allocation heap Storage management: Heap Storage Management stack address: Stack addressing stack transform: Stack transformation stack memory: Stack memory stack unit: Stack cellNext, summarize the differences in Java where the heap and stack store data separately.
Heap (heaps) Stack (Stack)
Features in the JVM Memory Data area Memory Instruction Area
Storing data Object Instances (1) Basic data types, instruction codes, constants, reference addresses for objects (2)
1. Saving an object instance is actually a property value that holds the object instance, the type of the property, and the type tag of the object itself, and does not save the object's method (the method is the instruction, saved in the stack).

After the object instance is allocated in the heap, you need to save a 4-byte heap memory address in the stack to locate the object instance in the heap so that it is easy to locate the object instance. 2. The basic data types are byte, int, char, long, float, double, Boolean, and short.
A function method belongs to an instruction. ======================= cited the widespread "Java heap and stack differences" in the face of heap and stack in the introduction; "The Java heap is a run-time data area where the objects allocate space from. These objects are established 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, 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 use data. However, the disadvantage is that the access speed is slower due to the dynamic allocation of memory at run time. " "The advantage of the stack is that the access speed is faster than the heap, after the register, the stack data can be shared." However, the disadvantage is that the size and lifetime of the data in the stack must be deterministic and inflexible. 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, 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!

Stack and heap differences

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.