Android memory overflow and memory leaks

Memory overflow (Out Of Memory): Android system, each application can apply for certain memory to the system memory when the application is not enough, it creates a memory overflow.

Memory leak: When an object is no longer used, ie no more variable references it, the object will be occupied by the memory recovery system. When an object is no longer used, but there is still another object variable references it, the memory occupied by the object system can not be recycled, resulting in a memory leak.

When too much memory leak, will reduce the available memory space, memory, application application not enough, it will lead to memory overflow.

 

Memory Overflow reasons:

1. Memory leak too much.

2. The amount of data loaded in memory exceeds the available amount of memory.

3. collections (for storing the reference object) has a reference to the object, it is not emptied after use.

4. Application of insufficient memory.

The dead cycle or cycle generator object instances excessive, resulting in a large amount of memory is consumed.

。。。

Memory leak reasons:

1. resource object is not closed:

(1) does not call unregisterReceiver after registration broadcast receivers () method cancellation of broadcast receivers.

Do not call close () after (2) Open the file stream method to close the file stream.

(3) database using cursor cursor after no call to close () method to close the cursor.

(4) does not call Bitmap image resource recycle after use () method recovery.

   。。。

2. Referenced long life cycle of an object held by the short life cycle of an object, resulting in a short life cycle of an object memory can not be recovered:

(1) life cycle and life cycle of an application singleton or static member variables are equal, when the need to refer to Context, if the incoming is Activity of Context, it can not be recovered when the Activity needs to be destroyed. The solution is passed Application of Context, Context because the Application of the life cycle is equal to the life cycle of the application.

(2) non-static inner class (anonymous inner classes, Handler, etc.) The default hold a reference to the outer class, if the object is non-static inner class instance life cycle is longer than the outer class life cycle (such as non-static inner class defines a static object instance), the external class logging off the system can not be recovered, causing the memory leak. Solution is to use a static class + weak internal reference.

Summary: The reasons for memory leaks produced there are mainly two: one is the resource objects in close operation after no use. The other is a long life cycle object reference short life cycle of an object, resulting in a short life cycle of an object even if no longer in use but still can not be recovery system. Fundamental reason is that needs to be recovered memory resources are not recycling system.

Guess you like

Origin www.cnblogs.com/linwenbin/p/11995493.html