Since Android is an operating system developed for mobile devices, we should always take the memory issue into account when developing applications. Although the Android system has a garbage collection mechanism, this does not mean that we can completely ignore when to allocate or free memory. Even if we all follow the programming recommendations given in the previous article to write programs, there is a good chance of memory leaks or other types of memory problems. So the only way to solve the problem is to try to analyze the memory usage of the application, and this article will teach you how to analyze it. If you haven't read the previous article, it's recommended to read the best Android Performance Practice (a)--manage the memory properly .
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:
[Java] view plaincopy
- Activitymanager manager = (Activitymanager) getsystemservice (Context.activity_service);
- int heapsize = Manager.getlargememoryclass ();
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.
Android Best performance Practice (ii)--Analyze memory usage