Android Best performance Practice (ii)--analyze memory usage
Source: Internet
Author: User
<span id="Label3"></p><p><p>Reprint Please specify source: http://blog.csdn.net/guolin_blog/article/details/42238633</p></p><p><p></p></p><p><p>Although the current cell phone memory is very large, but we all know that the system is not possible to allocate all the memory to our Application. yes, Each program will have a memory limit that can be used, which is called the heap Size. Different mobile phones, heap size is not the same, with the current hardware equipment is increasing, the heap size is already from Nexus One 32MB, the Nexus 5 o'clock 192MB. If you want to know what the heap size of your phone is, you can call the following code:</p></p><strong><strong>[java]</strong></strong>View Plain<span class="tracking-ad" data-mod="popu_168"><span class="tracking-ad" data-mod="popu_168"><span class="tracking-ad" data-mod="popu_168">Copy</span></span></span> <ol class="dp-j" start="1"> <ol class="dp-j" start="1"> <li class="alt">Activitymanager manager = (activitymanager) Getsystemservice (context.activity_service);</li> <li><span class="keyword">int heapsize = Manager.getmemoryclass ();</span></li> </ol> </ol><p><p>The result is returned in megabytes, where the memory used to develop the application does not exceed this limit, or OutOfMemoryError will Occur. therefore, for example, in our program we need to cache some data, we can determine the capacity of the cache data according to the heap Size.</p></p><p><p>Let's talk about the GC operation of android, the GC full name is garbage Collection, which is called garbage Collection. The Android system triggers GC operations at the right time, and once the GC is in place, some objects that are no longer used are recycled. So what objects are considered to be unused and can be recycled? Let's take a look at the following picture:</p></p><p><p></p></p><p><p>Each blue circle represents an object in memory, and the arrows between the circles are their referential relationships. Some of these objects are active, and some are no longer being used. Then the GC operation starts with an object called roots, and all the objects it can access indicate that it is still in use and should be preserved, while the other objects indicate that they are no longer being used, as shown in:</p></p><p><p></p></p><p><p>As you can see, all the yellow objects are still retained by the system, and the blue objects are collected by the system in the GC operation, which is probably the simple GC process of the Android System.</p></p><p><p>So when does it trigger a GC operation? This is usually determined by the system, we generally do not need to proactively notify the system should go to the GC (although we can do this, as described below), but we can still listen to the System's GC process, in order to analyze our Application's current memory State. So how do you listen to the System's GC process? In fact, it is very simple, every time a GC operation, the system will print a log in logcat, we just have to analyze this log, the basic format of the log is as Follows:</p></p><strong><strong>[plain]</strong></strong>View Plain<span class="tracking-ad" data-mod="popu_168"><span class="tracking-ad" data-mod="popu_168"><span class="tracking-ad" data-mod="popu_168">Copy</span></span></span> <span class="tracking-ad" data-mod="popu_168"><span class="tracking-ad" data-mod="popu_168"><span class="tracking-ad" data-mod="popu_169"> <span class="tracking-ad" data-mod="popu_167"><span class="tracking-ad" data-mod="popu_170"></span></span></span></span></span> <ol start="1"> <ol start="1"> <li class="alt">D/dalvikvm: <GC_Reason> <amount_freed>,
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