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
- 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) 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