Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there memory management tips to take care of when making Android Apps?

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.

  1. 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 ?

  2. 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.

  3. 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.

like image 343
MSaudi Avatar asked Aug 04 '12 22:08

MSaudi


People also ask

How memory management is used in Android application?

The Android Runtime (ART) and Dalvik virtual machine use paging and memory-mapping (mmapping) to manage memory. This means that any memory an app modifies—whether by allocating new objects or touching mmapped pages—remains resident in RAM and cannot be paged out.

How much RAM should an Android app use?

Simple apps and games will use a few hundred megabytes, while more sophisticated games might use up to a gigabyte of RAM. The most demanding games can use up to 1.5GB of RAM. With 4GB of RAM, there's enough room for several medium games or apps, along with the OS, to reside happily together.

Does Android Studio use a lot of memory?

1–1.5 gb will be consumed by most of your OS and processes running parallely. So with android studio basically you'll see that 80–85% ram is being used if you have 4gb ram. In case of 8gb it's more than enough.

How do you manage memory in Android?

Overview of memory management. The Android Runtime (ART) and Dalvik virtual machine use paging and memory-mapping (mmapping) to manage memory. This means that any memory an app modifies—whether by allocating new objects or touching mmapped pages—remains resident in RAM and cannot be paged out.

What is the best memory management app for Android?

Top 5 Android Memory Management Apps on Phone 1 SanDisk Memory Zone. SanDisk Memory Zone gives you the freedom to manage the cloud storage of your phone, SD card and Android device. 2 Auto Memory Manager. Auto Memory Manager is an easy to use and effective application. ... 3 Memory Manager. ... 4 GO Speed. ... More items...

What are the worst memory management mistakes an Android app can make?

Leaving a service running when it’s not needed is one of the worst memory-management mistakes an Android app can make. If your app needs a service to perform work in the background, do not keep it running unless it needs to run a job. Remember to stop your service when it has completed its task.

What is memory-mapping in Android?

The Android Runtime (ART) and Dalvik virtual machine use paging and memory-mapping (mmapping) to manage memory. This means that any memory an app modifies—whether by allocating new objects or touching mmapped pages—remains resident in RAM and cannot be paged out.


1 Answers

Answering one by one:

Nr. 1

Sounds wrong. It's wrong to say that RelativeLayout is 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 of TextView, one below the other, a LinearLayout would 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:

The Android system will attempt to keep the process hosting a service around as long as the service has been started or has clients bound to it. When running low on memory and needing to kill existing processes, the priority of a process hosting the service will be the higher of the following possibilities: [Full List]

On Activities, the following is listed:

An activity has essentially four states:

  • If an activity in the foreground of the screen (at the top of the stack), it is active or running.

  • If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused. A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations.

  • If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere.

  • If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.

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":

We follow two rules in the matter of optimization:
Rule 1: Don't do it.
Rule 2 (for experts only): Don't do it yet - that is, not until you have a perfectly clear and unoptimized solution.

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.

like image 63
Lukas Knuth Avatar answered Sep 20 '22 06:09

Lukas Knuth