Now kuaishou Bytedance and other companies are hiring a large number of people, the salary is good, but it is not so easy to get in, people know that the interview, these companies are eager to give you everything you know, so in-depth review knowledge points, make yourself a little bit more willing to ask. Here’s a look at the actual interview process for memory leaks:
1. What is memory leak
A memory leak means that an application cannot release the allocated memory, that is, the reclaimed object cannot be reclaimed. Generally, the lifetime of the object holding the reference is longer than the lifetime of the reference.
2. Which scenarios cause memory leaks
Resource objects that are not closed cause memory leaks, such as broadcasts that are not unregistered, EventBus that is not unregistered, file streams that are not closed, database Pointers that are not closed, and bitmaps that are not recycled when they run out.
Static modified member variables or singleton patterns, such as static modified context, can cause an activity to fail to be reclaimed because the modified object has the same lifetime as the app.
Holds static instances in non-static inner classes. Because non-static classes hold instances of external classes by default, holding static instances causes the lifetime of the inner class to be as long as the lifetime of the app, and thus the lifetime of the external class to be as long as the app and cannot be reclaimed. Simply change a non-static inner class to a static inner class.
3. What is the impact of memory overflow
One or two memory leaks doesn’t matter, but a large number of memory leaks can cause the system to continue to gc and cause the program to lag, while a large number of memory leaks can cause the system to run out of memory craH due to insufficient memory allocation.
4. How do I detect memory leaks
Mat analyzes memory integration leakcanery Detects memory leaks AndroidStudio Lint static code detects memory leaks
5. Please describe the process of MAT analyzing memory
When the program is running, open profiler on AS, select Memory, and perform operations on the phone, such as closing the corresponding activity, then click GC on AS, and click Dump. Generally speaking, AS can automatically analyze the hprof file and sort out the corresponding memory information. You can choose to sort by package. Then select our program to see which objects are still using memory. If objects that should be reclaimed are still using memory, a memory leak has occurred.
Then, if you do not want to analyze the Hprof file on AS, you can also export the hprof file and find MAT in Eclipse for analysis. However, MAT cannot recognize the hprof file and needs to use the hprof-conv command to convert it. The command is the output file of the hprof-conv source file. Then you can use MAT for memory analysis.
Histogran can see the number of buffers of different types and how much memory they occupy. Dominator Tree sorts objects according to their memory size. Then we can merge shortest Paths to GC roots. Then select the reference relationship to see which memory is leaking.
6. Is the principle of Leakecanery understood
If we want to monitor whether an object is recovered, we can use the combination of WeakReference and Referencequeue. When constructing WeakReference, we pass in a Referencequeue instance. When the object referenced by WeakReference is recovered, it will be stored in pending, and then a thread will read the pending queue wirelessly. The WeakReference will be added to the Referencequeue. At this time, we can find the Referencequeue queue. If there is such a WeakReference inside, it means that the corresponding object is recovered.
The App lifecycleCallbacks will be registered when the Leakcanery is initialized, and when the Activity executes onDestroy, Leakcanery will receive a callback and get a reference to the corresponding activity. At this time, the Watcher method of RefWatcher will be adjusted. In this method, a Runnable task will be started through AndroidWatcherExcetor, in which keyedWeakReference will be constructed, An activity object and an instance of referencequeue are passed in, and the task is delayed for 5 seconds.
When the activity is recovered, KeyedWeakReference will be added to the Referencequeue. If a memory leak occurs, the Referencequeue cannot find the KeyedWeakReference object, and then it can be judged that a memory leak occurs. Therefore, in this task, ensureGone method will be called to referencequeue to find whether there is a KeyedWeakReference object. If there is, it means that there is a memory leak and the activity is not recovered. Then GC method will be called again to recycle again. If there is still no KeyedWeakReference object in the Referencequeue at this time, then there must be a memory leak. Then heapDumper analysis prof file, And callback the results to the notification bar via HeampDumplister.
7. How does leakecanery know if there has been a memory leak?
As mentioned earlier, when the ReferenceQueue is passed into WeakReference, if the object reference held in WeakReference is recovered, the WeakReference object will be added to the ReferenceQueue queue. Using this principle, LeakCanary builds a keyedWeakReference instance when listening to the activity execute onDestroy, which inherits the keyedWeakReference and contains key and Name fields
When constructing keyedWeakReference instance, a key will be randomly generated and passed in, that is, the weak reference object is bound to the key. At the same time, this key is stored in Set retainedKeys. When the ensureGone method is executed, the WeakReference object will be searched in the keyedWeakReference team first. If there is WeakReference object, it means that it has been successfully recycled, and the corresponding key will be removed from retainedKeys. Then check whether there is still the WeakReference. Key in retainedKeys. If it does not exist, it means it has been successfully recycled; if it still exists, it means it has not been successfully recycled. A memory leak detection task was delayed by 5 seconds, giving the activity enough time to reclaim. If the activity fails to reclaim at this point, it must have leaked memory.
8. In which thread does the task of detecting memory leaks occur?
Through looper.myQueue ().addidleHandler (new Messagequeue.idleHandler ()), wait until the main thread is idle and send it to the child thread with a delay of 5 seconds.
9. What is memory overflow
Memory overflow refers to an OOM exception caused by insufficient available memory or allocated memory during application. Running out of memory can result if you run out of memory too much.
10. Memory overflow usually occurs in which part of the JVM
Virtual machines in the JVM program counter, stack, local method stack is born with the thread, along with the thread, stack frame with the method of entry and exit did into and out of the stack processing, to achieve the memory automatic cleaning, so general will occur on a heap of memory and method, the pile area for heap area used to store instance of an object, is the main management area, the garbage collector And manage objects by generation; The method area generally stores the class information loaded by the VIRTUAL machine, constants, static variables, compiled code and data. GC mainly collects the constants and unload the class in the method area. Memory leaks can occur in both areas.
These are common interview questions, I sorted out to share with you. I also put together a PDF document from Github’s 8.3K star Android performance optimization tutorial and a hands-on project.
Data all sorted out down to 722 pages, covering design ideas and code quality optimization, program performance optimization, development efficiency optimization, Github high star optimization actual war project analysis, which explains in detail the startup optimization, layout optimization, memory optimization, screen adaptation, OOM problems and so on. Interested friends [attention + like + simple letter] free access.
Github High star optimization actual combat project analysis
1. Startup speed 2. Fluency 3. Practice of Douyin in APK package size resource optimization 4. Network optimization 6. Mobile Taobao Double 11 Performance optimization project Revealed 7. Autonavi APP full link source code dependency analysis 8. Completely kill OOM combat experience sharing 9. Wechat Android terminal memory optimization practice
Design ideas and code quality optimization
1. Six principles 2. Design patterns 3. Data structures 4. Algorithms
Program performance optimization
Optimization of startup speed and execution efficiency 2. Layout detection and optimization 3. Memory optimization 4. 5. Network transmission and data storage optimization 6.APK size optimization 7. Screen adaptation 8.OOM Troubleshooting 9.ANR troubleshooting 10
Optimization of development efficiency
1. Git distributed version control system 2. Gradle automated build system
If you are stuck with a lack of learning resources, then I can help you. The above knowledge notes are all free to share, if you need to get the knowledge notes, [click me] to read and download. * *.
In addition, attach a collection of major factory interview questions (with answers), the need of friends【 Click on me 】Read and download.