Android performance tuning! 100% of Alibaba's interviews will ask about the JVM, and have already taken an offer to join the job

Lead

Do you want to do Android development, how do you learn systematically? You only need to read this one! ! Why is this one enough? Now CSDN, Zhihu, Nuggets, and GitHub are endlessly popping up. They have been deeply involved in Android development for many years and have summed up a lot of learning and dry goods. But at the same time, there are also many bloggers who are not conscientious, or they have lived out the essence of human beings and reproduced them everywhere like repeaters;

Either they simply talked about their work experience in a simple way, so that readers can't get the real useful information from the sea of ​​resources with uneven quality and high quality. The purpose of this article is very simple. In one sentence: Use the least time and the highest efficiency to make you clear: What do you need to learn if you want to do Android development? Where should you study?

The feature of this article is to present the tedious knowledge to the readers in the form of a series of topics. When you can follow the systematic learning knowledge points constructed by the blogger, congratulations, you have killed the BAT, Byte, NetEase and other major manufacturers. Almost 80% of Android developers. This kind of omniscient pleasure, does it feel that you have become a god in the eyes of others?

What are the common questions and answer ideas in the Android interview

table of Contents:

1. Network 2. Java
Foundation & Container & Synchronization & Design Pattern
3. Java Virtual Machine & Memory Structure & GC & Class Loading & Four References & Dynamic Proxy
4. Android Basic & Performance Optimization & Framwork
5. Android Modularity & Hot Fixing & Hot Update & Packing & Obfuscation & Compression
6. Audio and Video & FFmpeg & Player

1. Network

Network protocol model

Application layer : responsible for handling specific application details
HTTP, FTP, DNS

Transport layer : Provide end-to-end basic communication
TCP, UDP for two hosts

Network layer : control packet transmission, routing and other
IP

Link layer : operating system device drivers, network card related interfaces

The difference between TCP and UDP

TCP connection; reliable; orderly; byte-oriented; slow speed; heavier; full duplex; suitable for file transfer, browsers, etc.

  • Full duplex: While A sends messages to B, B can also send messages to A
  • Half-duplex: While A is sending messages to B, B cannot send messages to A

UDP connectionless; unreliable; disorderly; message-oriented; fast; lightweight; suitable for instant messaging, video calls, etc.

TCP three-way handshake

A: Can you hear it?
B: I can hear it, can you hear it?
A: I can hear it, let's start

Both parties A and B must ensure that: you can hear what I say; I can hear what you say. So it needs three handshake

TCP waved four times

A: I'm finished
B: I know, wait a minute, I may not finish
B: I'm finished too
A: I know, end it

After B receives the end of A message, B may not have finished speaking, so he can't reply to the end sign immediately, and can only tell A after speaking: I'm finished.

The difference between POST and GET

The Get parameter is placed in the url; the Post parameter is placed in the request body.
Get may be unsafe because the parameter is placed in the url

HTTPS

HTTP is a hypertext transfer protocol, which is transmitted in plain text; HTTPS uses SSL protocol to encrypt HTTP transmission data

HTTP default port 80; HTTPS default port 443

Advantages: Security
Disadvantages: time-consuming, SSL certificate charges, encryption capabilities are still limited, but much stronger than HTTP

2. Java Basics & Containers & Synchronization & Design Patterns

StringBuilder, StringBuffer, +, String.concat link string:

  • StringBuffer is thread-safe, StringBuilder is not thread-safe
  • +In fact, it is realized by StringBuilder, so the non-loop body can be used directly +, but the loop body cannot, because StringBuilder will be created frequently
  • String.concat is essentially a new String, which is inefficient and time-consuming to sort: StringBuilder <StringBuffer <concat <+

Java generic erasure

  • Generics related to class structure such as modified member variables will not be erased
  • Container generics will be erased

ArrayList、LinkedList

ArrayList

Based on array implementation, fast search: o(1), slow addition and deletion: o(n) with
an initial capacity of 10, and expansion through the System.arrayCopy method

LinkedList

Implementation based on doubly linked list, slow search: o(n), fast addition and deletion: o(1)
encapsulates the call of queue and stack

HashMap 、HashTable

HashMap

  • Based on the implementation of arrays and linked lists, arrays are the main body of HashMap; linked lists exist to resolve hash conflicts
  • When a hash conflict occurs and the linked list size is greater than the threshold, it will expand. JAVA 8 will convert the linked list to a red-black tree to improve performance.
    Allow key/value to be null

HashTable

  • The data structure is the same as HashMap
  • Value is not allowed to be null
  • Thread safe

ArrayMap、SparseArray

ArrayMap

1. Based on two arrays, one stores the hash; the other stores key-value pairs. When expanding capacity, only array copy is needed, no hash table reconstruction is required
. 2. High memory utilization
3. Not suitable for storing large amounts of data, because the key will be searched by dichotomy (below 1000)

SparseArray

1. Implementation based on two arrays, int as key
2. High memory utilization
3. Not suitable for storing a large amount of data, because the key will be searched dichotomously (below 1000)

volatile keyword

  • Can only be used to modify variables, applicable to modify variables that may be accessed by multiple threads at the same time
  • Equivalent to lightweight synchronized, volatitle can guarantee order (instruction reordering is disabled) and visibility; the latter can also guarantee atomicity
  • Variables are located in the main memory, each thread also has its own working memory, the variable has a copy in the working memory of its own thread, and the thread directly manipulates this copy
  • The variable modified by volatile will be synchronized to the main memory immediately after it is changed to maintain the visibility of the variable.

Double check singleton, why add volatile?

1. The problem that volatile wants to solve is that you want to use instance in another thread and find instance!=null, but in fact the instance has not been initialized yet.

2. Split instance = newInstance(); into 3 sentences. 1. Allocate memory 2. Initialize 3. Point instance to the allocated memory

3. Volatile can prohibit instruction reordering, make sure to execute 2 first, then execute 3

wait 和 sleep

  • sleep is a static method of Thread, which can be called anywhere
  • wait is a member method of Object, it can only be called in the synchronized code block, otherwise it will report IllegalMonitorStateException.
  • sleep will not release the shared resource lock, wait will release the shared resource lock

lock 和 synchronized

  • synchronized is a Java keyword, a built-in feature; Lock is an interface
  • Synchronized will automatically release the lock; lock needs to be manually released, so you need to write to the try catch block and release the lock in finally
  • synchronized cannot interrupt waiting for the lock; lock can be interrupted
  • Lock can improve the efficiency of multiple threads for read/write operations
  • When competition for resources is fierce, the performance of lock will be significantly better than synchronized

Reentrant lock

  • Definition: After the lock has been acquired, the synchronization code block is called again/when trying to acquire the lock, there is no need to reapply for the lock, and the relevant code can be executed directly
  • ReentrantLock and synchronized are both reentrant locks

Fair lock

  • Definition: The thread with the longest waiting time will get the lock first
  • Unfair locks cannot guarantee which thread acquires the lock. Synchronized is an unfair lock
  • ReentrantLock is a non-fair lock by default and can be set as a fair lock

Optimistic and pessimistic locking

  • Pessimistic lock : Once a thread gets the lock, other threads will hang and wait, which is suitable for scenarios with frequent write operations; synchronized is a pessimistic lock
  • Optimistic lock : Assuming that there is no conflict, no lock, judge whether the data is expired when updating the data, if it expires, the data will not be updated, suitable for scenarios with frequent read operations
  • Optimistic lock CAS : Compare And Swap, first compare whether the original value is equal when updating the data, if it is not equal, it means that the data is in the past, and the data is not updated
  • Optimistic lock implementation : AtomicInteger, AtomicLong, AtomicBoolean

4 necessary conditions for deadlock

  • Mutually exclusive
  • Hold and wait
  • Not preemptable
  • Loop waiting

synchronized principle

  • Each object has a monitor lock: monitor, the synchronization code block will execute monitorenter start, motnitorexit end
  • wait/notify relies on the monitor monitor, so IllegalMonitorStateException will be reported when executed in an asynchronous code block

3. Java virtual machine & memory structure & GC & class loading & four kinds of references & dynamic proxy

JVM

  • Definition: It can be understood as a fictitious computer, explaining that its own bytecode instruction set is mapped to the instruction set of the local CPU or OS. The upper layer only needs to pay attention to the Class file, which has nothing to do with the operating system, and realizes cross-platform
  • Kotlin can be interpreted as a Class file, so it can run on the JVM

JVM memory model

  • Java multi-threads communicate through shared memory, and each thread has its own local memory
  • The shared variable is stored in the main memory, and the thread will copy a shared variable to the local memory
  • The volatile keyword serves the memory model to ensure memory visibility and order

JVM memory structure

Thread private :

1. Program counter: Record the bytecode instruction address being executed, if the Native method is being executed, it will be empty
. 2. Virtual machine stack: When executing the method, save the data required by the method as a stack frame into the stack, and pop out the stack after execution
3. Native method stack: the same as the virtual machine stack, but for the Native method

Thread sharing :

1. Heap: store Java instances, the main area of ​​GC, the generational collection of GC method will divide the heap into new generation and old generation
2. Method area: storage class information, constant pool, static variables and other data

GC

Recovery area: only for the heap and method area; the data in the private area of ​​the thread will be destroyed with the end of the thread, and there is no need to reclaim

At last

Promising everyone’s preparations for gold, silver and silver, the real interview questions from Dachang are here!

This information will be published in various blogs and forums starting from the spring recruitment. Collect the high-quality intermediate and advanced interview questions for Android development on the website, and then find the best solution for the whole network. Every interview question is a 100% real question + the best answer. Package knowledge + many details.
Save everyone's time to search for information on the Internet to learn, and you can also share with friends around you to learn together.
Leave a thumbs up for the article and you can receive it for free~

Poke me to receive: 3000 pages of core knowledge notes of Android developer architects

"960 The most complete Android development notes in the entire network"

"379-page Android Development Interview Collection"

Including Tencent, Baidu, Xiaomi, Ali, LeTV, Meituan, 58, Cheetah, 360, Sina, Sohu and other first-line Internet companies interview questions were asked. Familiarity with the knowledge points listed in this article will greatly increase the chances of passing the first two rounds of technical interviews.

How to use it?
1. You can directly browse the required knowledge points through the catalog index, and check the missing points.
2. The number of five-pointed stars indicates the frequency of interviews, and represents an important recommendation index

"507 pages of Android development related source code analysis"

As long as it is a programmer, whether it is Java or Android, if you don't read the source code and only look at the API documentation, it will just stay on the skin. This is not good for the establishment and completeness of our knowledge system and the improvement of actual combat technology.

The one who can really exercise the ability is to read the source code directly, not only reading the source code of major systems, but also including a variety of excellent open source libraries.

Analysis of Real Questions in 2020-2021 Interviews of BAT Companies such as Tencent, ByteDance, Ali, Baidu, etc.

Data collection is not easy. If you like this article, or it is helpful to you, you may wish to like it and forward it. The article will continue to be updated. Absolutely dry goods! ! !

The establishment and completeness of the system and the improvement of actual combat technology are all disadvantageous.

The one who can really exercise the ability is to read the source code directly, not only reading the source code of major systems, but also including a variety of excellent open source libraries.

[External link image is being transferred...(img-tK2bwtKO-1613959651423)]

Analysis of Real Questions in 2020-2021 Interviews of BAT Companies such as Tencent, ByteDance, Ali, Baidu, etc.

[External link image is being transferred...(img-Ll1CwCUf-1613959651427)]

Data collection is not easy. If you like this article, or it is helpful to you, you may wish to like it and forward it. The article will continue to be updated. Absolutely dry goods! ! !

Guess you like

Origin blog.csdn.net/fanzhang_vip0723/article/details/113930025