Memory leakage detection and Android leakage during android Development

Source: Internet
Author: User

Memory leakage detection and Android leakage during android Development

I. Memory leakage

Memory leakage may reduce the number of available memory to reduce computer performance. In the end, in the worst case, too much available memory is allocated, causing all or some devices to stop working normally or applications to crash.

Memory leakage may not be serious and can even be detected by conventional means. In modern operating systems, an application is released upon termination of a regular internal program used by the application. This indicates that memory leakage in a transient running application will not cause serious consequences.

Memory leakage causes serious consequences in the following situations:

1) when the program runs, it ignores it and consumes more and more memory (such as background tasks on the server, especially background tasks in embedded systems) as the time is lost, these tasks may be ignored for many years after running );
2) New memory is frequently allocated, for example, when a computer game or animated video screen is displayed;
3) The program can request unreleased memory (such as shared memory), or even terminate the program;
4) leakage occurs inside the operating system;
5) leakage occurs in key drive of the system;
6) memory is very limited, for example, in an embedded system or a portable device;
7) when running on an operating system (such as AmigaOS) where the memory is not automatically released upon termination, and once lost, it can only be restored by restarting.

Ii. Memory leakage detection tools

When no other Java object references it, the Java Garbage Collector recycles it and releases the memory.

A Java Heap Dump file refers to the memory image export file of the Java object at a time point. It includes all objects, fields, original types, and object references. It indicates that a heap dump is automatically created when the JVM generates an OutOfMemory error.

Using Eclipse MAT (Eclipse Memory Analyser) helps to graphically reference Objects Based on Java heap dump and provides tools to detect potential Memory leaks. To enable the Java Virtual Machine (JVM) to generate a memory image file when an OutOfMemory error occurs, we can use the-XX: + HeapDumpOnOutOfMemoryError option, as shown in Figure 21.1.

To install the Eclipse MAT, follow these steps:

1) Start Eclipse and click Help-> Intall New Software ...;
2) Click the arrow on the right of the input column and select "Juno" (you need to find the option corresponding to your version name for different versions );
3) after the list is updated, find and expand General Purpose Tools ";
4) Select and download "Memory Analyser (Incubation)" and "Memory Analyser (Charts.
5) install the SDK as prompted.

Iii. MAT usage example

1) create a project

Create the Android project "com. devdiv. test. mat_test ". Create the following classes and run the project.

package com.devdiv.test.mat_test; import java.util.ArrayList;import java.util.List; import android.app.Activity;import android.os.Bundle; public class MainActivity extends Activity {     List<String> list = new ArrayList<String>();//    private PersonInfo person = new PersonInfo();         @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                 new Thread () {            @Override            public void run() {                   while (true){                        MainActivity.this.list.add("OutOfMemoryError soon");                }            }        }.start();    }   }

2) analysis memory

To obtain the. hprof memory image file, you can switch to the DDMS perspective page during the process running, select the process for which you want to view the memory image, and click "Dump HPROF file.



The generated hprof file is opened with MAT by default. Select "Leak Suspects Report" and click "Finish.

Figure 16-2 Use MAT to open a memory image file

After a period of initialization, You can intuitively see the memory leakage pie chart, as shown in:

Figure 16-3 memory leakage pie chart

Then you can view the related memory leakage, as shown in:


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.