Difference between heap and stack in Java, difference between Java

Source: Internet
Author: User

Difference between heap and stack in Java, difference between Java

When a person starts to learn Java or other programming languages, he will be exposed to heap and stack. Since there is no clear explanation at the beginning, many people will have a lot of questions, what is heap, what is stack? What is the difference between stack and stack? What's worse, there is a data structure In Java such as a post-In First-Out (Last In First Out) sequence, which is java. util. Stack. In this case, many people may not be able to understand the previous problems. In fact, the heap and stack are both part of the memory and have different effects, and a program needs to allocate memory in this area. As we all know, all Java programs run inside the JVM virtual machine, we will introduce the heap and stack in the JVM (virtual) memory.

Differences

The difference between heap and stack in java is naturally a common problem in the interview. The following are the specific differences:

Perform their respective duties

The main difference is that stack memory is used to store local variables and call methods.
Heap memory is used to store objects in Java. Whether it's member variables, local variables, or class variables, the objects they point to are stored in the heap memory.

Exclusive or shared

Stack memory belongs to a single thread, and each thread has a stack memory. Its stored variables can only be seen in the thread, that is, stack memory can be understood as the private memory of the thread.
Objects in heap memory are visible to all threads. Objects in heap memory can be accessed by all threads.

Error

 

If the stack memory does not have available space storage method calls and local variables, JVM will throw java. lang. StackOverFlowError.
If the heap memory has no available storage space to generate objects, JVM will throw a java. lang. OutOfMemoryError.

Space size

The stack memory is much smaller than the heap memory. If you use recursion, your stack will soon be full. StackOverFlowError may occur if recursion does not jump out in time.
You can use the-Xss option to set the stack memory size. -The Xms option can be used to set the size at the beginning of the heap, and the-Xmx option can be used to set the maximum value of the heap.

This is the difference between heap and stack in Java. If you understand this problem well, it can help you solve problems in development, analyze the usage of heap memory and stack memory, and even optimize performance.

View default value (Updated)

View the default heap value. Use the following code. InitialHeapSize indicates the size of the first heap, and MaxHeapSize indicates the maximum value of the heap.

 

123456789
13:17 $ java -XX:+PrintFlagsFinal -version | grep HeapSize    uintx ErgoHeapSizeLimit                         = 0                                   {product}    uintx HeapSizePerGCThread                       = 87241520                            {product}    uintx InitialHeapSize                          := 134217728                           {product}    uintx LargePageHeapSizeThreshold                = 134217728                           {product}    uintx MaxHeapSize                              := 2147483648                          {product}java version "1.8.0_25"Java(TM) SE Runtime Environment (build 1.8.0_25-b17)Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)

Check the stack default value. ThreadStackSize indicates the stack memory size.

1234567
13:21 $ java -XX:+PrintFlagsFinal -version | grep ThreadStackSize     intx CompilerThreadStackSize                   = 0                                   {pd product}     intx ThreadStackSize                           = 1024                                {pd product}     intx VMThreadStackSize                         = 1024                                {pd product}java version "1.8.0_25"Java(TM) SE Runtime Environment (build 1.8.0_25-b17)Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)

 

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.