Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

How to do Memory Optimizations in Android

714 views

Published on

Even though android is powerful mobile operating system platform still there are few pitfalls. One of them is memory optimization to ensure user never run out of space and do you know how to do it.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

How to do Memory Optimizations in Android

  1. 1. Android Memory Optimization Best Practices for Better Memory Management
  2. 2. Two types of memories in Android Clean RAM Android makes memory available to the system through paging and memory- mapping techniques which is clean RAM. Files and resources are kept in mapped pages which can be easily recovered from disk hence these can be paged out and memory can be released for the system. Dirty RAM Dirty RAM is one that is quite expensive when runs in background since they cannot be paged out. Maximum memory inside a running application is dirty memory and it is used for memory optimization.
  3. 3. Android Memory Overview All applications are recognized by android either as running process or cached process. Android kills one or multiple cached processes when there is need of memory for running process. According to LRU (least recently used) process under cached process are killed.
  4. 4. Memory Optimization: Best practices for enhanced memory usage When the system is not running out of memory then it is good for boosting the overall memory usage and performance of Android and eventually ensure a delightful user experience.
  5. 5. Avoid Memory Leaks at all cost ✓ Main reason for memory leak is to forget to free up the allocated memory before all related references go out of the scope. Memory leaks happen when references to the objects that are no longer needed in an app are not released and this type of memory leaks are known as logical memory leaks. ✓ A very strong Logical memory leaks is the result of not releasing references to those objects that are no more required in app. When a strong reference to an object is still present then even the garbage collector fails to remove the object from the memory
  6. 6. Avoid Memory Leaks at all cost
  7. 7. Avoid Memory Leaks at all cost It can be avoided by following ways: ✓ Once the database query is done always close the cursor. ✓ Remember to call unregisterReceiver() after calling registerReceiver(). ✓ Significant memory loss is caused by Activity instance leakage and it also increases the OOM(out of memory) value. This can be avoided as following: ✓ Don’t keep the long-lived references to a context-activity. ✓ Replace the context-activity with context-application. ✓ Use AsyncTask for short-lived operations.
  8. 8. Profile guided Dex file layout
  9. 9. Profile guided Dex file layout Application is wrapped in an APK file where multiple dex files (contains instruction for application execution) are present and when an application is launched on the device, the dex file is read by device into memory with several hundred pages being loaded into the memory. Usually, developers are not aware about important use cases required by a specific user hence, there are all a lot of unimportant things present along with important things.
  10. 10. Profile guided Dex file layout To improve the Dex file layout use the JIT profile information for moving important things close to each other as shown below:
  11. 11. Profile guided Dex file layout Below snapshot showcase a significant reduction in RAM usage by dex file after layout.
  12. 12. Code Sinking In this type of optimization instructions are moved closer to those instructions that actually use them. Instructions that are rarely used need to be moved closer to where they are used. It basically means instruction not in the regular flow should be moved to the exceptional cases.
  13. 13. Code Sinking
  14. 14. It is a common optimization technique in object oriented language a runtime will try to infer classes and methods that can be made final even though they aren't final actually. Having this information internally gives a lot of room for the compiler for more optimizations. Class Hierarchy Analysis
  15. 15. Class Hierarchy Analysis
  16. 16. Loop Optimization Programs tend to spend most of the time inside loops. Loop optimization has an analysis part where we look into the program and transformations are performed in the optimization part.
  17. 17. Optimized Data Containers Few classes provided are not optimized for usage on mobile devices. Implementation of generic HashMap is a bit memory intensive due to its requirement for a separate entry object in each mapping. This memory issue can be solved by using optimized data containers like SparseArray, SparseBooleanArray, and LongSparseArray. As SparseArray classes avoid the system requirement to autobox the key and sometimes the value thus, they are more efficient. When required switch to raw arrays for lean data structure.
  18. 18. Choose Data Types Wisely Using primitive types will reduce the memory usage. An integer objects consume 4 times more memory than a primitive int. Similarly, a boolean object will occupy more memory than a primitive boolean type. For Constants prefer static final variables as they consume half the memory in comparison to enums.

×