The Android system runs on the Java virtual machine. As an embedded device, the memory is often very limited. Understanding Android's garbage collection mechanism can effectively prevent memory leaks or OOM problems. This article is an introductory article that will briefly discuss the principles of garbage collection and memory leaks, and will not discuss the underlying mechanism of the Dalvik virtual machine or native-level issues. 1. Basics Before analyzing garbage collection, we need to review the basics of Java and discrete mathematics.
Google I/O 2011: Memory management for Android Apps The orange Object in the figure above is the starting point of the directed graph. Its Shallow heap is 100, and its Retained heap is 100 + 300 = 400. 2. What is Garbage Collection Java GC (Garbage Collection) mechanism is one of the main differences between Java and C++/C. As a Java developer, you generally do not need to write special memory recovery and garbage cleaning code, and you do not need to be as nervous about memory leaks and overflows as C programmers. This is because there is an automatic memory management and garbage cleaning mechanism in the Java virtual machine. In general, this mechanism marks the memory in the virtual machine and determines which memory needs to be recycled. According to a certain recycling strategy, the memory is automatically recycled, and the memory space in the virtual machine is guaranteed to be never stopped (Nerver Stop) to prevent memory leaks and overflows. 3. When is garbage collection needed? For GC, when the programmer creates an object, GC starts to monitor the address, size and usage of the object. Usually GC uses a directed graph to record and manage all objects in the heap, and in this way determines which objects are "reachable" and which objects are "unreachable". When an object is unreachable, that is, when the object is no longer referenced, it will be garbage collected. There are many documents on the Internet that introduce the reachable relationship. As shown in the figure, in the sixth line, o2 changes its pointing direction, and Obj2 no longer references main, that is, they are unreachable, and Obj2 may be recycled in the next GC. developerWorks Java technology 4. What is a memory leak? When you no longer need an instance, but the object is still referenced, preventing it from being garbage collected (Prevent from being bargage collected). This situation is called a memory leak. Below, we will use the translation of How to Leak a Context: Handlers & Inner Classes as an example to introduce a memory leak. Look at the following code
When you finish typing this code, the IDE should remind you
How did it leak?
Reference relationship Now, let's try running the following code
This program is very simple. We can imagine that it should start and then shut down instantly, but is it really shut down? Anyone with a little common sense can see that it sends a Message that will run ten minutes later, which means that the Message will be kept referenced for 10 minutes, resulting in a memory leak of at least 10 minutes. The final correct code is as follows
in conclusion
Finally, let me add a few words about iOS. In the latest versions of OC and Swift, iOS has introduced a new memory management system ARC (auto reference counting). When compiling C code, the compiler automatically adds code to release memory in a timely manner. References http://www.jianshu.com/p/22e73e80e950 http://www.ibm.com/developerworks/cn/java/l-JavaMemoryLeak/ http://stackoverflow.com/a/70358/4016014 http://blog.csdn.net/luoshengyang/article/details/8852432 http://developer.android.com/training/articles/perf-tips.html https://techblog.badoo.com/blog/2014/08/28/android-handler-memory-leaks/ |
<<: 25 New Android Libraries You Definitely Want to Try in Early 2017
>>: Google AI: No one knows how it works
Stock Brother Training Camp: Only Know How to Tra...
How to do a good job of search optimization ( ASO...
1. Introduction After experiencing the wild growt...
Haima Automobile, which has attracted attention d...
Review expert: Zeng Shenming, professor of the De...
Recently, the topic #AI fraud is breaking out acr...
Through refined operations , tapping into the con...
Popular Science Times reporter Chen Jie Recently,...
Everyone knows that women are easy to change. The...
There are many people who make a fortune on the In...
In recent months, the trend of manipulating the r...
When you’re developing your first product, you ma...
[[148415]] Apple will hold a new product launch e...
Recently, LED curved TVs have created a new trend...
It is always more important to actually implement...