Java heap and stack differences in the detailed _java

Source: Internet
Author: User
Tags memory usage mixed

When a person starts to learn Java or other programming language, will contact the heap and stack, because at the beginning without clear clear explanation, many people will produce many questions, what is heap, what is stack, heap and stack what is the difference? To make things worse, there is a data structure in Java that is in the order of a LIFO (last into first), which is java.util.Stack. In this case, many people could not help but understand the problems ahead. In fact, the heap and stack are part of the memory and have different functions, and a program needs to allocate memory on this area. As we all know, all Java programs are running inside the JVM virtual machine, and we are here to describe 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 are the specific differences

Specialize

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.

Unique 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 heap memory can be accessed by all threads.

Exception error

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

Space size

Stack memory is much less than heap memory, and if you use recursion, your stack will soon be filled. If recursion does not jump out in time, it is likely that stackoverflowerror problems will occur.
You can set the stack memory size by using the-XSS option. The-XMS option can set the size of the heap at the start, and the-XMX option sets the maximum value for the heap.

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

View default values (Updated)

View the default values for the heap, using the following code, where Initialheapsize is the size of the first heap, and maxheapsize is the maximum of the heap.

Copy Code code as follows:

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)

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

Copy Code code as follows:

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)

Translation Information

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

The translation is modified, sorted and deleted on the basis of the original text. If you are interested, you can visit the original. P.S. The address has been wall.

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.