Java Learning (17): JVM Heap and stack

Source: Internet
Author: User

1, the stack is the unit of the runtime, the heap is the unit of storage.

2, the problem of the operation of the stack solution, that is, how the program executes, or how to process the data, the heap solves the problem of data storage , that is, how to put the data, where.

3, in Java, a thread will have a corresponding line stacks corresponding to it. The heap is shared by all threads .

4, from the software design point of view, the stack represents the processing logic, and the heap represents the data . This separation makes the processing logic clearer.

5, heap and stack separation, so that the contents of the heap can be shared by multiple stacks (also can be understood as multiple threads to access 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.

6, the stack because of the needs of the runtime, such as saving the context of the system operation, the need to partition the address segment. Because the stack can only grow upward, it restricts the ability to store content on the stack. While the heap is different, the objects in the heap can grow dynamically as needed, so the stack and heap splits make the dynamic growth possible, and only one address in the heap is required to be recorded in the corresponding stack.

7, object-oriented is the perfect combination of heap and 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 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 also write the logic of the processing.

8. 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 4btye reference (the benefit of stack separation) why not put the base type in the 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 stack is enough, and it makes no sense to put him in the heap. 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.

9, the parameters in Java pass simultaneous value? Or a reference? To illustrate this problem, we must first clarify two points: 1. Do not attempt to analogy with C, there is no concept of pointers in Java 2. The program runs always on the 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.

10, in Java, themain function is the starting point of the stack, but also 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. No matter what Java program, find main to find the program execution of the portal.

Java Learning (17): JVM Heap and 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.