learn from mistakes! Take you to fully understand the drawing process of View, the Android series of advanced learning videos

background

As usual, let’s briefly state my own. I was born in 1991 and worked in Shenzhen after graduating from three books in 164. I have a small company with a salary of 13k, no house, no car, and no household registration.

At that time, I felt that life was okay. My parents had pensions. Basically, I didn't need to worry too much. My girlfriends had been together for a long time and were very considerate. They didn't ask me much.

I was satisfied when my life was going smoothly, but at the beginning of last year, something happened to my girlfriend's house, and I gave her all the savings. After the epidemic came, my family began to urge the marriage, and I felt the pressure.

The current wages cannot satisfy life. Although there has been a slight increase over the years, housing prices and prices have risen faster, so I decided to quit.

Since the end of last year, I started to cast my resumes. Looking back, I have cast a total of 33 resumes. Only 3 companies are interested, and 0 of them have interviewed.

(I was very sad at the beginning, but later... I got used to it later)

Then I spent a while, and suddenly realized that whether it was the economic winter or my winter, it was all here.

Interviewer : Tell me about the garbage collection mechanism

Me: …can solve the circular reference problem of reference counting through the combination of 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 the 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 problem of circular references:

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 it 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: Regardless of whether the 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

Finally, here is the information I reviewed during this period. This information was also shared by a friend by accident. It contains the analysis of the real interview questions of Tencent, ByteDance, Ali, and Baidu in 2020-2021, and organizes each technical point. Become a video and PDF (knowledge context + many details).

There are also  advanced architecture technology advanced mind maps and advanced advanced architecture materials to  help you learn and advance. Here I also share it with you for free and save you time searching for information on the Internet to learn, or you can share it with friends around you to learn together.

Friends in need can click: ** Android Interview Information **Get it for free~

Let's encourage each other~

613960897049)]

[External link image is being transferred...(img-W6OT0pUf-1613960897053)]

Let's encourage each other~

Guess you like

Origin blog.csdn.net/a120464/article/details/113930873