Differences between heap and Stack in Java

Source: Internet
Author: User

01, the respective duties;

Stack memory is used to store local variables and method calls,

Heap memory is used to store objects in Java, whether it is a member variable, a local variable, or a class variable

The objects they point to are stored in heap memory.

02, exclusive or shared;

Stack memory belongs to a single thread, each thread will have a stack of memory, its stored objects

Only visible in the thread to which it belongs, that is, stack memory can be understood as private memory, and objects in heap memory

Visible to all threads, the objects in the team memory can be accessed by all threads

03, abnormal error;

If the stack memory does not have an available space storage method call and a variable, the JVM throws

Java.lang.StackOverFlowError.

If there is no space available in the heap memory to store the generated objects, the JVM throws

Java.lang.OutOfMemoryErroy

04, Memory;

      Stack memory is much smaller than heap memory

  

  

         

Differences between 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.