Java heap memory, stack memory

Source: Internet
Author: User

Stack memory

The stack memory stores a variable of the base type (int a = 3;) and a variable of the reference type (car car). When a variable is defined in a block of code, Java allocates 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. The memory space can be used for other purposes immediately.

Heap Memory

Heap memory holds objects and arrays created by new, such as new auto;new int []; A reference variable is a normal variable that is defined when memory is allocated in the stack, and the reference variable is released in the program run to the extraterritorial scope. The array and object itself is allocated in the heap, and even if the program runs beyond the block of code that uses new to produce arrays and objects, the heap memory used by the array and the object itself will not be freed, and arrays and objects will become garbage when no reference variables point to them, but they also occupy memory. They are released at an indeterminate time by the garbage collector.

In addition to variables of 8 basic types (byte1 bytes int2 bytes short4 bytes Long8 bytes float4 bytes Double8 bytes char2 bytes boolean), other data types are referred to as reference data types, which is OBJECT.O The bject operation is all referenced by the object. Where references are a bit like pointers

Java heap memory, stack memory

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.