It's October 24, 2020, you may not know the secrets of those JVMs

1 Introduction

All the programmers here, on October 24th, is there anyone sitting at the company working overtime today?

I guess there must be someone who makes us programmers, just like the soldiers who defend the country, always fighting on the front lines of the battlefield to protect the peace of life of the people.

No matter what, I would like to congratulate my compatriots on a happy holiday, never BUG, ​​always youthful, never lose hair~~~

2. Think about it

When the editor first started to learn the Java language, and then learned about the JVM, the teacher has always said that the instance objects in Java are stored in the heap area of ​​the JVM.

Let me give you a picture of the memory model of the JVM. This picture should be very familiar, and you should have a general idea of ​​what each piece is used for.

After a few years, the concept that all instance objects in Java are stored in the heap area has been deeply imprinted by the editor.

I still remember that Xiaobian encountered a similar problem during the interview:

Interviewer: Do you know which area of ​​the Java instance objects are stored in the JVM? 

Editor: Heap area.

Interviewer: Assuming that one million objects are new in the method, are they still all stored in the heap area?

So the key point is here. At the beginning, for the young and ignorant editor, how could he think so much, but think that as long as it is an instance object, it will only be stored in the heap.

If you guys don’t know the answer, then take this question and watch the actual operation.

public class Test_1 {

    public static void main(String[] args) {
       for (int i=0;i < 10000;i++){
           create();
       }
        while (true);

    }

    public static void create(){
        Test_1 test = new Test_1();
    }
}

In the code, loop 1w times, through the HSDB tool (HSDB can view the contents of the JVM data area at runtime), it is obvious that there are indeed 1w Test_1 objects in the heap area, so now it is changed to loop Change it to 100w and look again.

This is all right, the count did not reach 100w. At first, the editor thought about whether it was recycled by the GC, and then printed the GC log but did not find it.

So the question is, where did the remaining objects go? ? ?

 

3. Take a look

In fact, there is a knowledge point involved here, called: escape analysis . By default, escape analysis is turned on. Let's turn off escape analysis first and try again.

After turning off the escape analysis, by looking at the objects, there are finally 100w.

4. What is escape analysis?

In official terms, escape analysis is a way to determine the dynamic range of a pointer, which can analyze where the pointer can be accessed in the program. The pointer here can be understood as the reference address of the java instance object, and the dynamic range of the pointer can be understood as the access modifier (public, private, etc.) of the object.

It is estimated that many friends do not understand the above explanation, use the code to give an example.

In the start method, a new Test_1 object is created. It is obvious that the test_1 object can only be used in the start() method and cannot be used in other places.

This kind of object can be understood as a non-escaping object, because it cannot be accessed by other places.

public class Test_1 {

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

	public static void start() {
	   Test_1 test_1 = new Test_1();
	}
}

Now change the start method and return the test_1 object. Then this object can be understood as an escape object, because it is returned. As long as the start method is called, you can get this object and be used by others. Place to visit.

In other words, when the reference pointer of an instance object is referenced by multiple methods or threads, we say that this pointer has escaped.

	public static Test_1 start() {
		Test_1 test_1 = new Test_1();
		return test_1;
	}

Finally, through this escape and non-escape phenomenon, to analyze, it is called: escape analysis.

5. Allocation on the stack

Allocation on the stack is based on the analysis of escape analysis, and finally an optimization method analyzed. The biggest benefit should be to reduce the pressure of gc and allocate those objects that do not escape on the stack. After the escape analysis is completed, it can be determined which variables can be allocated on the stack. The allocation of the stack is faster than the heap and the performance is better.

This way, as demonstrated at the beginning of this article, not all 100w objects are in the heap, but when the escape analysis is turned off, they are all in the heap.

 逃逸分析可以通过这个参数控制,-XX:+/-DoEscapeAnalysis,+就是表示开启,-就是表示关闭。

 Next time, if you encounter the problem of asking whether the objects are all on the pile, don't just say that they are all on the pile.

Of course, stack allocation is only one of the optimizations, and there are also scalar replacement , lock elimination, etc. During escape analysis, if there is a synchronization lock on the method of the object you define, but at runtime, only one thread is accessing At this time, the machine code after escape analysis will run without the synchronization lock.

 

Seeing that Xiaobian 1024 is still in the code article, click like, like, like~~~~~~

Guess you like

Origin blog.csdn.net/weixin_38111957/article/details/109257814