[Java] Differences Between Stack and heap (final)

Source: Internet
Author: User
First, we should clearly understand the stack. The Chinese Translation of heap: Stack-stack, heap-heap. In Chinese, stack can be translated as "stack", so I directly searched for the words heap and start with Stack in computer terms: Heapstorage: heapstorage Heap Storage Allocation: heapstorage Allocation Heap Storage Management: Heap Storage Management Stack address: Stack addressing stack Transformation: Stack transformation stack storage: stack memory stack unit: Stack CellNext, we will summarize the differences between heap and stack in Java that store data separately.
Heap) Stack)
Features in JVM Memory Data Zone Memory Command Area
Store Data Object instance (1) Basic data type, command code, constant, Object Reference address (2)
1. the method for saving an object instance is actually to save the property value of the object instance, the type of the property and the type mark of the object itself, and not to save the object (the method is instruction and saved in stack ).

After the object instance is allocated in heap, a 4-byte heap memory address needs to be saved in the stack to locate the location of the object instance in heap, so as to locate the object instance easily. 2. Basic data types include byte, Int, Char, long, float, double, Boolean, and short.
The function method is a command. = stack introduction; "Java heap is a runtime data zone, class (the object allocates space from it. These objects are created using commands such as new, newarray, anewarray, and multianewarray. They do not need program code to be explicitly released. The heap is responsible for garbage collection. The advantage of the heap is that the memory size can be dynamically allocated, and the lifetime does not have to be told in advance because the heap dynamically allocates memory at runtime, the Java Garbage Collector automatically collects the unused data. However, the slow access speed is due to the need to dynamically allocate memory during runtime. " "The advantage of stack is that the access speed is faster than that of stack, 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 fixed, and there is a lack of flexibility. The stack mainly stores some basic types of variables (, Int, short, long, byte, float, double, Boolean, char) and object handles.  " It can be seen that the garbage collection GC is for heap, And the stack is automatically released because it is Filo-first in and last out. In this way, we can understand that all new products are created in heap!

This article from the "Learning android" blog, please be sure to keep this source http://android.blog.51cto.com/268543/50100

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.