Memory optimization based Android interview questions and answers

Memory Leaks
1) How do you find memory leaks in the mobile app on the Android platform?

Answer: Android Studio is using Android Device Manager (ADM), this ADM is used to detect the memory leaks in the Android platform.

When you open ADM in the Android Studio then on the left-hand side of the ADM, you will find your device or emulator in which a heap sign will be displayed. When you are running any mobile app then you will see the heap size, memory analysis and other statistics displayed on it.

CAN YOU PROVIDE SOME IDEAS ON HOW TO PREVENT MEMORY LEAKS INU YOUR APP?

There are many strategies that can be applied. Here there is just a small set of ideas.

-Is better to use an Application Context rather than an Activity Context ,

since Activities are more likely to be leaked.

-Is generally good to avoid having long-lived references to Activities.

-Is better to avoid non-static inner classes in Activities unless we control their

lifecycle. Is better to use static inner classes with weak references, so they can’t be

collected when they are not used it.

There is a library published by Square called Leak Canary[16]. A

candidate with knowledge about this library will likely have

experience fixing memory leaks

DO YOU KNOW THE FUNCTION ONTRIMMEMORY()?

This is a callback called when the OS decides that is a good moment to optimize

memory from the running processes. For example, when an Activity goes in the

background and there is no enough memory to keep alive all the running processes.

There are different level for onTrimMemory , and it can be retrieved

with ActivityManager.getMyMemoryState(RunningAppProcessInfo).

DESCRIBE HOW AN OUTOFMEMORYERROR HAPPENS IN ANDROID?

As the application progresses, more objects get created and heap space is expanded to accommodate new objects. The virtual machine runs the garbage collector periodically to reclaim memory back from dead objects. The VM expands the Heap in Java some where near to the maximum heap size, and if there is no more memory left for creating new object in java heap, it will throw a java.lang.OutOfMemoryError killing the application Before throwing OutOfMemoryError the VM tries to run the garbage collectorto free any available space but if even after that there is still not much space available on Heap in Java, it will result into an OutOfMemoryError .

WHEN WILL BE AN OBJECT ELIGIBLE FOR GARBAGE COLLECTION?

When there is no reference alive for that object or if any live thread can’t reach it.

Garbage collection thread is a daemon thread which will run upon a complex GC algorithmand when runs it collects all objects which are eligible for GC.

A solid candidate will be able to answer that a cyclic reference doesn’t count as a live reference, and if two objects are pointing to each other and there is no live reference for any of them, then both are eligible for GC.

WHAT CAN HAPPEN IF AN STATIC VARIABLE IS POINTING TO AN ACITIVITY CONTEXT?

Very likely a memory leak when the Activity disappears.

How memory trimming can be done in Android?

Implement ComponentCallbacks2, register it using registerComponentCallbacks on your Activity, Service, ContentProvider, or Application and listen for onTrimMemory callback.
onTrimMemory is called with different memory levels some of them are,
TRIM_MEMORY_RUNNING_MODERATE : The device is beginning to run low on memory
TRIM_MEMORY_RUNNING_LOW : The device is running much lower on memory
TRIM_MEMORY_RUNNING_CRITICAL : The device is running extremely low on memory
Reference
http://developer.android.com/reference/android/content/ComponentCallbacks2.html

How do you find memory leaks in android application?

– There are lots of classes and components who are responsible for application memory leaking :-
* static classes- if you are using static classes into your apps and do not clearing their instance than m/m is leaking
* static view -also leaking m/m in our apps
* Inner classes its also same like static classes .
* anonymous class
* Handler -handler also responsible for m/m leaking if you are not using in proper way.
* Thread ,Task & Timer Tasks
* Sensors
* Services and Intent services

App size optimization

How to Reduce APK size in android?

  • Remove unused sources
  • Use of Vector Drawables
  • Reuse your code
  • Compress PNG and JPEG files
  • Use of Lint
  • Use images in WebP file format
  • Use of proguard
  • Use of ShrinkResources
  • Limit the usage of external libraries
  • Use the Android Size Analyzer tool
  • Generate App Bundles instead of APK
  • Use of Resconfigs

To read more, refer to the article: How to Reduce APK Size in Android?


Aaaa


Comments

Popular posts from this blog

Jetpack Compose based Android interview and questions

Null safety based Kotlin interview questions and answers

CustomView based Android interview questions and answers