Brush the interview questions for entering Ali-five questions a day (cheer for workers)

Brush interview questions for entering Ali-10 questions a day (Come on and hit workers)

The following topics are all understood by yourself, if there are omissions or mistakes, please point them out! Come on!

JAVA basics

1. JVM memory allocation principle, garbage collection principle

Insert picture description here


The method area and heap are shared by all threads, and the stack, local method stack and program virtual machine are private to the thread.

In the jvm running structure, there are heap, method area, local method stack, program counter, stack area
heap : all the memory of objects created by new are allocated in the heap, and this is where the garbage collection is mainly performed.
(Jdk1.7 is divided into the new generation, the old generation and the permanent generation
jdk1.8 is divided into the new generation, the old generation and the metaspace metaspace are
not in the virtual machine, but use local memory. Therefore, by default , the metaspace The size is only limited by local memory)
Method area : used to store class information, constants, and static variables that have been loaded by the virtual machine, such as static modified variables are loaded into the method area when the class is loaded

Local method stack: store native c++ methods for invocation.
Program counter: thread private, record the address of the instruction, to ensure that the program (understood as a process in the operating system) can be executed continuously down the
stack: in charge of the operation of the Java program, it saves the method Local variables (8 basic data types, reference addresses of objects), partial results, and participate in method call and return.

Insert picture description here


Garbage collection is because objects are created during the running of the program. Objects occupy memory. One day there will be insufficient memory. Therefore, garbage collection is needed to manage unused objects.
According to the different life cycles of objects, different memory spaces are divided into different memory spaces for easy management and Recycle

JVM is divided into Cenozoic and Old Era. The size ratio of Cenozoic to Old Era is 1:2. The Cenozoic is divided into Eden Zone and Survival Zone. The memory ratio of Survival and Eden Zone is 1:1:8.
1. Object When created, it will be placed in the Eden Park. Slowly, the Eden memory will be insufficient. At this time, youngGC will be performed. At this time, objects not referenced by the root node will be destroyed, and the others will be placed in Survival Area 0, and new objects will be loaded into Eden Zone
2. When Survival Zone 0 is full, garbage collection will be carried out. At this time, the objects of the surviving Eden Zone and Survival Zone 0 will be placed in Survival Zone 1, and then the two surviving zones will be exchanged. That is, keep the survival area 1 empty.
3. When the objects in Eden Park and Survival Area 0 are not enough to be placed in Survival Area 1, they will enter the old age, and the age of the objects that have experienced a GC will increase by 1. When the objects that are still alive after 15GC will go to the old age
4. In the retirement area, it is relatively leisurely. When the old-age area memory is insufficient, GC: Major GC is triggered again to clean up the old-age area memory.
5. If the senior care area performs Major GC and finds that the object still cannot be saved, an OOM exception will occur

What kind of object will be marked as garbage
when an object is no longer referenced by any surviving objects
1. Reference counting algorithm
Reference counter attribute. Used to record the situation where the object is referenced. Being referenced ++, reference invalidation-
unable to solve the circular reference problem, that is, A refers to B, B refers to A

2. The reachability analysis algorithm
treats the object and its reference relationship as a graph, selects the active object as the GC Roots, and then tracks the reference chain. If an object and GC Roots are unreachable, that is, there is no reference chain, Then it can be considered as a recyclable object.
What are Roots?
Objects referenced in the virtual machine stack,
such as: parameters, local variables, etc. used in the methods invoked by each thread.
Objects referenced by JNI (usually referred to as local methods) in the local method stack Objects referenced by
static properties of the class For
example: Java class reference type static variables
Objects referenced by constants in the method area For
example: References in the String Table
All
references within the Java virtual machine of the object held by the synchronization lock synchronized .
The Class object corresponding to the basic data type, some resident exception objects (such as: NullPointerException, OutOfMemoryError), and the system class loader.
JMXBean that reflects the internal situation of the java virtual machine, callbacks registered in JVMTI, and local code cache

What algorithms are used in the old and young generations, and why? The
new generation: replication algorithms

Old age: marking and sorting algorithm The
marking phase is the same as the marking and cleaning algorithm. After marking, instead of cleaning up the objects, the live objects are moved to one end of the memory. Then clear the objects outside the end boundary

2. The difference between the underlying structure of ConcurrentHashMap, hashMap, and hashTable

HashMap is a structure of an array plus a linked list, which performs a hash operation on the number, then modulates the length of the array and puts it into the corresponding bucket. When hashMap is stored, null values ​​are allowed, but the thread-unsafe
hashTable adds synchronized to the method, allowing only single-threaded execution. Relatively speaking, the execution efficiency is not hashMap, but thread safety can be guaranteed.
ConcurrentHashMap is on the basis of hashMap, plus cas and synchronized to achieve thread safety, and can support concurrent processing, using a segmented lock mechanism, each thread is responsible for a region, and it is more efficient when performing operations.

When storing data, it will first XOR the high and low bits of the data to calculate the hashcode, then divide by the length of the array and put it into the corresponding bucket (bucket), if there is no value at the current position, put it directly, if there is a value, use equal The comparison value, if it is equal to cover, if it is not equal, use the zipper method to traverse backward and put it to the last position

Expansion factor 0.75 Initialize capacity16, and the maximum allowable storage threshold is 12. After reaching this threshold, the capacity will be expanded.
For expansion, the bitwise AND of the original capacity and the current element will be calculated. If the value is 1, the original array length will be added when shifting. If it is 0, lower it to the original position.

3. Several ways of multithreading creation, the principle of field pool and the type of thread pool you often use

1. Inherit the Thread class and rewrite the run method
. 2. Implement the Runnable interface and rewrite the run method
. 3. Create a thread through the Callable and Future interfaces.

The working principle of the thread pool:
1. After the thread pool is created, wait for the submitted task request
. 2. When the execute() method is called to add a request, the thread will make a judgment.
2.1 If the number of running threads is less than corePoolSize, then it will be created Thread to run this task

Insert picture description here

ThreadPool

4. The role and difference of the two keywords synchronized and volatile

5. The principle of Spring AOP bottom layer implementation, dynamic proxy

6. Design mode, focus on dynamic agency mode, chain of responsibility mode

7. The order of Spring IOC class loading

Microservices/distributed framework

1. The difference between http protocol and RPC protocol

2. The difference between the http protocol methods POST, GET, PUT DELETE 4 methods

3. Talk about the microservice framework used (spring-cloud or dubbo), and talk about the common components and functions of the microservice framework

4. How to deal with distributed locks and distributed transactions

5. Whether Redis can be persisted, and how to do Redis cluster expansion

6. What are the commonly used message queues (MQ), and how does the message compensation mechanism work?

database

1. How to do mysql query de-duplication

2. What does mysql select * from table for update mean (row lock)

3. What is the difference between the two keywords mysql union and union all

4. What are the general steps of SQL optimization, how to look at the execution plan (explain keyword), and how to understand the meaning of each field.

5. MYsql index principle, what are the types of indexes, how to create a reasonable index, and how to optimize the index.