awesome! ByteDance Android post classic interview questions, the final battle

beginning

Netizens often ask two questions in Zhihu Q&A: "Is there any prospect for learning mobile development now?" "What else can be studied in development?". Netizens replied: "Now I am still learning mobile development, just like joining the national army in 49." In fact, these two questions correspond to the same phenomenon. Whether it is preparing to enter the industry or just entering the industry, or a relatively senior developer, the future of the mobile development career , There are some confusion, some anxiety. Why is this happening?

Interviewer : Tell me about the garbage collection mechanism

Me: …can solve the circular reference problem of reference counting by combining strong and weak reference counting. In fact, Android's smart pointer is implemented like this...

Smart pointer

Smart pointers are widely used in the entire Android project. You can see references of sp and wp types in the binder related source code:

 sp<IBinder> result = new BpBinder(handle);

 wp<IBinder> result = new BpBinder(handle);

sp is a strong pointer reference; wp is a weak pointer reference.

In Java, we don't need to care about object destruction and memory release. The GC mechanism will automatically identify and recycle useless objects, and smart pointers are a small GC implementation in the native layer.

Smart pointers identify useless objects by reference counting. Objects using smart pointers need to inherit from RefBase. RefBase maintains the number of strong references and weak references of this object.

The strong pointer sp overloads the "=" operator. When referring to other objects, the strong reference count is +1, and the strong reference count is -1 in the sp destructor. When the strong reference count is reduced to 0, the referenced object is destroyed. In this way, the automatic release of the object is realized.

When a weak pointer refers to other objects, the weak reference count is +1, and the weak reference count is -1 in the wp destructor. When the strong reference count is 0, the referenced object will be destroyed regardless of whether the weak reference count is 0 or not.

How to solve the circular reference problem

Only relying on the strong reference counting method will have the problem of circular references, which will cause the object to never be released. Weak references are specifically used to solve the circular reference problem:

If A strongly refers to B, then B needs to use weak references when referencing A. When judging whether it is a useless object, only the strong reference count is considered to be 0, and the number of weak reference counts is not concerned.

This solves the problem that the object cannot be released due to the circular reference, but this will cause the wild pointer problem: when B wants to access A through a weak pointer, A may have been destroyed, and the weak pointer to A becomes a wild pointer Up. In this case, it means that A does not exist anymore, and other operations such as re-creation are needed

Smart pointer customization rules

Smart pointers are not fixed "when the strong reference count is 0, the referenced object will be destroyed regardless of whether the weak reference count is 0", but the rules can be customized. RefBase provides the extendObjectLifetime() method, which can be used to set the rules of the reference counter. Different rules have different judgments on the timing of deleting the target object, including the following three rules:

  • OBJECT_LIFETIME_STRONG: The object will be destroyed only when the strong counter value in the memory space of this object is 0

  • OBJECT_LIFETIME_WEAK: The object will be destroyed only when the values ​​of the strong counter and the weak counter in the memory space of this object are both 0

  • OBJECT_LIFETIME_MASK: No matter whether these two counters are both 0, the object is not destroyed, that is, it is the same as a normal pointer, or you have to manually release the object by yourself

At last

Share an advanced learning route system for Android programmers who have been working for more than 1 to 5 years, hoping to be able to meet those who are still engaged in Android development but do not know how to improve themselves, and are still confused!

  • Alibaba P7-level Android Architect Technical Mind Map; Check for leaks and fill vacancies, and systematically study and improve ( click my GitHub for details )

  • **Full set of systematic high-level architecture videos; **Seven mainstream technology modules, video + source code + notes

If you need to structure the study notes map, you can click on my GitHub to receive

There are many technical experts and experts in the group. If you have any questions, you are welcome to communicate with all netizens. The group still shares high-level Android learning video materials and interview materials for free from time to time~

There are many technical experts and experts like the cloud. If you have any questions, you are welcome to communicate with all netizens. The group also shares high-level Android learning video materials and interview materials for free from time to time~

Guess you like

Origin blog.csdn.net/CHAMPION8888/article/details/112784787