Skip to main content

Detecting a memory consumption issue

In this tutorial, we’ll guide you through identifying high memory consumption in your app and improving object management using the Kotzilla Platform.

info

Issue context

High memory consumption occurs when components create too many objects or manage resources inefficiently, leading to performance issues and potential crashes, especially on low-memory devices.

Why it matters?

  • Excessive memory usage can slow down the app, cause it to crash, or impact the overall performance of the user’s device, leading to a poor experience.
  • High memory consumption can make your app sluggish, cause crashes, or even impact the performance of the user’s device.
  • Apps that manage memory inefficiently may suffer from poor performance, slow responsiveness, and higher resource usage, leading to negative user feedback and uninstalls.

Suggested user resolution actions

  • Detect high memory usage: Use the Kotzilla Platform to identify components with high object creation rates and suggest optimizations.
  • Optimize object lifecycle: Use single instead of factory in DI frameworks like Koin to stabilize frequently used instances and reduce memory pressure.
  • Implement resource management: Free up resources when they’re no longer needed and use caching or object pooling for frequently used objects.

Prerequisites

In this tutorial, we will be using the NowInAndroid app as an example. Before you begin, ensure you've completed the first two steps of the Get started with Kotzilla Platform

Step 1 - Simulate a slow dependency resolution

We will simulate a delay in the constructor of the OfflineFirstUserDataRepository, which is a key dependency within the app. This delay will help us observe the impact of slow resolutions on the app’s performance.

Here’s the code to introduce a 1-second delay in the OfflineFirstUserDataRepository:

class OfflineFirstUserDataRepository() {
init {
// Simulate a 1-second delay during initialization
Thread.sleep(1000)
}
}

Step 2 - Create user sessions to capture execution data

Once the delay is introduced in the component, create user sessions to capture data about how the slow initialization impacts the app. Repeat this process three times to gather consistent performance data.

Follow the navigation steps detailed in this guide to simulate a repeatable sequence of interactions. Repeat this three times to create consistent data on Kotzilla Platform.

Step 3- Analyze results in Kotzilla Platform

3.1 Observing impact on user sessions

Log into the Kotzilla Platform and go to the Dashboard View of your app. Click on the Sessions View to view the recorded sessions. In the Max Event Duration column, you should observe a 1-second delay due to the OfflineFirstUserDataRepository initialization.

We can also observe that the slow initialization results in 31 dependencies out of 33 being loaded simultaneously. This simultaneous loading can overwhelm the main thread, slowing down the entire app.

In the Timeline View, you can clearly see how the 1-second delay during the screen lifecycle transitions, especially between the started and resumed states, causes the main thread to be blocked. The delayed initialization of OfflineFirstUserDataRepository impacts app performance.

3.2 Investigating object creation and memory spikes

In the Threads View, observe how high memory consumption causes multiple dependencies load at once. This can lead to bottlenecks and reduced app performance, especially during startup.

Analyzing the Event Sequence further reveals how the main thread is affected by high memory usage, slowing down overall app responsiveness.

Key Takeaways

Simulating high memory usage demonstrated how excessive object creation affects app performance. The Kotzilla Platform helped isolate the issue, allowing you to improve memory management practices such as optimizing object lifecycles, minimizing memory load, and enhancing the app’s overall efficiency.