(a) Releasing the object's reference, mistakenly storing an object with a short life cycle to a relatively long life cycle object, also known as "Object Free". Concealed Inner Classes (Anonymous Inner Class):
Mhandler = New Handler () {
@Override
public void Handlemessage (Message msg) {
....
}
};
Equivalent:
Class MyHandler implements Handler {
....
}
Mhandler = new MyHandler ();
There are similar listener,observer,receiver and so on.
Since an inner class implicitly has a reference to an external class object, it is important to pay attention to the declaration period of the Inner class instance.
It is best not to leak the instance of an inner class unless the consumer uses weak reference to refer to the instance of the inner class. For example, it is important to use weak reference when some handler,listener are leaking out as a return value.
Note: Some system-provided classes sneak themselves out of the way, such as Fileobserver, will be burst into their own internal thread to open, but fortunately it is using weak reference. But too much is broadcastreceiver, once we use the context Registerreceiver, the system (activitymanagernative) will have a strong reference to it, Unless we explicitly call Unregisterreceiver.
Conclusion: Any class that provides a switch function must be guaranteed to explicitly invoke their switching function.
(b) When constructing adapter, no cached Convertview are used, resulting in the system creating a large number of view and not being recycled.
Public View GetView (int position, view Convertview, ViewGroup parent)
(c) When a resource object is not in use, it should call its close () function, shut it off, and then set to null. Resource objects, such as cursor and so on, tend to use some buffering, and when we are not in use, we should close them in time so that their buffers can be recovered in time. Their buffering exists not only in the Java Virtual machine, but also outside the Java Virtual machine. If we simply set its reference to null without shutting them down, it often causes a memory leak.
(iv) All threads are a curse.
Because the execution time of the thread is unknown, the external objects held by the thread are generally used weak reference unless you affirm their life cycle.
In addition, when you post runnable or send message to other threads, you also need to be aware of the variables used inside Runnalbe and the contents of the message.
(v) asynctask-hidden Stories
The asynctask uses a static threadpoolexecutor with three parameters that are fixed by the system:
Core_pool_size:5
maximum_pool_size:128
Queue_capacity:10
When a asynctask is execute:
1. If the thread pool has fewer threads than core_pool_size, it will be directly threaded;
2. If the queue is not satisfied, it is added to the queue;
3. If the queue is full, the direct thread is executed;
4. If the thread pool has more threads than maximum_pool_size, it will be exception.
The place to tricky is the cancel operation of Asnctask.
1. Not all can be successful, such as while (TRUE) {} cannot be cancel;
2. If the asynctask is still in the queue, then the cancel operation does not remove it from the queue, but only flag is set, and when it is executed by thread, the flag is judged, if false, The Dobackground code is not executed.
Tragic plot: You are in the queue, the execution of a few buddies are block, you have been staying.
(vi) The story bitmap-had to say
Bitmaps in Android is created innative memory, not on the VM heap, so the actual Bitmap object on the VM Heapis very smal L as it doesn ' t contain any actual bitmap data.
In general, we are on-demand sortu, do not need to get the original picture, so we can first query the original image size, in the scale:
1. Query the original image size, through the Bitmapfactory.options property injustdecodebounds to do, If set to True, the decoder will return null (NOBITMAP), but T He out ... fields would still be set, allowing the caller to querythe bitmap without have to llocate the memory for its PI Xels.
2. The value of a scale is then computed and scaled by the attribute insamplesize in Bitmapfactory.options.
Bitmap Bitmap;
float Imagew = 300; Target size
float Imageh = 300; Target size
Bitmapfactory.options bitmapfactoryoptions = new Bitmapfactory.options ();
Bitmapfactoryoptions.injustdecodebounds = true;
Bitmap = bitmapfactory.decodefile (imagefile, bitmapfactoryoptions);
int yratio = (int) math.ceil (Bitmapfactoryoptions.outheight/imageh);
int xratio = (int) math.ceil (BITMAPFACTORYOPTIONS.OUTWIDTH/IMAGEW);
if (yratio > 1 | | xratio > 1) {
if (Yratio > Xratio) {
bitmapfactoryoptions.insamplesize = yratio;
} else {
bitmapfactoryoptions.insamplesize = xratio;
}
} else{
insamplesize = 1
}
Bitmapfactoryoptions.injustdecodebounds = false;
Bitmap = bitmapfactory.decodefile (imagefile, bitmapfactoryoptions);
Myimageview.setimagebitmap (bitmap);
bitmap-recycle useful? The following is an official source code comment:
The native object associated with this bitmap, and clearthe reference to the pixel data.
This would not be free the pixel data synchronously;
It simply allows it to be garbagecollected if there is no other references. The bitmap is marked as "dead", meaning it willthrow a exception ifgetpixels () or SetPixels () is called, and would draw no Thing. This operation cannot is reversed, so it should only becalled if you are sure there is no further uses for the bitmap.
? ? Call,and normally need not being called, since the normal GC Processwill free up this memory when there a Re no more references to this bitmap.
(vii) SUBSTRING's nightmare
? ? public String substring (int beginindex, int endIndex) {
? ? return new String (offset + beginindex,
Endindex-beginindex,
Value);
? ?}
? ? String (int offset, int count, Char value[]) {
? ? ? this.value = value;
? ? ? this.offset = offset;
? ? ? This.count = count;
? ?}
? ? That is, even if you just want to use some of these bytes, Java automatically saves the entire contents of the original string for you.
? ? ArrayList<string> oomstringlist = new ArrayList<string> (1024);
? for (int i=0; I<32*1024; i++) {
? ? String srcstring = new String (new char[1024]);
? ? String dststring = srcstring.substring (0,1);
? ? oomstringlist. Add (dststring);
? ??}
refrence:http://blog.csdn.net/edisonlg/article/details/7082063
Reasons for Android memory leaks