Created
April 12, 2013 09:22
-
-
Save moea/5370785 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.novoda.example.MemoryLeaker; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.os.Debug; | |
import android.view.View; | |
import android.widget.TextView; | |
import java.io.File; | |
import java.io.IOException; | |
public class LeakingActivity extends Activity | |
{ | |
private static final int BYTES_IN_A_MEGABYTE = 1024 * 1024; | |
private static final int BYTES_TO_ALLOCATE = BYTES_IN_A_MEGABYTE; | |
private static final String HPROF_DUMP_BASENAME = "LeakingActivity.hprof"; | |
private byte[] byteArray; | |
private TextView heapSizeTextView; | |
private void updateHeapSize() { | |
heapSizeTextView.setText(Runtime.getRuntime().totalMemory() / BYTES_IN_A_MEGABYTE + "mb"); | |
} | |
@Override | |
public void onCreate(Bundle savedInstanceState) | |
{ | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
heapSizeTextView = (TextView)findViewById(R.id.tv_heap_size); | |
findViewById(R.id.btn_gc).setOnClickListener( | |
new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
System.gc(); | |
} | |
} | |
); | |
findViewById(R.id.btn_heap).setOnClickListener( | |
new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
dumpHprofData(); | |
} | |
} | |
); | |
byteArray = new byte[BYTES_TO_ALLOCATE]; | |
} | |
private void dumpHprofData() { | |
String absPath = new File(getApplicationInfo().dataDir, HPROF_DUMP_BASENAME).getAbsolutePath(); | |
try { | |
// this'll cause a collection | |
Debug.dumpHprofData(absPath); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
@Override | |
public void onResume() { | |
super.onResume(); | |
updateHeapSize(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment