Wednesday, January 13, 2016

Dump the Android application heap if an out of memory crash occured

Dump the Android application heap if an out of memory crash occured - Hallo sahabat Google Android Developer Tutorial, Pada Artikel yang anda baca kali ini dengan judul Dump the Android application heap if an out of memory crash occured, kami telah mempersiapkan artikel ini dengan baik untuk anda baca dan ambil informasi didalamnya. mudah-mudahan isi postingan Artikel android, yang kami tulis ini dapat anda pahami. baiklah, selamat membaca.

Judul : Dump the Android application heap if an out of memory crash occured
link : Dump the Android application heap if an out of memory crash occured

Baca juga


Dump the Android application heap if an out of memory crash occured

Solution overview

The idea is to store the application heap if an out of memory error happens. The HPROF file can be then analyzed using Eclipse Memory Analyzer Tool that I will present later.

Step 1: Create a class that implements Thread.UncaughtExceptionHandler

The implemented method uncaughtException will check if the exception is out of memory and if it's the case, the HPROF file will be dumped in the device storage.
public static class ExtendedUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
Log.e("UncaughtException", "Uncaught exception occured!", ex);
if (ex.getClass().equals(OutOfMemoryError.class)) {
try {
android.os.Debug.dumpHprofData(Environment.getExternalStorageDirectory().getPath() + "/dump.hprof");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

Step 2: Set the uncaught exception handler the thread we want to create the heap dump

It's very sample, we can either set the uncaught exception handler to the main thread:
Thread.currentThread().setUncaughtExceptionHandler(new ExtendedUncaughtExceptionHandler());

Or, to any running thread:
// Invoke memory leak in a separate thread
Thread runningThread = new Thread(new Runnable() {
@Override
public void run() {
InvokeMemLeak();
}});
// Set uncaught exception to that thread too.
runningThread.setUncaughtExceptionHandler(new ExtendedUncaughtExceptionHandler());
runningThread.start();

Step 3: Create a memory leaked code to invoke an out of memory exception

For example, by doing:
private void InvokeMemLeak() {
List<String> list = new ArrayList<String>();
while (true){
list.add("OutOfMemory is coming. OutOfMemory is coming. OutOfMemory is coming. OutOfMemory is coming...");
}
}

Step 4: Analyze the HPROF file to fix the memory leak

Eclipse Memory Analyzer tool can be used to analyze the dumped heap, I will present the method in a separate article.

Source code

The Activity code is the following and the sample source code can be found on Github.
package anaware.memoryleaksample;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
public static class ExtendedUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
Log.e("UncaughtException", "Uncaught exception occured!", ex);
if (ex.getClass().equals(OutOfMemoryError.class)) {
try {
android.os.Debug.dumpHprofData(Environment.getExternalStorageDirectory().getPath() + "/dump.hprof");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set uncaught exception handler to the UI Thread
Thread.currentThread().setUncaughtExceptionHandler(new ExtendedUncaughtExceptionHandler());
// Invoke memory leak in a separate thread
Thread runningThread = new Thread(new Runnable() {
@Override
public void run() {
InvokeMemLeak();
}});
// Set uncaught exception to that thread too.
runningThread.setUncaughtExceptionHandler(new ExtendedUncaughtExceptionHandler());
runningThread.start();
}
private void InvokeMemLeak() {
List<String> list = new ArrayList<String>();
while (true){
list.add("OutOfMemory is coming. OutOfMemory is coming. OutOfMemory is coming. OutOfMemory is coming...");
}
}
}



Demikianlah Artikel Dump the Android application heap if an out of memory crash occured

Sekianlah artikel Dump the Android application heap if an out of memory crash occured kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.

Anda sekarang membaca artikel Dump the Android application heap if an out of memory crash occured dengan alamat link https://googleandroiddevelopertutorial.blogspot.com/2016/01/dump-android-application-heap-if-out-of.html

Artikel Terkait

Dump the Android application heap if an out of memory crash occured
4/ 5
Oleh

Berlangganan

Suka dengan artikel di atas? Silakan berlangganan gratis via email