The stack is the unit of the runtime, and the heap is the stored unit .
Stack solves the problem of how the program executes, or how the data is handled, and the heap solves the problem of data storage, that is, where the data is put and where it is placed .
It is easy to understand that a thread in Java has a line stacks corresponding to it, because different thread execution logic differs and therefore requires a separate line stacks. The heap is shared by all threads. Because the stack is a running unit, the information stored inside it is information that is related to the current thread (or program). This includes local variables, program run states, method return values, and so on, and the heap is only responsible for storing object information.
Why do you want to differentiate the heap from the stack? Is it possible to store data in the stack?
1. From a software design standpoint, the stack represents the processing logic, and the 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.
2. The separation of the heap from the stack allows the contents of the heap to be shared by multiple 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 kind of sharing provides an effective way of data interaction (for example: Shared memory), on the other hand, the shared constants and caches in the heap can be accessed by all stacks, saving space.
3. Stack because of the needs of the runtime, such as saving the context of the operation of the system, the address segment needs to be divided. Because the stack can only grow upward, so it limits the ability to store content on the stack, and the heap is different, the objects in the heap can grow dynamically as needed, so the stack and heap splits make it possible to dynamically grow, and only one address in the heap is recorded in the corresponding stack.
4. Object-oriented is the perfect combination of stacks and stacks. 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 heap, and the object's behavior (method) is running logic, placed in the stack. When we write the object, we write the data structure, and we write the logic to deal with it. I have to admit, object-oriented design is really beautiful.
In Java , the main function is the starting point of the stack and the starting point of the program.
There is always a starting point for the program to run. Like the C language, main inJava is the starting point. No matter what Java program, find main to find the program execution of the portal.
What is stored in the heap? What is stored in the stack?
The object is stored in the heap. The stack is a reference to the base data type and the objects in the heap. The size of an object is not estimated, or can be dynamically changed, but in the stack, an object only corresponds to a 4byte reference (the benefit of stack separation).
Why not put the basic type in the heap? Because the space it occupies is generally 1~8 bytes---need less space, and because it is the basic type, so there is no dynamic growth of the situation---length fixed, so the stack storage is enough, if it exists in the heap is meaningless (also wasted space, explained later). It can be said that the basic type and object references are stored in the 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 stack, one is the data in the heap. One of the most common problems is the problem with parameter passing inJava .
Parameter passing in Java is a pass value? Or a reference?
1. Do not attempt to analogy with C, there is no concept of pointers inJava .
2. The program is always run in the stack, so when the parameter is passed, there is only the problem of passing the base type and object reference. 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, which is fine and simplifies C complexity.
But how is the illusion of citation caused? In the run stack, the basic type and the reference are treated the same, and are all passed values, so if it is a reference to a method call, it can also be understood as a value invocation of "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 heap, which corresponds to the actual object. If you modify at this point, you are modifying the reference object, not the object itself, that is, modifying the data in the heap. So this modification can be maintained.
objects, in a sense, are made up of basic types. You can view an object as a tree, the object's properties are 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.
Stacks and stacks are the most fundamental things that a program runs. A program can run without a heap, but not without a stack. The heap is a data storage service for the stack, and the heap is a shared memory. However, it is the idea of the separation of heaps and stacks that makes Java garbage collection possible.
In Java , the size of the stack is set by-XSS, and when the data stored in the stack is relatively large, it needs to be properly resized, or Java. Lang will appear. Stackoverflowerror exception. The common occurrence of this exception is the inability to return recursion because the information saved in the stack is the record point returned by the method.
Java Heap and Stack