In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Today, I will talk to you about how to learn more about JVM stack and JVM stack. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
The JVM stack solves the running problem of the program, that is, how the program executes, or how to deal with the data; the JVM stack solves the problem of data storage, that is, how to put the data and where to put it, and objects are stored in the JVM heap. What is stored in the JVM stack are basic data types and references to objects in the JVM heap.
Basic concepts of JVM: JVM heap and JVM stack
Data type
In the Java virtual machine, data types can be divided into two categories: basic types and reference types. The basic type of variable holds the original value, that is, the value it represents is the value itself, while the variable of the reference type holds the reference value. "reference value" represents a reference to an object, not the object itself, which is stored at the location of 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
JVM heap and JVM stack are the key to program running, so it is necessary to clarify their relationship.
The JVM stack is the unit of runtime, while the JVM heap is the unit of storage.
The JVM stack solves the running problem of the program, that is, how the program executes, or how to deal with the data; the JVM stack solves the problem of data storage, that is, how to put the data and where to put it.
In Java, a thread will have a corresponding thread JVM stack, which is easy to understand, because different threads have different execution logic, so a separate thread JVM stack is required. The JVM heap is shared by all threads. Because the JVM stack is a running unit, the information stored in it is related to the current thread (or program). It includes local variables, program running state, method return values, and so on, while the JVM heap is only responsible for storing object information.
Why separate the JVM heap from the JVM stack? isn't it possible to store data in the JVM stack?
* from a software design point of view, the JVM stack represents the processing logic, while the JVM heap represents the data. This separation makes the processing logic clearer. The idea of divide and rule. This idea of isolation and modularization is reflected in all aspects of software design.
Second, the separation of the JVM heap from the JVM stack allows the contents of the JVM stack to be shared by multiple JVM stacks (which can also be understood as multiple threads accessing the same object). There are a lot of benefits from this sharing. On the one hand, this sharing provides an effective way of data interaction (such as shared memory), on the other hand, the shared constants and caches in the JVM heap can be accessed by all JVM stacks, saving space.
Third, the JVM stack needs to be divided into address segments because of runtime needs, such as saving the context in which the system is running. Because the JVM stack can only grow upward, it limits the ability of the JVM stack to store content. Unlike the JVM heap, the objects in the JVM heap can grow dynamically as needed, so the split of the JVM stack and the JVM heap makes dynamic growth possible, and only one address in the JVM heap needs to be recorded in the corresponding JVM stack.
Fourth, object-oriented is the combination of JVM heap and JVM stack. In fact, there is no difference in execution between object-oriented programs and previously structured programs. However, with the introduction of object-oriented, the way of thinking about problems has been changed, and it is closer to the natural way of thinking. When we take the object apart, you will find that the property of the object is actually data and is stored in the JVM heap, while the behavior (method) of the object, which is running logic, is placed on the JVM stack. When we write objects, we actually write both the data structure and the logic to deal with the data. I have to admit that 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 a program to run. Like C, Main in java is that starting point. No matter what java program, if you find main, you will find the entrance to program execution:)
What is stored in the JVM stack? what is stored in the JVM stack?
Objects are stored in the JVM heap. What is stored in the JVM stack are basic data types and references to objects in the JVM heap. The size of an object is inestimable, or can be changed dynamically, but in the JVM stack, an object corresponds to only one reference to 4btye (benefits of JVM stack JVM stack separation:).
Why not put the basic types in the JVM heap? Because it typically takes up 1 to 8 bytes of space-- it requires less space, and because it's a basic type, there's no dynamic growth-- the length is fixed, so it's enough to store it in the JVM stack, and it doesn't make sense to store it in the JVM heap (and it wastes space, as explained later). It can be said that references to primitive types and objects are stored in the JVM stack and are a number of bytes, so they are handled in a uniform way when the program is running. But the basic type, object reference, and the object itself are different, because one is the data in the JVM stack and the other is the data in the JVM heap. One of the most common problems is the problem of passing parameters in Java.
What about the value when the parameter in Java is passed? Or pass the quotation?
To explain this problem, we must first make two points clear:
1. Don't try to compare with C, there is no concept of pointer in Java.
two。 Programs always run in the JVM stack, so when passing parameters, there is only the problem of passing basic types and object references. Will not directly pass on the object itself.
After clarifying the above two points. When Java passes parameters in a method call, it always makes a value call because it does not have a pointer (you can refer to the C value call for this). Therefore, many books say that Java is to pass value calls, which is no problem, but also simplifies the complexity of C.
But how is the illusion of biography and quotation caused? In running the JVM stack, the basic type and reference are treated the same, both pass values, so if it is a method call that passes a reference, it can also be understood as a value call of "passing reference value", that is, the handling of a reference is exactly the same as that of a basic type. But when entering the called method, the value of the reference passed is interpreted (or looked up) by the program to the object in the JVM heap, which corresponds to the real object. If you make a change at this time, you are modifying the corresponding object, not the reference itself, that is, you are modifying the data in the JVM heap. So this change can be maintained.
Objects, in a sense, are made up of basic types. You can think of an object as a tree. If the attribute of an object is still an object, it is still a tree (that is, a non-leaf node), and the basic 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 everything below that node.
In the JVM heap and JVM stack, the JVM stack is the most fundamental thing for a program to run. A program can run without a JVM heap, but not without a JVM stack. The JVM heap is a data storage service for the JVM stack, which makes it clear that the JVM heap is a piece of shared memory. However, it is the idea of separating the JVM heap from the JVM stack that makes Java garbage collection possible.
In Java, the size of the JVM stack is set by-Xss. When there is a lot of data stored in the JVM stack, you need to increase this value appropriately, otherwise an java.lang.StackOverflowError exception will occur. The most common occurrence of this exception is unreturnable recursion, because the information stored in the JVM stack is the record point returned by the method.
After reading the above, do you have any further understanding of how to learn more about JVM heap and JVM stack? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.