Heap memory and stack memory in Java

Source: Internet
Author: User

Java divides the memory into two types:

A kind of called stack memory
A heap of memory called

Stack Memory : Some basic types of variables and object reference variables defined in the function are allocated in the stack memory of the function. When a variable is defined in a block of code, Java allocates a memory space for the variable in the stack, and when the scope of the variable is exceeded, Java automatically frees the memory space allocated for that variable, which can be used immediately by another.

heap Memory : Heap memory is used to hold objects and arrays created by new . The memory allocated in the heap is managed by the Java Virtual Machine automatic garbage collector. In arrays and objects, when no reference variable points to it, it becomes garbage, can no longer be used, but still occupies memory and is freed by the garbage collector at a later indeterminate time. This is also the main reason why the Java comparison occupies memory.

According to the compiling principle, there are three kinds of strategies for memory allocation when the program runs, which are static, stacked, and stacked.

static storage allocation means that at compile time you can determine the storage space requirements for each data target at run time, so that they can be allocated a fixed amount of memory at compile time.

stack Storage allocations can also be called dynamic storage allocations, which require that all storage requirements be known at the entrance of the process, while heap storage allocations are specifically responsible for memory allocations of data structures that are not determined at compile time or at runtime at the module entrance.

From the heap and stack function and the role of the popular comparison, the heap is mainly used to store objects, the stack is mainly used to execute the program.

Heap memory and stack memory 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.