Memory optimization for Android Apps
The modern day android developers are very pretty adept at developing android apps. But often it has been seen that memory utilization and optimization of the running app creates lot of problems in term's of performance metrics. So, we need a workaround to manage this sort of scenario where we can see the usage of memory while running the app.
In the other hand finding memory usage in Linux is an uphill task. The chance of getting the correct numbers is extremely low.
Here comes the technical part. We all know about Activity Manager. Now there is a method called getMemoryInfo(). It is the highest-level API for getting information about memory data.
It helps the application to know how close the system is coming for crossing the line of no memory for its background task.
Now in lower-level, we can use the Debug API to get raw kernel-level information about memory.
This is the low-level MemoryInfo structure with all of this data:-
public int dalvikPss;
public int dalvikPrivateDirty;
public int dalvikSharedDirty;
public int nativePss;
public int nativePrivateDirty;
public int nativeSharedDirty;
public int otherPss;
public int otherPrivateDirty;
public int otherSharedDirty;
Now what are these terms called "Pss", "PrivateDirty", and "SharedDirty"? Let’s have a look.
In Android Multiple process uses a lot of shared memory. So how much memory a processes uses and paging out to disk is not clear to us. The funniest mathematical part is if we take the entire physical RAM mapped in to each process and adds up all those processes, we get a number much greater than the actual total RAM.
Now here comes the term Pss number. It is a metric which kernel computes that takes into account memory sharing. Another interesting term is PrivateDirty, It is the amount of RAM inside process which can't be paged to disk.
While using adb we can use the command "adb shell dumpsys meminfo" which will give us a bunch of information about the memory. We can also get details on the name which we are using or the pid of a single process by using this command below "adb shell dumpsys meminfo system".
"MEMINFO" in pid 747 [system] :
'size' :- It is the total size in address space of a particular heap.
'allocated' :-It is the actual allocations in kb that heap thinks it has.
'free' :-It is the remaining size free that the heap has for its additional allocations.
'pss' and 'priv dirty' :- These are the specific to pages associated with each of the heaps.
There is another command called 'adb shell cat /proc/meminfo'. It gives total usage of memory of the system. One example is:-
MemTotal : 384244 kB
MemFree : 174527 kB
Buffers : 870 kB
Cached : 83214 kB
SwapCached : 0 kB
MemTotal is the total memory available to the kernel MemFree is the total RAM memory that is not being used at all. Cached is the RAM being used for filesystem caches and other such things.
If you use these little techniques while making your android apps you can make your app pretty much faster in terms of memory utilization.
[I am Chiradeep, I am passionate about Android Application Development at Connecting Dots. We are a Business & Technology Consulting and Digital Marketing. At Connecting Dots, we help startups and emerging companies build great businesses, we are also Magento solution partners and build great eCommerce portals]