[Android Interview Questions] The latest interview topic in 2023: Principles of Java Virtual Machine (4)

10 Difference between StackOverFlow and OOM? When does it happen, what is stored in the JVM stack, and what is stored in the heap? (Meituan)

What is this question trying to investigate?

JVM memory

Knowledge points of inspection

Memory leak, JVM runtime data area

How should candidates answer

StackOverFlow occurs due to insufficient stack space. The main reason is that too many methods are called during the running of a single thread or the storage space used by the stack frame requested by the method recursive operation exceeds the storage space requested by a single stack.

OOM mainly occurs when the memory space requested by the heap area is not enough, for example, a single application for a large object exceeds the continuous available space in the heap.

The stack is mainly used to store variables, references and temporary data that need to be used during the running of the program, and the heap mainly stores the object space applied for in the program.


11 Efficiency of StringBuffer and StringBuilder in string operations (byte beating)

What is this question trying to investigate?

How to make reasonable use of StringBuffer and StringBuilder when string operations are required in development

Knowledge points of inspection

string tools

How should candidates answer

When modifying the string, use StringBuffer and StringBuilder to modify it multiple times without generating new unused string objects.

When String performs string operations, each operation will create a new StringBuilder, such as string concatenation:

String result = "Hello ";
for(int i = 0; i < 100; i++){
    
    
    result += "Hello ";
}

The above code is equivalent to:

String result = "Hello ";
for(int i = 0; i < 100; i++){
    
    
   result = new StringBuilder().append(result).append("Hello ");
}

It can be seen that each cycle needs to create a StringBuilder and use its appendmethod to complete string splicing. Frequent creation and recycling of objects will cause "memory jitter" problems, causing program freezes or even OOM.

Therefore, relative to using String directly for string operations, the code should be optimized as:

StringBuilder result = new StringBuilder("Hello ");
for(int i = 0; i < 100; i++){
    
    
   result.append("Hello ");
}

The functions and implementation principles of StringBuffer and StringBuilder are the same. The difference between the two is that the methods in StringBuffer are all declared by keywords synchronized, which means that they are thread-safe, but because each execution method needs to acquire a lock, the efficiency is slightly lower than that of StringBuilder.

When manipulating strings in a multi-threaded environment, you should use StringBuffer; otherwise, you should choose StringBuilder, which is more efficient.

12 Difference between JVM DVM ART

What is this question trying to investigate?

The difference between Android virtual machine and Java virtual machine

Knowledge points of inspection

  1. Basic knowledge of JVM virtualization
  2. Basic knowledge of DVM virtual machine
  3. Basic knowledge of ART virtual machine

How should candidates answer

JVM

The JVM is a stack-based virtual machine. For a stack-based virtual machine, each runtime thread has an independent stack. The history of method calls is recorded in the stack, and every time there is a method call, there will be one more stack frame in the stack. The topmost stack frame is called the current stack frame, which represents the currently executing method. Stack-based virtual machines perform all operations through the operand stack.

insert image description here

To execute the bytecode in the JVM, the

int a = 1;
int b = 2;
int c = a + b;

Compiled into bytecode, the resulting instruction is:

ICONST_1  #将int类型常量1压入操作数栈
ISTORE 0  #将栈顶int类型值存入局部变量0
ICONST_2
ISTORE 1
ILOAD 0  #从局部变量表加载0:int类型数据
ILOAD 1
IADD     #执行int类型加法
ISTORE 2

Data is constantly moved between the operand stack and the local variable table.

Dalvik

Dalvik Virtual Machine (Dalvik Virtual Machine), referred to as Dalvik VM or DVM. DVM is a virtual machine specially developed by Google for the Android platform, which runs in the Android runtime library.

Difference from JVM
based on different architectures

DVM is a register-based virtual machine, and there is no operand stack in the JVM stack frame, but there are many virtual registers. In fact, the same as the operand stack, these registers are also stored in the runtime stack, which is essentially an array. Similar to the JVM, each thread in the Dalvik VM has its own PC and call stack, and the activity record of the method call is saved on the call stack in units of frames.

Compared with the JVM version, Dalvik does not need to read and write data from the stack, requires fewer instructions, and data does not need to be moved frequently.

#int a = 1
const/4 v0, #int 1 // #1  
#int b = 2
const/4 v1, #int 2 // #2
#int c = a + b
add-int v2, v0, v1
The executed bytecode is different
  • In Java, a Java class will be compiled into one or more .class files, packaged into a jar file, and then the JVM will obtain the corresponding bytecode through the corresponding .class file and jar file.
  • DVM will use the dx tool to convert all .class files or jar files into a .dex file, and then DVM will read instructions and data from the .dex file.

insert image description here

The .jar file contains multiple .class files, and each .class file contains the constant pool, class information, attributes, etc. of the class. When the JVM loads the .jar file, it will load all the .class files inside. This loading method of the JVM is very slow and is not suitable for mobile devices with limited memory.

The .dex file also contains a lot of class information, but dex integrates multiple class information, and multiple classes reuse constant pools and other areas.

ART

ART (Android Runtime) was released by Android 4.4 to replace Dalvik virtualization. Android 4.4 still uses DVM by default, and the system will provide an option to enable ART. In Android 5.0, ART is used by default.

The difference with DVM

During the installation process of the application under Dalvik, an optimization will be performed to optimize the dex bytecode to generate an odex file.

ART is compatible with bytecode execution in Dalvik. However, ART introduces the pre-compilation mechanism (Ahead Of Time). When installing an application on a device below Android 4.4 to Android 7.0, ART will use the dex2oat program to compile the application and compile the dex in the application into local machine code. But this process results in slower app installation.

Therefore, starting from Android N (Android 7.0), ART mixes AOT, JIT and interpreted execution:

1. No AOT compilation is performed when the application is initially installed (to avoid slow installation), and it is interpreted and executed during the running process. JIT is performed on frequently executed methods, and the JIT-compiled methods will be recorded in the Profile configuration file.

2. When the device is idle and charging, the compilation daemon will run, and perform AOT compilation of commonly used codes according to the Profile file. It will be used directly in the next run.

at last

This interview question will continue to be updated, please pay attention! ! ! !
Friends who need this interview question can scan the QR code below to get it for free! ! !
At the same time, by scanning the QR code below, you can also enter the group to enjoy the service of the ChatGPT robot! ! !

Guess you like

Origin blog.csdn.net/datian1234/article/details/131808140