What are the tools you can use in Android App development to help you find and eliminate memory leaks

Android Studio Profiler tool

Android Studio as the official Android App development IDE, more and more powerful function, from Android Studio 3.0 to provide Profiler to App CPU, memory, network, power detection. Help developers find potential problems with App runtime

How to use

First, simulate a memory leak scenario

MainActivity

public class MainActivity extends AppCompatActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		findViewById(R.id.btn_jump).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
			    // Jump from MainActivity to SecondActivty
				startActivity(new Intent(MainActivity.this, SecondActivity.class)); }}); }}Copy the code

SecondActivity

public class SecondActivity extends AppCompatActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_second);

		// The non-static anonymous inner class sends a delayed message and then immediately hits the back button
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run(a) { System.out.println(); }},10 _000); }}Copy the code

You can see that two Leaks have been detected in the upper left corner. Click on the image below one by one to clearly print the reference chain of memory Leaks:

Eclipse Memory Analyzer (www.eclipse.org/mat/)

This is a memory analysis tool provided by Eclipse. It has more features than Profiler, but it is also more difficult to use, but it can analyze memory snapshots more clearly

Heap dump = profiler-capture heap dump = heap dump = Capture heap dump = Capture heap dump = Capture heap dump = Capture heap dump = Capture heap dump You need to run the /platform-tools/hprof-conv command under the Android SDK, as follows

hprof-conv demo.hprof demo-conv.hprof
Copy the code

Open the demo-conv.hprof file via MAT and click overview-histogram:

As an example of the memory leak above, you can search “SecondActivity” with the re and then right-click the results, Merge Shortest Paths to GC Roots -> exclude all phantom/weak/soft etc. References

You can see that Message holds a reference to Handler, and Handler holds a reference to SecondActivity, causing a memory leak

tip

Mat also supports comparing two hprof files, so you can compare memory snapshots before and after a page is opened in dump to determine which objects are not freed

LeakCanary (square.github.io/leakcanary/)

LeakCanary was developed by the well known Square team, which also developed popular open source frameworks like Okhttp and Retrofit

The above is a simple memory leak scenario, but the actual development environment is much more complex and requires the developer to actively detect memory leaks, and LeakCanary detects the life cycle of components such as activities, fragments, ViewModels, and Services. LeakCanary can detect if the App has a memory leak in time, and as the functionality has been iterated, it has been able to print out the reference chain very clearly to help developers locate code more quickly.