Ali interviewer: Do Java objects have to be allocated in the heap? My body is imperfect

Interview experience

I remember that a few years ago, Ali went to an interview and asked this question:

Are objects in Java allocated in the heap? Explain why!

At the time I was asked with a stunned look, and was instantly smashed to perfection. At the time, I didn't know what knowledge points he was testing. Isn't the object allocated in the heap? In the end, there was no more. I went back and waited for the notice.

Ali interviewer: Do Java objects have to be allocated in the heap?  My body is imperfect

 

Ali interviewer: Do Java objects have to be allocated in the heap?  My body is imperfect

 

Ali interviewer: Do Java objects have to be allocated in the heap?  My body is imperfect

 

Ali interviewer: Do Java objects have to be allocated in the heap?  My body is imperfect

 

Object allocation

Almost all objects are allocated in the heap. This is a sentence that everyone often sees, but this sentence does not mean all. Objects in the JVM can be allocated on the stack, but the premise is to judge the escape state.

Object escape state

1. GlobalEscape

That is, the scope of an object escapes the current method or current thread. There are several scenarios:

  • Object is a static variable
  • The object is an object that has escaped
  • Object as the return value of the current method

2. Parameter escape (ArgEscape)

That is, an object is passed as a method parameter or referenced by a parameter, but no global escape occurs during the call. This state is determined by the bytecode of the called method.

3. No escape

That is, the object in the method does not escape.

Escape analysis code

public class EscapeAnalysisTest { 
  public static void main(String[] args) throws Exception {    
    long start = System.currentTimeMillis();      
    for (int i = 0; i < 50000000; i++) {            
      allocate();          }           System.out.println((System.currentTimeMillis() - start) + " ms");    
    Thread.sleep(600000);    }​   
    static void allocate() {        
      MyObject myObject = new MyObject(2020, 2020.6);    }​ 
      static class MyObject {        int a;        double b;​        MyObject(int a, double b)
        {            this.a = a;            this.b = b;        
        }    }}

In the process of calling this code, the object myboject belongs to the global escape, and the JVM can allocate it on the stack

Then observe the difference by turning on and off the DoEscapeAnalysis switch.

Enable escape analysis (JVM is enabled by default)

Ali interviewer: Do Java objects have to be allocated in the heap?  My body is imperfect

 

View execution speed

Ali interviewer: Do Java objects have to be allocated in the heap?  My body is imperfect

 

Turn off escape analysis

Ali interviewer: Do Java objects have to be allocated in the heap?  My body is imperfect

 

View execution speed

Ali interviewer: Do Java objects have to be allocated in the heap?  My body is imperfect

 

The test results show that enabling escape analysis has a great impact on the execution performance of the code! Then why is there such an impact?

Escape analysis

If the object analyzed by escape can be allocated on the stack, then the life cycle of the object follows the thread, and garbage collection is not required. If this method is called frequently, the performance can be greatly improved.

After adopting escape analysis, the objects satisfying the escape are allocated on the stack

Ali interviewer: Do Java objects have to be allocated in the heap?  My body is imperfect

 

The escape analysis is not enabled, and the objects are allocated on the heap, which will frequently trigger garbage collection (garbage collection will affect system performance), resulting in slow code running

Ali interviewer: Do Java objects have to be allocated in the heap?  My body is imperfect

 

Code verification

Enable GC print log

-XX:+PrintGC

Turn on escape analysis

Ali interviewer: Do Java objects have to be allocated in the heap?  My body is imperfect

 

You can see that there is no GC log

Turn off escape analysis

Ali interviewer: Do Java objects have to be allocated in the heap?  My body is imperfect

 

It can be seen that the escape analysis is turned off, and the JVM is performing garbage collection (GC) frequently. It is this operation that causes a big difference in performance.

to sum up

JVM is something that many interviewers like to ask. But some interviewers' questions are very tricky, because the JVM has a broad and profound knowledge system, so if you are not sure, you should not answer them lightly. Some knowledge requires practice to experience, calmly explaining these knowledge points will let you take the initiative in the interview.

Guess you like

Origin blog.csdn.net/python8989/article/details/108759836