Really fragrant series! Dachang's classic high-frequency interview questions are systematically collected, shocking and free download!

beginning

For programmer interviews, in addition to the interview technology, some companies often ask applicants questions that have nothing to do with technology to test the applicant’s comprehensive ability and use this as a basis for hiring. In many cases, there are often no standard answers to such questions. It depends on how well the candidate responds on the spot.

Zhang Gong is an Android developer. Recently, he went to an interview with a well-known Internet company. At the end of the interview, the interviewer asked Zhang Gong this question:

Do you think there is a future for Android?

Zhang Gong was caught off guard by the question.

I'm talking about the cold winter of the Internet, especially the mobile terminal development market is becoming saturated. When I was looking for a job, I continued to look for it for a month. It should be my technical level.

Is there a future for Android now? Some people say that Android development prospects are getting worse and worse. I think it is one-sided. Android development prospects are actually pretty good.

Nowadays, major companies are in fact short of senior Android development engineers, and some companies are still hiring people for Android development positions.

**For junior Android developers, self-discipline and perseverance are the ability to continue to draw a gap in life with others. **Junior developers should think about how to master the knowledge points, rather than change careers easily. If they don't have the spirit of specializing, no matter which direction they change, such as the popular big data development, they will be stuck in the bottleneck period.

For Android developers, some of them have graduated from computer-related majors, and some have participated in IT training for a period of time, and then found an internship or junior Android engineer position. They have longing and hope to show themselves well in the company. Therefore, I study while working at work, and I don’t forget to continue to recharge myself after get off work.

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

Give everyone a complete set of Android learning materials.

In the past, I had been looking for things on the Internet. The things I found were scattered. Many times I just watched it and disappeared. Time was wasted, but the problem has not been solved, which is very maddening.

Later, I compiled a set of information by myself, let alone, it's so fragrant!

The information is organized, systematic, and comprehensive. It is not convenient for me to directly release it. You can first see if there is any use.

Attached the address of the prostitute: "Android Architecture Video + BATJ Interview Special PDF + Study Notes"

Series of tutorial pictures

2020Android review data summary.png

flutter

NDK

Open source framework for design ideas

[External link image transfer...(img-pyqMviV7-1611302096780)]

[External link image is being transferred...(img-Ed61HSAI-1611302096783)]

WeChat Mini Program

Guess you like

Origin blog.csdn.net/dajie1213/article/details/112987135