Basic Android tutorial _ memory analysis tool memoryanalizer

Source: Internet
Author: User
Memoryanalizer is a memory Analysis Tool (memory analizer tool) released by eclipse. We can use it to find out memory leaks and reduce memory consumption. to use it first install the plug-in eclipse, steps are as follows: 1>, first download the plug-in, address: http://www.eclipse.org/mat/downloads.php
2> 2> after the download is complete, you can install it in eclipse
public class Main {/** * @param args */public static void main(String[] args) {List<String> list = new ArrayList<String>();while (1<2){list.add("OutOfMemoryError soon");}}}


Right-click --> RUN deployments. The following dialog box is displayed. Add the content marked in red.-XX: + heapdumponoutofmemoryerror.

Click Run. The following information is displayed on the console:

Refresh the project and find a file ending with hprof, as shown below:


Then open it through the plug-in, but our plug-in does not know why it cannot be opened, always reports an error, and finally find the answer on the Internet, it is recommended to use RCP (Rich Client platform, fat client platformYou can download the memoryanalyzer.exefile at http://www.eclipse.org/mat/downloads.php.

Then open the hprof file we generated through the tool for more details refer to: http://wiki.eclipse.org/index.php/MemoryAnalyzer#Getting_Startedhttp://www.blogjava.net/rosen/archive/2010/06/13/323522.html



Use the mat plug-in Android:The Dalvik virtual machine also has a garbage collection mechanism during operation, but this does not mean we can ignore memory management. We should be more concerned with the memory usage of mobile devices, because the phone memory is relatively tight. in this article, we will look at some memory profiling tools in the android SDK to help us adjust the memory usage of the application. some memory usage problems are obvious. For example, memory leakage occurs in your application every time you touch the screen. This may trigger an outofmemoryerror and cause the program to crash. other memory leaks are subtle, and may only cause applications (garbage collection times are more frequent and takes longer) and system execution efficiency to decline, the android SDK provides two main methods to describe the use of program memory: Allocation tracke (in ddms [Dalvik debug monitor service]) and heap dumps.

What type of allocation will happen in a given period of time, but it will not give us all the status information about the application heap. for more information about allocation traker, see http://developer.android.com/resources/articles/track-mem.html. we will put the focal point on heap dumps, which is a more powerful memory analysis tool. A heap dumps is a snapshot of an application heap. It is stored in a binary file named hporf. Dalvik uses this format in a similar way, but unlike hprof
Tool in Java. Here are some heap dumps that generate a running Android program. One is to use the dump hprof file button in ddms.


If we want to know more accurately when dump is created, we can use android. OS. debug. the dumphprofdata () method is programmed to create the DEAP dump. analyzes a heap dump and can use standard tools like jhat (http://docs.oracle.com/javase/6/docs/technotes/tools/share/jhat.html) or mat. then, the first step needs to be converted. the hprof file format is from Dalvik format to javase hprof format. you can use the hprof-Conv tool in the android SDK and enter the command in the CMD command line. The format is as follows:

hprof-conv in.hprof out.hprof
The following example shows how to create an activitypublic class mainactivity extends activity {

Static arraylist <pilot> List = new arraylist <pilot> (); // defines a set, which may be cached in the actual program. @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); For (INT I = 0; I <2000; I ++)
{// Add 2000 objects to it. List. Add (new pilot ("dsfdsgdgdgvfgdsfsd", I ));}}

} Then start the program. The process of the program can be seen in the device panel of ddms.


This section describes the functions of these buttons:

Click the process of our program and then click the update heap button. The content shown in may appear on the right panel. If no content is displayed, click cause GC.


Create a heap dump and a dump hprof file button in the devices toolbar, click this button to display the path for saving the hprof file (before that, make sure that we select the process of our program), and then select a path to save it. then, use the hprof-conver command to convert the format. finally, open the plug-in or RCP. I used RCP to open it here.:



You can also open histogram view, where:

Shows the result:

We have created 2000 Pilot objects in activty, which are displayed here. it also supports the number of objects, shallow heap (indicating the memory size occupied by all objects) and retained heap (to keep these objects alive, including the memory required for other objects to reference them .). debugging memory leaks. Sometimes it is useful to compare the heap state at a different time point. We need to create two hprof files, so how to compare two heap dumps in mat (this is a little complicated )? 1> open the first hprof file (Click file --> open heap dump) 2> open histogram view3>... original http://android-developers.blogspot.com/2011/03/memory-analysis-for-android.html:

  1. Open the first hprof file (usingFile> open heap dump).
  2. Open the histogram view.
  3. In the navigation history view (UseWindow> navigation historyIf it's not visible), right click onHistogramAnd selectAdd to compare basket.
  4. Open the second hprof file and repeat steps 2 and 3.
  5. Switch to the Compare basket view, and clickCompare the results(The Red "! "Icon in the top right corner of the view ).
Conclusion

In this article, I 've shown how the allocation tracker and heap dumps can give you get a better sense of your application's memory usage. I also showed how the eclipse memory analyzer (MAT) can help you track down memory leaks in
Your app. MAT is a powerful tool, and I 've only scratched the surface of what you can do with it. if you 'd like to learn more, I recommend reading some of these articles:

  • Memory analyzer news: the official blog of the eclipse mat Project
  • Markus Kohler's Java performance blog has extends helpful articles, including Analysing the memory usage of Android
    Applications with the eclipse memory analyzer and 10 useful tips for the eclipse memory analyzer.

Welcome reprinted: http://blog.csdn.net/johnny901114/article/details/7823219

Related Article

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.