Android technology articles! Android basic interviews often die on these questions, the interviewer series!

beginning

Recently, an old iron told me that after working for a month, he regretted that he was anxious to join the company. He used to do mobile phone research and development at Meitu. This year, Meitu also had a wave of organizational optimization adjustments this year. He was one of them. After negotiating to leave his job, he was anxious to find a job to work because he had a mortgage and could not live without a source of income. So I hurriedly selected a company, which is actually a large outsourcing company, mainly dispatched to other mobile phone manufacturers to do outsourcing projects. **At that time, I promised that the treatment was not bad, so I started to work immediately. But after I joined the company, I found that the salary package was not what HR said, and that HR had already run away. **Recently, he said he was very distressed and changed jobs to see opportunities. Few companies gave interview opportunities. Many HRs asked him how he went from a big factory to outsourcing. . . In this case, I'm honest and don't know how to suggest (see if you have any suggestions? You can leave a message), this is a problem for me. Because changing jobs frequently is a taboo, especially when changing jobs gets worse. .

What is a memory leak?

A memory leak that is ML (Memory Leak)a phenomenon in the application program memory, when the memory do not need to use but can not be returned to the program released &

What are the memory leaks and the corresponding solutions?

In the final analysis, the cause of the memory leak is that when the memory of the variable that needs to be recovered is held by other variables, the memory recovery fails.

Common reasons are:

1. Non-static inner class/anonymous class

  • Reason 1: 非静态内部类/匿名类 HandlerHolding Activity/Fragmenta reference to an external object, causing Activity/Fragemntit to not be recycled when it is destroyed

  • solution:

    1. Change the Handler class to a static inner class + weak reference ( WeakReference) to hold Activityreferences (static classes do not hold external references by default)
    2. When the external class (usually Activity/Fragment) ends its life cycle, clear the Handler queue
  • Reason 2: 非静态内部类的实例(对象)= 静态实例(its life cycle = application life cycle)

  • solution:

    1. Change the class to static class (static class does not hold external references by default)
    2. Extract the class and encapsulate it as a singleton
  • Reason 3: The external class needs to be destroyed when the multi-threaded worker thread is processing a task. At this time, the worker thread holds a reference to the external class and cannot be recycled

  • solution:

    1. Change the thread class class to static class (static class does not hold external references by default)
    2. Force the end of the thread when the external class ends its life cycle

2. Collection

  • Reason: The collection class holds a reference to the collection element after adding an element, which causes the collection element to be uncollectible, resulting in a memory leak

  • solution:

    1. After the collection class has used the element object, the element must be deleted from the collection (because there are multiple elements in a collection, the simplest solution is to empty the collection object (clean) & set to Null)

3. The resource object is not closed after use

  • Cause: The resource object is not closed after use, there is no off / log off when these resources are destroyed Activity / Fragemnt, will not lead to recovery such as: 广播BraodcastReceiver, 文件流Fire, 图片资源Bitmap, 数据库游标etc.

  • solution:

    1. ActivityTimely close/deregister resources when destroyed

4.Static keyword modifies member variables

  • The reason: Static keyword modified member variable life cycle = life cycle applications such as private static Context mContext = contextso context corresponding to the context Activitycan not be recovered

  • solution:

    1. Try not to use Static member variable references, use weak references instead of strong references
    2. Use singleton pattern

4. Other uses

  • Reason 1: Context: When Activity contextthe referenced object is still in use, it Activitycannot be recycled when it needs to be destroyed

  • solution:

    1. The right contextreference should not exceed its life cycle
    2. Use ApplicationContextinstead
  • Reason 2: WebView: The WebView object is not destroyed in time after it is no longer used, resulting in memory usage

  • solution:

    1. WebViewDestroy objects that are no longer used through multiple threads
  • Reason 3: Adapter: When sliding ListView and RecycleView to obtain a new View, a View object is re-instantiated in getView(), which wastes resources and makes the memory usage larger and larger

  • solution:

    1. Use cachedconvertView
    2. Use directlyViewHolder

At last

I hope that everyone will have a good attitude. You have to think clearly about what kind of company you want to enter. It is not necessarily a large company, and I am not a mega company. Of course, if you don’t know the choice or plan, choose a big company! I hope that we can choose the company we want to go to before investing or recommending it, instead of having a company that wants me to go! Also, don’t be afraid, don’t be pressured, just treat it with your heart, but be prepared. Finally, I hope everyone can get a satisfactory offer! If you currently have a job, please cherish it and work hard. Finding a job is actually quite tiring and hard.

Attached here are dozens of sets of ByteDance related to the above-mentioned interview questions, interview questions from JD.com, Xiaomi, Tencent, Toutiao, Ali, Meituan and other companies in 19 years. The technical points are organized into videos and PDFs (in fact, a lot of effort was spent than expected), including knowledge + many details.

Due to limited space, I will show you a small part in the form of pictures. You can click GitHub to get it for free

95%E6%8B%BF%E9%AB%98%E8%96%AA%EF%BC%81.md)

[External link image is being transferred...(img-bZkjXAuA-1611298812218)]

Guess you like

Origin blog.csdn.net/Sunbuyi/article/details/112985353