SoFunction
Updated on 2025-04-09

Android memory overflow and memory leak reasons are analyzed

Out Of Memory: Every application in the Android system can apply for a certain amount of memory from the system. When the required memory is not enough, a memory overflow occurs.

Memory leak: When an object is no longer used, that is, no variables refer to it, the memory occupied by the object will be recycled by the system. When an object is no longer used, but there are variables in other objects refer to it, the memory occupied by the object cannot be recycled by the system, resulting in a memory leak.

When there is too much memory leak, the available memory space will be reduced, and the application applies for insufficient memory, which will cause memory overflow.

Reasons for memory overflow:

1. Too much memory leak.

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

3. There are references to objects in the collection class (used to store references to objects) and are not cleared after use.

4. The memory applied for is insufficient.

5. A dead loop or loop generates too many object instances, resulting in a large amount of memory being consumed.

。。。

Causes of memory leaks:

1. The resource object is not closed:

(1) After registering the broadcast receiver, the unregisterReceiver() method is not called to log out of the broadcast receiver.

(2) After opening the file stream, the close() method is not called to close the file stream.

(3) After using the database cursor, the close() method is not called to close the cursor.

(4) The recycle() method is not called after the image resource Bitmap is used.

。。。

2. Objects with long life cycles hold references to objects with short life cycles, resulting in the memory of objects with short life cycles being unable to be recycled:

(1) The life cycle of a singleton pattern or static member variable is equal to the life cycle of the application. When a Context is required, if the Activity Context is passed in, the Activity cannot be recycled when it needs to be destroyed. The solution is to pass in the Context of Application, because the Context lifecycle of Application is equal to the lifecycle of the application.

(2) Non-static inner classes (anonymous inner classes, Handlers, etc.) hold references to external classes by default. If the life cycle of the object instance of the non-static inner class is longer than the life cycle of the external class (for example, the non-static inner class defines a static object instance), the external class cannot be recycled by the system when it is logged out, resulting in memory leaks. The solution is to use static inner class + weak reference.

Summary: There are two main reasons for memory leaks: one is that the resource object does not close after use. Another type is that objects with long life cycles refer to objects with short life cycles, resulting in objects with short life cycles that cannot be recycled by the system even if they are no longer used. The fundamental reason is that the memory resources that need to be recycled are not recycled by the system.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.