because Android uses Java as a development language, and many people are using a problem that doesn't pay attention to memory.
so sometimes encountered when the program runs constantly consumes memory, resulting in outofmemery, the program exits unexpectedly, which is caused by memory leaks.
Let's summarize what might be causing a memory leak:
- Querying the database without closing the cursor
In Android, the cursor is a very common object, but in writing the code is often someone forgets to call close, or because the code logic problem condition causes close to not be called.
Typically, in activity, we can callStartmanagingcursoror direct useManagedqueryLet activity automatically manage cursor objects.
However, it is important to note that the cursor will no longer be available when the activity is introduced!
If the code and UI for manipulating the cursor are out of sync (such as a background thread), there is no need to first determine if the activity has ended, or wait for the background thread to end before calling OnDestroy.
In addition, the following are the more common cases where the cursor will not be closed:
- Try {
- Cursor C = querycursor ();
- int a = C.getint (1);
- ......
- C.close ();
- } catch (Exception e) {
- }
Although it appears that Cursor.close () has been called, if an exception occurs, close () is skipped, causing a memory leak.
So, our code should be written in the following way:
- Cursor C = querycursor ();
- Try {
- int a = C.getint (1);
- ......
- } catch (Exception e) {
- } finally {
- C.close (); //Call Close () in finally to make sure it is called
- }
- Unregisterreceiver () was not called after calling Registerreceiver.
After calling Registerreceiver, if Unregisterreceiver is not called, the amount of memory it occupies is quite large.
And we can often see code similar to the following:
- Registerreceiver (new broadcastreceiver () {
- ...
- }, filter); ...
This is a serious mistake because it causes the broadcastreceiver to not be unregister and causes a memory leak.
-
Inputstream/outputstream not closed
Use of Inputstream/outputstream can also lead to memory leaks when using files or accessing network resources
- Bitmap not called recycle () after use
Calling recycle is not required, as described in the SDK. But in actual use, bitmap occupies a large amount of memory, so when we no longer use, try to call recycle () to release resources.
- Context leaks
This is a very cryptic case of memory leaks.
First let's look at the following code:
- Private Static Drawable Sbackground;
- @Override
- protected void onCreate (Bundle state) {
- Super. OnCreate (state);
- TextView label = new TextView (this);
- Label.settext ("Leaksis bad");
- if (Sbackground = = null) {
- Sbackground = getdrawable (R.drawable.large_bitmap);
- }
- Label.setbackgrounddrawable (Sbackground);
- Setcontentview (label);
- }
In this piece of code, we used a static drawable object.
This usually happens when we need to call a drawable frequently, and its loading is time consuming and does not want to create this drawable every time the activity is loaded.
At this point, using static is undoubtedly the quickest way to write code, but it's also very bad.
When a drawable is attached to a view, the view is set to the drawable callback (implemented by calling Drawable.setcallback ().
This means that the drawable has a textview reference, and TextView has a reference to the activity.
This causes the memory to not be freed after the activity is destroyed.
Common memory leaks under Android Classic