Book
  • Introduction
  • Welcome !!
  • Chapter 1: The mobile ecosystem
    • Fragmentation is the devil
    • There is more than one type of mobile app
    • ... more than one type of app
    • ... one type of app
    • Under pressure (ee da de da de) !!
    • Further reading!!
  • Chapter 2: Let's start with design thinking
    • A taste of design thinking
    • The five steps
    • Design for everybody
    • Accessibility in mobile apps
  • Chapter 3: Give me a context and I will give you an app
    • Users
    • Personas? Users ? What is the difference?
    • Please, help me to model the context
    • The context canvas
  • Chapter 4: Powerful models
    • Data architecture is the foundation of analytics
    • From data to information and knowledge
    • Information/Knowledge in our mobile ecosystem
    • Questions to ask yourselves when building and classifying questions
    • The visualization-data map
    • On the scene: describing how personas interact with your app
  • Chapter 5: A GUI is better than two thousand words
    • 'Good to Go:' Let's explore the Design Systems
    • Designing GUI Mocks
    • No prototype... no deal
  • Chapter 6: About mobile operating systems ... and other deamons
    • The Android OS ... son of LINUX
    • iOS son of Darwin? or is it iOS son of UNIX?
    • Kernels
  • Chapter 7: Yes, software architecture matters !!
    • Self-test time
    • About design and design constraints
    • Architects' mojo: styles and patterns
    • What you need is a tactic !!
    • Self-test time 2 (for real)
    • Further reading
  • Chapter 8: Finally... coding
    • MVC, MVVM, MV*, MV...What?
    • Programming models: the Android side
    • Hello Jetpack, my new friend... An Android Jetpack Introduction
    • Programming models: the iOS side
    • Controllers and more controllers
    • Flutter son of... simplicity
    • Programming models: Flutter?
    • Flutter: State matters... Let´s start simple
    • Flutter: State matters... Complex stuff ahead
    • Micro-optimizations
  • Chapter 9: Data pipeline
    • Generalities data pipelines
    • Data storage types
    • Types of data pipelines
  • Chapter 10: Error Retrieving Chapter 10
    • Eventual Connectivity on Mobile Apps
    • How to handle it on Android
  • Chapter 11: The jewel in the crown: Performance
    • As fast as a nail
    • Memory bloats
    • Energy leaks
    • Final thoughts
  • Chapter 12. Become a performance bugs exterminator
    • Weak or strong?
    • Micro-optimizations
    • The single thread game !!
    • Using multi-threading like a boss !!
    • Caching
    • Avoiding memory bloats
    • Further readings
Powered by GitBook
On this page
  1. Chapter 11: The jewel in the crown: Performance

Memory bloats

PreviousAs fast as a nailNextEnergy leaks

Last updated 1 year ago

(By Mario Linares-Vásquez)


Memory is perhaps one of the most valued resources in a mobile device (after energy) and one of the biggest headaches for mobile developers. The mobile operating systems have very specialized mechanisms for memory management aimed at assuring that there are no memory greedy apps (in the device) that can shutdown the OS. Therefore, it is good for you to know the best practices what will help your apps to not be tagged (by the users and the OS) as a memory greedy app.

The term Memory bloat refers to an excessive consumption of memory in a software system. Common causes of memory bloats are complicated/huge data structures, strong references, cyclic references, not using good practices for dealing with images and bitmaps, excessive/unnecessary declaration of objects (e.g., inside loops), among others. If you want to be a successful mobile development, then you should be a master/ninja/guru in memory management.

Memory bloats can lead to OutOfMemory errors (OOM) that force the app to crash without mercy. The amount of memory available for an app is dynamically allocated and it depends on (i) the memory capacity in the device, and (ii) the amount of apps running in the phone (in background or foreground). Therefore, an OOM error occurs when your app is allocating more memory than the one assigned to the app in the heap. Because this is a forbidden operation, then app crashes or the OS decides to kill your app.

Read about the basic memory management concepts in the Android OS .

Since API level 14, Android has the interface which allows developers to do finer memory management. It can be used with Activities, Services, ContentProviders, and the Application class. The interface has the onTrimMemory public method, which should be used by developers to release memory based on current memory constraints. This will help app responsiveness and could prevent the process to be killed by the OS. The callback method will be automatically invoked by the OS depending on the memory constraint levels. A similar mechanism in iOS is available as a callback method in controllers: . This callback is invoked by the OS when the amount of memory is low. You can override this method to dispose of not critical resources as cached images or data that can be recreated (user-entered data, network data), but never deallocate elements that are shown on the screen.

Take a look to the documentation.

In Android apps, the available memory can be also queried via the ActivityManager class. Adapting (programatically) your app behavior is a nice feature that users appreciate, therefore, querying available memory and reacting to it is a MUST in modern mobile apps. For example, checking the memory available to decide whether to load a high-resolution version of a photo or a low-resolution one, is an example of adaptation feature. To this you can use the class

Check the article to learn to check (programatically) how much memory is available in your Android app.

ComponentsCallback2
ActivityManager.MemoryInfo
Manage your app´s memory
HERE
ComponentsCallback2
didReceiveMemoryWarning