How to resolve the memory leak issue using an Android Studio profiler

Use the Android Studio Profiler tool to analyze and resolve the memory leak issue.

Narendra Harny
Make Android

--

Before resolving the memory leak issue let’s see the memory leak issue in a simple example.

Example Source code for Memory Leak!!

package com.example.memoryleak;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

private Handler mHandler;
private static Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.go_to);
// Create a handler to perform periodic tasks
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// Perform some tasks
// This could involve manipulating UI elements or other resources

// Schedule the next execution after a delay
sendEmptyMessageDelayed(0, 1000);
}
};

// Start the periodic task
mHandler.sendEmptyMessage(0);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SecondActivity.class));
}
});
}
}
package com.example.memoryleak;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class SecondActivity extends AppCompatActivity {

private static Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
button = findViewById(R.id.back);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
}
}

The above simple code has a memory leak issue which you can see in the profiler tool in Android Studio.

How to analyse memory leaks using Profiler!!

Using the Android Profiler in Android Studio can help you identify and resolve memory leaks in your Android application. Here are the steps to use the profiler for this purpose:

1. Open Android Studio

2. Connect a Device or Emulator (Ensure that your Android device or emulator is connected to Android Studio).

3. Open the Profiler

→ Click on the “View” menu at the bottom of Android Studio.

→ Select “Tool Windows” and then choose “Profiler.”

Use above steps incase the profiler option is not available at bottom bar

4. Choose a Device

→ In the Profiler window, select the connected device or emulator from the device dropdown list.

Select the Device and the Process which needs to debug for profiling

5. Select the Memory Profiler

→ In the Profiler window, choose the “Memory” tab. This will open the Memory Profiler tool.

6. Start Recording

→ Click on the “Start Memory Profiling” button (a green circle) to begin recording memory allocations and deallocations.

7. Use Your App

→ Interact with your app on the connected device or emulator. Perform actions that trigger the suspected memory leak.

8. Stop Recording

→ Click on the “Stop Memory Profiling” button (a square) to stop recording the memory profile.

9. Analyze the Memory, Profiler Data

→ Review the Memory Profiler data to identify any abnormal memory usage patterns, increasing allocations, or retained objects.

→ Look for spikes or plateaus in the memory graph, which might indicate memory leaks.

10. Inspect Heap Dump

→ If needed, capture a heap dump for further analysis:

→ Click on the “Dump Java Heap” button.

→ Analyze the heap dump using tools like MAT (Memory Analyzer Tool) or other profiling tools.

11. Identify Leaked Objects

→ Examine the Memory Profiler’s object allocations and deallocations to identify objects that are not being released when they should.

12. Review Call Stacks

→ Use the Call Stack view to understand where in your code the memory allocations and leaks are occurring.

→ Click on specific entries in the Call Stack to navigate to the corresponding code.

13. Resolve the Issues

→ Based on the information gathered, address the issues causing the memory leaks in your code.

→ Fix potential leaks, release references, unregister listeners, and ensure proper resource management.

14. Re-run and Verify

→ Make the necessary code changes to fix the memory leaks.

→ Re-run the app, repeat the profiling process, and ensure that the memory usage is more stable.

By following these steps, you can leverage the Android Profiler in Android Studio to identify, analyze, and resolve memory leaks in your Android application. Regular profiling during development can help you catch and address memory issues early in development.

Thanks for Reading!

Please comment with questions and suggestions!! If this blog is helpful for you then hit Please hit the clap! Follow on Medium, Connect on LinkedIn, Follow on Insta and send emails if any discussion is needed on the same topic on copy email.
Please Follow & subscribe to Make Android Publication by Narendra K H for prompt updates on Android platform-related blogs.

Thank you!

Photo by Jeremy Bishop on Unsplash

--

--

Narendra Harny
Make Android

Connect on https://medium.com/make-android | Write On, Android AOSP & Applications | Python | DevOps | Java | C++ | Kotlin | Shell | Linux | Android Auto | IVI