DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Build a React Native Chat App for Android
  • The 12 Biggest Android App Development Trends in 2023
  • Android Cloud Apps with Azure
  • How Java Apps Litter Beyond the Heap

Trending

  • Is Agile Right for Every Project? When To Use It and When To Avoid It
  • Integration Isn’t a Task — It’s an Architectural Discipline
  • Automating Data Pipelines: Generating PySpark and SQL Jobs With LLMs in Cloudera
  • 5 Subtle Indicators Your Development Environment Is Under Siege
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. How to Capture Heap Dump From an Android App

How to Capture Heap Dump From an Android App

Capture heap dumps from your Android app to diagnose memory leaks and other problems in order to optimize memory usage.

By 
Ram Lakshmanan user avatar
Ram Lakshmanan
DZone Core CORE ·
Jun. 15, 18 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
36.5K Views

Join the DZone community and get the full member experience.

Join For Free

Heap dumps are vital artifacts to diagnose memory-related problems such as memory leaks, garbage collection problems, and java.lang.OutOfMemoryError. They are also vital artifacts to optimize memory usage.

In this article, we have given a few different options to capture heap dumps from Android apps. Once you have captured heap dumps, you can use great tools like HeapHero and Android Studio’s heap analyzer to analyze heap dumps.

1. Memory Profiler

Below are the steps to capture heap dumps from Memory Profiler in Android studio:

a. Run the app and select the device you want to profile from Android Studio.

b. In your Android studio, click on View >> Tool Windows >> Android Profiler

Android HD1

c. There will be a Memory timeline, which would be below the CPU timeline, but above the Network timeline. In this memory timeline, click on the download button (highlighted in the below image) to generate heap dump from the Android app.

Andr HD2

d. To store heap dump in your system, click on the highlighted icon in the below image.

Screenshot (217)

e. Choose a location to save the generated heap dump file.

Screenshot (218)

2. Android Debug Bridge (ADB)

Android Debug Bridge is a command line tool which allows you to interact with a device. ADB provides a variety of device actions, such as installing and debugging apps. It also gives access to the Unix shell to run a variety of commands on the device. You can use this tool to generate android heap dumps. Launch ADB shell and follow the below steps:

a. Identify your Android App’s Process Id

First step is to identify your Android App’s process Id. You can do that by issuing below command:

adb shell ps | grep <APP-NAME> 

The above command will return details about the process. The second number will be the PID of your app. Please check the below screenshot.Android HD4

b. Create a Heap Dump:

adb shell am dumpheap <PID> <HEAP-DUMP-FILE-PATH>  

PID: Your Android App Process Id
HEAP-DUMP-FILE-PATH: Location where heap dump file should be generated

Example:

adb shell am dumpheap 1769 /data/local/tmp/android.hprof

c. Pull the file to your machine

The above step will generate the heap dump file in the device. For analysis, you need to pull the generated file to your machine. You do that by issuing below command:

adb pull <HEAP-DUMP-FILE-PATH>

HEAP-DUMP-FILE-PATH: Location where heap dump file

Example:

adb pull /data/local/tmp/android.hprof

3. Capture Heap Dumps on OutOfMemoryError

If you place the below code in your application, it will capture heap dumps whenever your application receives an OutOfMemoryError.

public class CaptureHeapDumps extends Application {
       private static final String FILE_NAME = "heap-dump.hprof";
       @Override
       public void onCreate() {
          super.onCreate();

Thread.currentThread().setUncaughtExceptionHandler(OutOfMemoryException());

       }
       @NonNull
       private Thread.UncaughtExceptionHandler OutOfMemoryException() {
               return new Thread.UncaughtExceptionHandler() {
               @Override
               public void uncaughtException(Thread t, Throwable e) {
                  String directory = getApplicationInfo().dataDir;
                  String absolutePath = new File(directory, FILE_NAME)
                         .getAbsolutePath();
                  try {
                      Debug.dumpHprofData(absolutePath);
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
               }
        };
    }
}

This would generate the heap dump file in this location: /data/user/0/appname/heap-dump.hprof

garbage collection Dump (program) Android (robot) app

Opinions expressed by DZone contributors are their own.

Related

  • How to Build a React Native Chat App for Android
  • The 12 Biggest Android App Development Trends in 2023
  • Android Cloud Apps with Azure
  • How Java Apps Litter Beyond the Heap

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!