Java heap and stack

Source: Internet
Author: User

The main storage variables in the stack memory. When the lifecycle of a variable is reached, the variable is automatically released. Java heap memory mainly stores arrays and objects, and the lifecycle will become garbage, waiting for the garbage collection mechanism to recycle. Variables of some basic types defined in the function and referenced variables of the object are allocated in the function stack memory. WhenCodeWhen a block defines a variable, Java allocates memory space for the variable in the stack. When the scope of the variable is exceeded, Java Automatically releases the memory space allocated for the variable, the memory space can be used by another user immediately.
Heap memory is used to store objects and arrays created by new. The memory allocated in the heap is managed by the Java Virtual Machine's automatic garbage collector. After an array or object is generated in the heap, you can define a special variable in the stack so that the value of this variable in the stack is equal to the first address of the array or object in the heap memory, the variable in the stack becomes the referenced variable of the array or object. The referenced variable is equivalent to an array or object name.ProgramTo access the array or object in the heap. 

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.