I was recently asked some questions in an interview about Android. I searched for some of them but I couldn’t find the suitable resources and answers. So I wanted to share it with you here.
-
What is the preferred layout to use in Android ( for Better memory consumption or so ) ?
I didn’t have answer for that and the interviewer told me it is relative layout. Is this true ? any explanation for that ? -
Tell me some practices you do for better memory consumption ?
I had a look here but it seems there are other stuff. because the interviewer mentioned some things related to static variables are better. -
If Android needs memory, will it kill a service or Activity ? a matter of priority issue. I also didn’t find any one discussing this. The interviewer said some thing about Service has high priority (??) than activity so the activity is the component which will be killed. Is this true ? any further resources or explanations ?
Kindly share any knowledge or resources you know about this issues.
Answering one by one:
Nr. 1
Sounds wrong.It’s wrong to say thatRelativeLayoutis always faster than any other layout. What makes a layout “fast” or “slow” is how long it takes to calculate the positions and sizes for all it’s children. So when you’re only displaying 15 rows ofTextView, one below the other, aLinearLayoutwould most certainly be faster (and less cumbersome to use).Generally, I would advice to use the layout that best suites your situation.
Nr. 2
Preferring static variables has the “advantage” that they are initialized (and therefor in memory) only once. But this is more of a design decision than a performance one.
You should avoid huge nested collections in memory (such as
List<List<HashMap<?,?>>), but this should really be common sense. The thing with object creation is, that if you create many objects and don’t keep any references to them, they’ll get garbage collected. This will add runtime-overhead to your application.Nr. 3
This is both right and wrong. Services can be started with different priorities. But, before anything used by your application (be it a service or an activity) gets killed, backgrounded applications and their resources will be freed.
For Services, the documentation gives multiple hints:
On Activities, the following is listed:
So, for Activities, it depends on the current state how likely it is for it to be killed.
Conclusion
A quote on optimization by “M. A. Jackson”:
Not using a particular platform feature because it is “too slow” is often a bad idea. Google and Oracle take great care that their standard libraries are as
optimized as possible. Let the experts worry about things like this.