In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail the example analysis of Android memory performance optimization memory, the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.
1) Memory, GC, and Performance
It is well known that Java has a mechanism for GC, which requires manual coding to request and free memory. There is a Generational Heap Memory model in the Android system, and the system will perform different GC operations according to different memory data types in memory. For example, recently allocated objects are placed in the Young Generation area, where objects are usually quickly created and quickly destroyed and recycled, and the GC operation in this area is faster than the GC operation in the Old Generation area.
Except for the speed difference, any operation on all threads needs to be paused when the GC operation is performed, waiting for the GC operation to complete before other operations can continue to run.
Generally speaking, a single GC does not take much time, but a large number of constant GC operations can significantly take up frame interval time (16ms). If you do too much GC during the frame interval, then naturally there will be less time available for other similar operations such as computing, rendering, and so on.
2) Memory Monitor Walkthrough
The Memory Monitor in Android Studio can help us to see the memory usage of the program.
3) Memory Leaks
A memory leak indicates that objects that are no longer used cannot be recycled because they are misreferenced.
Memory leaks cause the remaining available Heap Size in the Memory Generation to become smaller and smaller, which can lead to frequent GC triggers and further performance problems.
For example, a memory leak. The following init () method comes from a custom View:
Private void init () {ListenerCollector collector = new ListenerCollector (); collector.setListener (this, mListener);}
The above example is prone to memory leaks. If the activity is recreated because the device is flipped, the custom View will automatically re-bind the newly created mListener to the ListenerCollector, but when the activity is destroyed, the mListener cannot be recycled.
4) Heap Viewer Walkthrough
The following figure shows the function of Heap Viewer in Android Tools. We can see the situation of Heap Size in the current process, what types of data there are, and what the percentage is.
5) Understanding Memory Churn
Memory Churn memory jitter, memory jitter is because a large number of objects are created and released immediately in a short period of time. Instantly producing a large number of objects will seriously occupy the memory area of Young Generation. When the threshold is reached and there is not enough space left, it will trigger GC and cause the newly generated objects to be quickly recycled. Even if each allocated object takes up a small amount of memory, they add to the pressure on the Heap, triggering more other types of GC. This operation may affect the frame rate and make users aware of performance problems.
There is a simple and intuitive way to solve the above problem. If you see multiple memory ups and downs in a short period of time in Memory Monitor, it means that memory jitter is likely to occur.
At the same time, we can also use Allocation Tracker to view the same objects in and out of the same stack in a short period of time. This is one of the typical signals of memory jitter.
When you roughly locate the problem, it is relatively straightforward to fix the problem. For example, you need to avoid allocating objects in the for loop to take up memory, you need to try to move the creation of the object outside the loop body, and the onDraw method in the custom View also needs to be noticed. Every time the screen is drawn and the animation is executed, the onDraw method will be called to avoid performing complex operations in the onDraw method and avoid creating objects. For those situations where it is inevitable to create objects, we can consider the object pooling model to solve the problem of frequent creation and destruction through object pooling, but it is important to note that objects in the object pool need to be manually released after use.
6) Allocation Tracker
For more information about the use of the Allocation Tracker tool, refer to the following link:
Http://developer.android.com/tools/debugging/ddms.html#alloc
Http://android-developers.blogspot.com/2009/02/track-memory-allocations.html
7) Improve Your Code To Reduce Churn
Here is an example of how to avoid memory jitter by modifying the code. Memory detection diagram before optimization:
After locating the code, fixed the problem of String splicing:
Optimized memory monitoring map:
8) Recap
Three tools for measuring memory have been mentioned above, and here is a brief summary of their respective characteristics:
Memory Monitor: tracks the memory changes throughout the app.
Heap Viewer: view the current memory snapshot to make it easier to compare and analyze which objects may have been leaked.
Allocation Tracker: trace the source of memory objects.
Android memory performance optimization memory example analysis is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.