[Java] Heap and stack differences

Source: Internet
Author: User

Memory in Java is divided into two types, heap memory and stack memory

One, stack memory

The basic type variable and the object reference variable defined in the function, Java allocates the stack memory to it. Once you leave the scope, the memory will be freed.

Java also allocates stack space to variables defined in the code block within the function.

Second, heap memory

The memory created with the New keyword in Java is allocated on the heap, which is automatically managed by the Java Virtual machine's garbage collector. In the stack, you can define special reference variables to point to the objects allocated in the heap memory or to the first address of the array, and use these reference variables to manipulate the heap memory. When there is no reference to the memory in these heaps, the memory becomes inaccessible memory, which becomes garbage, but is not reclaimed at this time. GC garbage collector, which recycles the garbage at an indeterminate time.

[1] http://blog.csdn.net/huozhicheng/article/details/6575960

[Java] Heap and stack differences

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.