today proposed resignation, suddenly feel relaxed a lot, write down this article.
The entire system architecture of Android is divided into Application layer, application framework layer, class library and runtime and kernel layer, the application layer and application framework layer are developed in the Java language, while the library and runtime are developed using the C class language. The JVM has an automatic garbage collection mechanism, so many developers use the Java language without much attention to memory usage issues.
But Android developers have to take care of the problem. Android uses a sandbox mechanism to isolate each application, and each application runs in its own separate environment, and the birth and death of a single application does not affect the operation of other applications. Moreover, the Android system defaults to allocating 16M of memory for each app by default, and once this memory is used, and the memory space is not released, the Android app crashes with the infamous Oom exception, so talking about Android memory management has to start with Oom.
This article will explain from Oom, avoiding memory leaks, code optimization, and image memory management.
One,oom introduction:
while Android apps are running, the system limits the amount of memory that can be used when each app is running, but there's no concept of swap partitioning in Android, so the only place that frees up memory is to release the object's references. When a reference is not released in time, a memory shortage can cause an oom exception. So the oom exception can be summed up as the exception that is thrown by the virtual machine when the memory used in the heap (Android virtual machine above consumes) reaches the upper limit that can be used. If you want to increase the upper limit of memory used by each app, you can add an largeheap=true to the manifest file, but this approach also has a certain limit.
second, memory leaks:
memory leak means that the requested memory space is not used, but this memory resource is not released in a timely manner. The only thing we can do after a memory leak is to push the VM to reclaim memory, and we can't intervene. All we can do is to avoid memory leaks.
1. References and GC
This means that an object that occupies a large memory is not released correctly, so even if we do not end up in a method, we are releasing the object we did not use before.
2. Android App process Strategyin Android, the application process will not die with the disappearance of the four components, even if all the activity is destroyed, even if the process is an empty process, the process may exist. So this also leads to a problem, in code we often use static variables, static variables are class variables, are all classes shared variables, even if the application process is still there, this variable will always exist, if this class is an activity, when the activity is destroyed, Will cause a memory leak. So try not to set a class variable for a variable in an activity. Maybe a friend would say what if you want to use another activity a variable in activity B? The general practice is to set the variable in a to static and then access it directly in B, as described in the above method is not desirable. One way I'm here to introduce you is to use Application, which can be stored in a using Myapplication.setdata (List.clone ()). In B, you can Myapplication.getdata () to the variable so that the data can be transmitted using application to prevent losememory leaks. Some friends may ask why List.clone () is used here, because if you do not use cloning, it will cause B to hold a list in a reference, resulting in a can not be destroyed in a timely manner. 3. Activity and Context memory leaksThis is usually the case when we start another activity in one activity.
Intent a = new Intent (this,b.class); StartActivity (a);
So B will hold a reference to this context, which will also cause a to not die in time.another situation is that when the screen switch, Activity A will be rebuilt, can not release the original A, if you hold the object reference in class B at this time, will cause two copies of a in memoryso a lot of places can use the applicationcontext instead of the place when using the context. 4. Activity and handler memory leakswhen a sends a message using handler, the message is held by the queue of messages in the main thread, and if a exits at this point but the message is not processed, then a cannot be released in a timely manner, resulting in a memory leak. other to avoid memory leaks are such as timely dereference, correct use of cursor, timely release of registered broadcasts and so onThis article is temporarily written here, will continue!
Android Memory Management