Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the conceptual model of JVM

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you what is the conceptual model of JVM. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

1. 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.

two。 Stack and stack

The stack and stack are the key to program running, the stack is the unit of runtime, and the heap is the unit of storage. The stack solves the running problem of the program, that is, how the program executes, or how to deal with the data; the heap 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 stack, which is easy to understand, because different threads have different execution logic, so a separate thread stack is required. The heap is shared by all threads. Because the stack is the running unit, the information stored in it is related to the current thread (or program). It includes local variables, program running state, method return value, and so on, while the heap is only responsible for storing object information.

3. Why separate the heap from the stack? Isn't it possible to store data in the stack?

First, from a software design point of view, the stack represents the processing logic, while the 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 heap from the stack enables the contents of the heap to be shared by multiple 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, shared constants and caches in the heap can be accessed by all stacks, saving space.

Third, the stack needs to divide the address segment because of the needs of the runtime, such as saving the context in which the system is running. Because the stack can only grow upward, it limits the ability of the stack to store content. Unlike the heap, the objects in the heap can grow dynamically as needed, so the split of the stack and the heap makes dynamic growth possible, and only one address in the heap needs to be recorded in the corresponding stack.

Fourth, object-oriented is the perfect combination of heap and 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 properties of the object are actually data stored in the heap, while the behavior (method) of the object is to run the logic and put it on the 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 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:)

4. What is stored in the heap? What is stored in the stack?

Objects are stored in the heap. What is stored in the stack are the basic data types and references to objects in the heap. The size of an object is inestimable, or can be changed dynamically, but in the stack, an object corresponds to only one reference to 4btye (benefits of stack separation:).

Why not put the basic types in the heap? Because it generally takes up 1 to 8 bytes of space-less space is needed, and because it is a basic type, there is no dynamic growth-the length is fixed, so storage in the stack is enough. it doesn't make sense to store it in the heap (and it wastes space, as explained later). It can be said that references to basic types and objects are stored on the stack and are a number of bytes, so they are handled in a uniform way when the program is running. But the basic type, the object reference, and the object itself are different, because one is the data in the stack and the other is the data in the heap. One of the most common problems is the problem of passing parameters in Java.

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 on the 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 the run stack, the basic type and reference are treated the same, both passing values, so if it is a method call of passing reference, it can also be understood as a value call of "passing reference value", that is, the treatment of reference is exactly the same as that of the 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 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 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 stack and stack, the stack is the most fundamental thing for the program to run. A program can run without a heap, but not without a stack. The heap is the data storage service for the stack, to put it bluntly, the heap is a shared memory. However, it is the idea of separation of heap and stack that makes garbage collection of Java possible.

In Java, the size of the stack is set by-Xss. When there is a lot of data stored in the stack, you need to increase this value appropriately, otherwise a java.lang.StackOverflowError exception will occur. The most common occurrence of this exception is unreturnable recursion, because the information stored in the stack is the record point returned by the method.

This is what the conceptual model of JVM shared by the editor is. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report