Interviewer: Are all objects stored in the heap in Java? Do you know escape analysis?

Interviewer: What areas are the memory of the Java virtual machine divided into?

me (smiling): program counter, virtual machine stack, native method stack, heap, method area

Interviewer: Where are objects generally stored?

me: heap.

Interviewer: Are the objects all stored in the heap?

Yes I am.

Interviewer: Do you understand escape analysis?

I (frowning): Is it a memory overflow?

Interviewer: No.

Me (scratching my head): I don't really understand.

Interviewer: Today's interview is here first, go back and wait for the news!

Then there was no more, and I was unwilling to start looking for relevant information.

escape analysis

Escape Analysis (Escape Analysis) is an analysis method to determine the dynamic range of an object's reference. In other words, it is to analyze where in the program the reference to the object can be accessed.

When an object is allocated in a method, a reference to the object may escape to other threads of execution, or return to the caller of the method.

If a method allocates an object and returns a reference needle of the object, the place where the object may be accessed cannot be determined, and the reference to the object "escapes". If the reference to the object is stored in a static variable or other data structure, because the static variable can be accessed outside the current method, the reference to the object also "escapes".

Escape analysis determines all the places where a reference to an object can be accessed, and whether the lifetime of a reference to an object is guaranteed to be only in the current process or thread.

escape state

The escape state of an object is generally divided into three types: global escape, parameter escape, and no escape.

Global Escape

The reference to the object escapes the method or thread. For example: an object reference is assigned to a static variable, or stored in an escaped object, or an object reference is given to the calling method as the method's return value.

For example, the singleton pattern of hungry man:

package one.more;

public final class GlobalEscape {

    // instance对象赋值给了一个静态变量,发生了全局逃逸
    private static GlobalEscape instance = new GlobalEscape();

    private GlobalEscape() {
    }

    public static GlobalEscape getInstance() {
        return instance;
    }
}

Argument escape (ArgEscape)

Objects are passed as method parameters or referenced by parameters, but no global escape occurs during invocation. This state is determined by analyzing the bytecode of the called method.

for example:

package one.more;

public class ArgEscape {

    class Rectangle {

        private int length;
        private int width;

        public Rectangle(int length, int width) {
            this.length = length;
            this.width = width;
        }

        public int getArea() {
            return this.length * this.width;
        }
    }

    public int getArea(int length, int width) {
        Rectangle rectangle = buildRectangle(length, width);
        return rectangle.getArea();
    }

    private Rectangle buildRectangle(int length, int width){
        Rectangle rectangle = new Rectangle(length, width);
        // rectangle对象发生了参数逃逸
        return rectangle;
    }
}

No Escape (NoEscape)

The object in the method does not escape, which means that the object can not be allocated on the heap.

for example:

package one.more;

public class NoEscape {

    class Rectangle {

        private int length;
        private int width;

        public Rectangle(int length, int width) {
            this.length = length;
            this.width = width;
        }

        public int getArea() {
            return this.length * this.width;
        }
    }

    public int getArea(int length, int width) {
        // rectangle对象没有逃逸
        Rectangle rectangle = new Rectangle(length, width);
        return rectangle.getArea();
    }
}

Optimization after escape analysis

If an object does not escape, or only parameters escape, it is possible to take different degrees of optimization for this object, such as: stack allocation, scalar replacement, synchronization elimination.

Stack Allocations

If an object does not escape from the thread, it would be a good idea to allocate memory for the object on the stack, and the memory space occupied by the object can be destroyed when the stack frame is popped off the stack. Then, the object will be automatically destroyed with the end of the method, which can reduce the frequency of the garbage collector running, and the pressure of garbage collection will be greatly reduced.

Scalar Replacement

A scalar is a piece of data that cannot be broken down into smaller pieces. The basic data types in the Java virtual machine (numeric types such as int, long and reference types, etc.) cannot be further decomposed, so these data can be called scalars. In contrast, if a piece of data can continue to be decomposed, it is called an aggregate, and an object in Java is a typical aggregate.

If a Java object is dismantled, and the member variables used by it are restored to the basic type for access according to the program access, this process is called scalar replacement .

If an object does not escape and can be replaced by scalar, then the member variables of the object are allocated and read and written on the stack, and do not need to be allocated to the heap.

Scalar substitution can be regarded as a special case of stack allocation, which is simpler to implement, but requires a higher degree of escape. It does not allow objects to escape without escaping.

Synchronization Elimination

Thread synchronization itself is a relatively time-consuming process. If an object does not escape the thread and cannot be accessed by other threads, then there will be no competition for reading and writing of the object, and the synchronization and locking operation of the object can be implemented. Safely removed.

Summarize

Having said so much, it can be found that objects are not all allocated memory on the heap. Because after escape analysis, scalar replacement can be performed on objects without escape.

In addition, due to complexity and other reasons, the optimization of stack allocation is not currently supported in HotSpot.

Finally, thank you for being so handsome, and for giving me likes and attention .

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324116381&siteId=291194637