The difference between heap and Stack in Java

Source: Internet
Author: User

(Reprint technology Small black House) The difference between heap and Stack in Java

DEC 7TH, 2014

When a person begins to learn Java or other programming languages, it comes into contact with heaps and stacks, and since there is no clear explanation at first, many people will have many questions, what is a heap, what is a stack, and what is the difference between a heap and a stack? To make things worse, there is a data structure in Java that is in the order of a last-in, first-out, which is java.util.Stack. In this case, it inevitably makes many people more puzzled by the previous question. In fact, the heap and stack are all part of the memory, have different functions, and a program needs to allocate memory on this area. As we all know, all Java programs run inside the JVM virtual machine, and what we are describing here is naturally the heap and stack in the JVM (virtual) memory.

Difference

The difference between heap and stack in Java is naturally a common problem in the interview, the following points are the specific differences

Perform their duties

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

Exclusive OR shared

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

Exception error

If the stack memory does not have space to store method calls and local variables, the JVM throws Java.lang.StackOverFlowError.
And if the heap memory does not have space available to store the generated objects, the JVM throws Java.lang.OutOfMemoryError.

Space size

The memory of the stack is much smaller than the heap memory, and if you use recursion, your stack will soon be filled. If recursion does not jump out in time, stackoverflowerror problem is likely to occur.
You can set the stack memory size with the-XSS option. The-XMS option sets the size at which the heap starts, and the-XMX option sets the maximum value for the heap.

This is the difference between heap and Stack in Java. Understanding this problem can help you solve problems in development, analyze heap memory and stack memory usage, and even performance tuning.

View default values (Updated)

View the heap defaults, using the following code, where Initialheapsize is the size of the first heap, and maxheapsize is the maximum value of the heap.

123456789       
13:17$ java-xx:+printflagsfinal-version | grep heapsizeUintxErgoheapsizelimit= 0{Product}UintxHeapsizepergcthread= 87241520 {product} Uintx initialheapsize: Span class= "o" >= 134217728 {product} Uintx  Largepageheapsizethreshold = 134217728 {product}  Uintx maxheapsize: = 2147483648 {product}java version  "1.8.0_25" java (tm (build 1.8.0_25-b17) java Hotspot (tm (build 25.25-b02, mixed Mode)  /span>                

View the default value of the stack, where threadstacksize is the size of the stack memory.

1234567     
13:21$ java-xx:+printflagsfinal-version | grep threadstacksizeintx compilerthreadstacksize = 0 {PD product} intx threadstacksize  = 1024x768 {PD product} intx vmthreadstacksize = 1024x768 {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)          
Translation Information

Authentic English Original: http://javarevisited.blogspot.com.au/2013/01/difference-between-stack-and-heap-java.html.

The translation has been modified, collated and abridged on the basis of the original text. You can access the original text if you are interested. P.S. Address has been wall.

A book

Java performance This book can help you drill down into the JVM, garbage collection, memory monitoring, and performance tuning.

The difference 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.