JVM Heap and JVM stack

Source: Internet
Author: User

JVM Stack resolver operation problem, that is, how the program executes, or how to process the data; The JVM heap solves the problem of data storage, where the data is placed, where it is placed, and what is stored in the JVM heap is the object. The JVM stack is a reference to the base data type and the objects in the JVM heap.

JVM Basic Concepts: JVM heap and JVM stack

Data type

In a Java virtual machine, a data type can be divided into two categories: the base type and the reference type. A variable of the underlying type holds the original value, that is, the value he represents is the value itself, whereas a variable of the reference type holds the reference value. A reference value represents a reference to an object, not the object itself, where the object itself resides at the address represented by the reference value.

Basic types include: byte,short,int,long,char,float,double,boolean,returnaddress

Reference types include: Class types, interface types, and arrays.

JVM Heap and JVM stack

The JVM heap and JVM stacks are key to the program's operation, and it's important to make their relationship clear.


The JVM stack is the unit of the runtime, and the JVM heap is the unit of storage.

JVM Stack resolver operation problem, that is, how the program executes, or how to process the data; The JVM heap solves the problem of data storage, where the data is placed and where it is placed.

It is easy to understand that a thread in Java has a thread JVM stack corresponding to it, because different thread execution logic differs, and therefore requires a separate thread JVM stack. The JVM heap is shared by all threads. The JVM stack is a running unit, so the information stored in it is related to the current thread (or program). This includes local variables, program run states, method return values, and so on, while the JVM heap is only responsible for storing object information.

Why would you differentiate the JVM heap from the JVM stack? Is it possible to store data in the JVM stack?

First, from the point of view of software design, the JVM stack represents the processing logic, and the JVM heap represents the data. This separation makes the processing logic clearer. The idea of divide and conquer. This idea of isolation and modularity is reflected in every aspect of software design.

Second, the separation of the JVM heap from the JVM stack allows the contents of the JVM heap to be shared by multiple JVM stacks (and can also be understood as multiple threads accessing the same object). The benefits of this sharing are many. On the one hand this sharing provides an efficient way of interacting with data (e.g. shared memory), and on the other hand, shared constants and caches in the JVM heap can be accessed by all JVM stacks, saving space.

Thirdly, the JVM stack needs to divide the address segment because of its runtime needs, such as saving the context of the system's operation. Because the JVM stack can only grow upward, it restricts the ability of the JVM stack to store content. Unlike JVM heaps, objects in the JVM heap can grow dynamically as needed, so the splitting of the JVM stack and JVM heap makes it possible to dynamically grow, and only one address in the JVM heap is recorded in the corresponding JVM stack.

Finally, object-oriented is the perfect combination of JVM heap and JVM stack. In fact, the object-oriented approach to the implementation of the previous structured program is not any different. However, the introduction of object-oriented, so that the way to think about the problem has changed, and closer to the natural way of thinking. When we take the object apart, you will find that the object's properties are actually data, stored in the JVM heap, and the object's behavior (method) is run logic, placed in the JVM stack. When we write the object, we write the data structure, and also write the logic of the processing. I have to admit, object-oriented design is really beautiful.

In Java, the main function is the starting point of the JVM stack and the starting point of the program.

There is always a starting point for the program to run. Like the C language, main in Java is the starting point. Whatever the Java program, find main and find the entry for the program execution:)

What is stored in the JVM heap? What is stored in the JVM stack?

An object is stored in the JVM heap. The JVM stack is a reference to the base data type and the objects in the JVM heap. The size of an object is not estimated, or can be dynamically changed, but in the JVM stack, an object only corresponds to a 4btye reference (JVM heap JVM Stack Separation benefits:)).

Why not put the basic type in the JVM heap? Because the space it occupies is typically 1~8 bytes-it takes less space, and because it is a basic type, there is no dynamic growth-fixed length, so storage in the JVM stack is enough, and it makes no sense to put him in the JVM heap ( Also wasted space, explained later). It can be said that the basic type and object references are stored in the JVM stack, and are a number of bytes, so when the program runs, they are handled in a uniform manner. But the basic type, the object reference, and the object itself are different because one is the data in the JVM heap, one is the data in the JVM stack. One of the most common problems is the problem with parameter passing in Java.

Does the parameter in Java pass the simultaneous value, or is it a reference?

To illustrate this issue, first make two points clear:

1. Do not attempt to analogy with C, the concept of no pointers in Java

2. The program runs always in the JVM stack, so when parameters are passed, there is only a problem passing the base type and object references. The object itself is not passed directly.

Explicitly after the two points above. When Java passes a parameter in a method call, because there is no pointer, it is the invocation of a value (this can be referred to as a call to the value of C). As a result, many of the books say that Java is a call to value, and that this is no problem and simplifies C complexity.

But how does the illusion of citation come about? In running the JVM stack, the basic type and the reference are handled the same, all of which are pass values, so if it is a reference to a method call, it can also be understood as a value invocation of the "reference value", that is, the processing of the reference is exactly the same as the base type. However, when the called method is entered, the value of the reference being passed is interpreted (or found) by the program to the object in the JVM heap, which corresponds to the actual object. If you modify at this point, you modify the reference object instead of the reference itself, which is: the data in the JVM heap is modified. So this is a change that can be maintained.

objects, in a sense, are made up of basic types. You can look at an object as a tree, the object's properties, if it is an object, is still a tree (that is, a non-leaf node), and the base type is the leaf node of the tree. When a program parameter is passed, the passed value itself cannot be modified, but if the value is a non-leaf node (that is, an object reference), you can modify all the content underneath the node.

In the JVM heap and JVM stacks, the JVM stack is the most fundamental thing the program runs. The program can run without the JVM heap, but not without the JVM stack. The JVM heap is a data storage service for the JVM stack, and the JVM heap is a shared memory. However, it is because of the separation of the JVM heap and the JVM stack that the garbage collection of Java is made possible.

Java, the size of the JVM stack is set by-XSS, when the JVM stack stored data more than a long time, you need to adjust this value appropriately, otherwise there will be Java.lang.StackOverflowError exception. The common occurrence of this exception is the inability to return recursion because the information saved in the JVM stack is the record point returned by the method.

JVM Heap and JVM stack

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.