Heap and Stack in Java

Source: Internet
Author: User

What is a heap

Heap memory is a form of Java memory that is used to store objects and arrays in Java, and when we new an object or create an array, it opens up a space in the heap memory for storage.

What are the characteristics of heap memory?

(1): First-out, after-the-back (for example, supermarket line with Bullets magazine).

(2): Heap is reserved for dynamic allocation of memory space, you can dynamically allocate memory size, the lifetime does not have to tell the compiler beforehand, because it is at runtime to allocate memory dynamically, but the disadvantage is that because the runtime to allocate memory dynamically, the access speed is slow. Usually released by the programmer, if the programmer does not release, the program ends may be collected by GC. Each application usually has only one heap.

For heaps, data item locations are not in a fixed order. You can insert and delete in any order

What is a stack

The stack is the memory space that is reserved for the execution thread, and is mainly used to execute the program. For example: basic types of variables and reference variables for objects

What are the features of stack memory?

(1): Advanced after, LIFO. (for example, something placed in a cask, first put in, and then taken out).

(2): The access speed is faster than the heap, second only to the register, the stack data can be shared, but the disadvantage is that the existence of the stack data size and lifetime must be deterministic, lack of flexibility.

When you create a variable on the stack, it expands and is automatically recycled. For the stack, the new data item in the stack is placed at the top of the other data, and you can only remove the topmost data (not overrun) when you remove it.

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.