"JVM Road to Immortality" virtual machine stack can still be played like this

"JVM's Road to Immortality" tender spring minnow, points deducted for homophones

I hope I can learn JVM with everyone in an interesting way, making us feel that this knowledge is not so difficult.

nonsense

The book continues from the previous chapter. Along the direction the boss is looking, you can only see rapid rivers and minnows with unusual vitality.

You were a little confused and looked at the boss, so what is it?

The boss turned around and said, v50, otherwise I won’t tell you.
Insert image description here
Xiao Wang had a dark look on his face, but he had no choice but to endure his anger and turned around. Then he forced out a smile and said, "Okay now, tell me why."

The boss pointed to the fish in the river below and said that this fish is called tender spring minnow. As long as you eat it, you can increase your internal strength. However, it depends on your personal physique. Some people can increase a part of their internal strength every time they eat one. After eating one, your internal energy will not increase. Seeing you like this, you can only do one thing at most.

After forcing out a thank you, Xiao Wang took Xiao Mei to catch fish.
Insert image description here
After struggling all afternoon, I finally caught one. After it was roasted, Xiao Wang gave me half of it. After eating, I suddenly felt a warm current rising in my body. This warm current then traveled to my abdomen and stopped moving. You also learn their spell-casting postures, but it obviously has no effect. On the other hand, Xiaomei next door has already plowed two miles of land with this enthusiasm.

Just when you feel like you can't change your fate, an old man in white walks towards you two.

The old man said, "Young man, what a system. There is no change after eating the tender spring minnows. He is indeed the chosen one." Seeing as you are extremely talented, why not join me as my disciple? I will definitely train you well.

In this way, you and Xiaomei both become disciples of the old man in white, and the old man in white will temporarily call him Great Elder.

The first lesson the Great Elder teaches you is how to use your own inner strength. The great elder said, after eating the tender spring minnows, you have developed your own virtual machine stack. Whenever you want to release a skill, there is a virtual machine stack for that skill to use. And in order to release the skills more smoothly, we will divide the skills into methods one by one, and each method will be allocated the internal force of a stack frame in the virtual machine stack exclusively for use by this method.

Seeing Xiaomei's confused face, you can't help but secretly rejoice. Doesn't this correspond to the stack frames in the virtual machine stack? Each method corresponds to a stack frame, and each stack frame stores the parameters of the method. Information such as local variables and return values.

The great elder continued. In fact, the method is like a cheat book, it just tells you the steps of how to release the skill, and the things you operate when releasing the skill are stored in another part.

You look at the elder to see if the things you operate when releasing the skill are stored in the pile.
Insert image description here

The elder looked at you in surprise, then smiled and said, I didn't expect you to have such understanding at such a young age. It's really good.

As you said, it's stored on the heap. We call the things operated by skills as objects. For example, the flames released in skills are objects. These flames also require our internal strength to support them. The source of internal strength is called a heap. And how to let the flames spray out, or where to spray them, is what needs to be done in the method.

After listening to this, you become more confident in yourself, and you are more convinced that the skill release mechanism here is consistent with the internal structure of the jvm. The so-called skill can correspond to a thread, and the method is the method called during the running process of the thread. When calling the method, you need to apply for a stack frame in the virtual machine stack of the corresponding thread, and when creating a new object in the method, you need to apply in the heap. Space corresponds exactly to the skill release process.

At this point, you can't hold back your excitement and want to try your skills. The master saw your thoughts and taught you how to release the flames.

I saw a ball of flame appear in the palm of your hand with a snap of your fingers. At the same time, you also sensed the changes in the virtual machine stack in your body. You sensed the virtual machine stack corresponding to the skill, and at the same time, you noticed that a stack frame was pushed into the stack. , and at the same time, your flame object also applies for internal force in the heap. When the flame goes out, you feel the stack frame popping up in the virtual machine stack, and the internal force applied in the heap is also replaced.

You take Xiaomei and practice over and over again. But the appearance of a ball of fire at one time no longer satisfies you, but you can only create a ball of fire at the moment. At this time, you suddenly think, I only open one method and apply for one stack frame at a time. Can I open multiple methods and apply for multiple stack frames at the same time? You will feel happy when you think of this, let's try it.
Insert image description here

As the flames appear in your palms, you become more and more excited, thinking that you are really a genius. But slowly you realized something was wrong. As the flames increased, you slowly felt dizzy until you suddenly fell to the ground.

When the great elder heard the noise, he immediately helped you up and waited for you to sit quietly for a while before slowly waking up. You looked at the great elder with difficulty and asked, what's wrong with me?

The great elder said, you are too eager for quick success and have opened multiple flame generation methods at once, resulting in too many stack frames being applied for and the stack memory overflowing.

You can't help but smile, thinking that after writing so many programs, you actually forgot about the termination condition of recursion.

The great elder asked you to take a rest, but you had no choice but to give up. At the same time, you also discovered something. Why did the flames appear so slowly? Can the speed be increased?

JVM content

  • There is a virtual machine stack in the JVM's memory structure, and each thread has an exclusive virtual machine stack. This makes content stored in the virtual machine stack generally thread-safe.
  • Each virtual machine stack stores multiple stack frames. Each method executed by a thread must apply for a stack frame, which stores information such as method parameters, local variables, and return values. But if local variables are returned as return values, or multiple methods use the same object as parameters, it is not thread safe.
  • When too many stack frames are applied for, the virtual machine stack memory will overflow.
  • Garbage collection does not manage memory on the stack

Too many stack frames java example

public class test {
    
    
    public static void dg(int a) {
    
    
        System.out.println(a);
        dg(a + 1);
    }

    public static void main(String[] args) {
    
    
        dg(1);
    }

}

If the above code performs unlimited recursion, it will cause too many stack frames and memory overflow.
Insert image description here

As my own notes, it is also the blogger’s own interest. If you like this style, you are welcome to click the like and favorite buttons below. I also hope to receive your comments.

For more content, please pay attention to the JVM Road to Immortality column
》》JVM Road to Immortality Column《》

Insert image description here

Guess you like

Origin blog.csdn.net/qq_43627076/article/details/132498511