Memory leak detection during Android development

Source: Internet
Author: User

Transfer from http://blog.csdn.net/shimiso/article/details/44677041

One, memory leaks

Memory leaks can reduce the performance of your computer by reducing the amount of available memory. Eventually, in the worst case, too much free memory is allocated, causing all or part of the device to stop working properly, or the application crashes.

Memory leaks may not be serious and can even be detected by conventional means. In modern operating systems, an application that uses a routine within the existing program terminates when it is released. This means that a memory leak in a short-running application does not cause serious consequences.

A memory leak causes more serious consequences in the following cases:

1) Ignore the program after running, and over time to consume more and more memory (such as the background tasks on the server, especially the embedded system in the background tasks, these tasks may be run after many years are ignored);
2) New memory is allocated frequently, such as when displaying computer games or animated video images;
3) The program can request memory that has not been freed (such as shared memory), even when the program terminates;
4) leakage occurs within the operating system;
5) leakage occurs in the critical drive of the system;
6) memory is very limited, such as in embedded systems or portable devices;
7) on top of an operating system (such as AmigaOS) that is not automatically released when running at a termination, and once lost can only be restored by a reboot.

Second, Memory leak detection Tool

When a Java object already has no other object referencing it, the Java garbage collector reclaims it and frees up memory.

A Java heap dump file refers to the memory image export file of a Java object at a point in time. It includes all objects, fields, primitive types, and object references. It can indicate that a heap dump is created automatically when the JVM generates a outofmemory error.

Use Eclipse MAT (Eclipse Memory Analyser) to help graphically visualize object references based on Java heap dumps and provide tools to discover potential memory leaks. To enable the Java Virtual Machine (JVM) to generate a memory image file when a outofmemory error occurs, we can use the-XX:+HEAPDUMPONOUTOFMEMORYERROR option, shown in 21.1.

The steps to install Eclipse mat are as follows:

1) Start Eclipse, click Help, Intall New software ...;
2) Click on the small arrow to the right of the input field and select "Juno" (different versions to find the option corresponding to their version name);
3) Wait for the list to update, find and expand "General Purpose Tools";
4) Select and download the "Memory Analyser (incubation)" and "Memory Analyser (Charts)" in the two.
5) Follow the prompts to install.

Third, Mat use example

1) Create a project

New Android Project "Com.devdiv.test.mat_test". and create the following class, and then run the project.

[Java]View Plaincopy
  1. Package com.devdiv.test.mat_test;
  2. Import java.util.ArrayList;
  3. Import java.util.List;
  4. Import android.app.Activity;
  5. Import Android.os.Bundle;
  6. Public class Mainactivity extends Activity {
  7. list<string> list = new arraylist<string> ();
  8. Private PersonInfo person = new PersonInfo ();
  9. @Override
  10. public void OnCreate (Bundle savedinstancestate) {
  11. super.oncreate (savedinstancestate);
  12. Setcontentview (R.layout.activity_main);
  13. New Thread () {
  14. @Override
  15. public Void Run () {
  16. While (true) {
  17. Mainactivity.  This.list.add ("OutOfMemoryError soon");
  18. }
  19. }
  20. }.start ();
  21. }
  22. }

2) Analyzing memory

To get the. HPROF memory image file, you can switch to the DDMS perspective page while the process is running, select the process where you want to view the memory image, and click "Dump HPROF File".



The generated hprof file is opened by default using the mat, and when you select Leak Suspects report, click the Finish button.

Figure 16-2 Opening the memory image file with the mat

After a period of initialization, you can visually see a pie chart of memory leaks, as shown in:

Figure 16-3 Memory Leak pie chart

You can then view the associated memory leaks as shown in:

Memory leak detection during Android development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.