Câu hỏi phỏng vấn Android
Câu hỏi

What are some best practices to...

Câu trả lời

Here are some best practices to avoid memory leaks in Android apps:

  1. Avoid holding references to Android Context objects statically. If a Context object like an Activity or Fragment is held statically, it can prevent the object from being garbage collected even after it has been destroyed. Use WeakReference to hold references to Context objects instead[1][3].

  2. Unregister callbacks and listeners when they are no longer needed. For example, unregister BroadcastReceivers in the onPause() or onDestroy() methods of an Activity or Fragment[3][4].

  3. Be careful with threads and asynchronous tasks. Threads that outlive the Activity or Fragment that created them can cause leaks. Always cancel async tasks in onPause() or onDestroy()[3].

  4. Avoid using non-static inner classes, especially in Activities and Fragments. Non-static inner classes hold an implicit reference to the outer class instance, which can lead to leaks if the inner class lives longer than the outer class[1].

  5. **Use memory profiling tools like Android Studio's Memory Profiler, LeakCanary, or Eclipse Memory Analyzer (MAT) to detect and diagnose memory leaks during developm...

senior

senior

Gợi ý câu hỏi phỏng vấn

middle

What is Intent Filter?

middle

Explain how HashMap works

senior

What is a Sticky Broadcast ?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào