Android OOM and leak window

Source: Internet
Author: User

OOM is short for Android out of memory. When developing apps Based on Android, out of memory may often appear.
Exception.
In Android, a process can only use 16 MB of memory. If this limit is exceeded, this exception occurs. This requires us to always think about releasing resources. Java Collection is handed over to GC. There are many ways to enable GC to recycle unused objects in real time.

OOM is very simple because the total memory usage exceeds 16 Mb. When bitmap is not in use, it must be a recycle. Otherwise, oom is very easy to appear.


But there will be another situation: Obviously there is a lot of memory, but OOM occurs.

This type of situation often occurs when generating bitmap. If you are interested, you can try to generate a 13 m int array in a function.

After the function is completed, it is reasonable to say that the int array has been released or can be released. The space of 13 m should be empty,

At this time, if you continue to generate a m int array from a self-built millionaire, it will be no problem. Instead, generating a 4 m bitmap will jump out of oom. This is strange. Why is 10 m int enough space, but 4 m bitmap is not enough?

This problem has been plagued for a long time. On the internet, major forums outside China have searched for a long time. Generally, the explanation and solution for OOM are as follows: how to make the GC recycle code style as soon as possible, there is no root cause for the actual expenditure above.


I finally saw this explanation on a foreigner's blog yesterday. After my understanding, I summarized it as follows:

In Android:

1. the memory of a process can be composed of two departments: Java memory application and C memory application. The sum of the two memories must be less than 16 Mb. Otherwise, the OOM is familiar to everyone, this is the first oom.


2. the strange thing is: after the memory is allocated to Java, the memory can only be applied to Java even after it is released, this estimation is related to the reason why the Java Virtual Machine divides the memory into several blocks for caching. c won't want to use this block of memory anyway, so if Java suddenly occupies a large block of memory, even though it was soon explained:


The memory that C can use = 16 m-Java occupies the maximum memory used by college students to start their own business at a certain moment.

Bitmap is generated by malloc during the process of memory allocation, which occupies C memory. This explains the reason why the above mentioned 4mbitmap cannot be generated, because after 13 m is used by Java, only 3 m is available for C.


The leak window is literally known as a form leak, that is, the memory leak we often call. Why does the form leak?

02-29 10:07:51. 410: Error/windowmanager (4236): Activity CN. bookall. android. zbook. reader. docreader has leaked window android. widget. textview @ 485348b0 that was originally added here
02-29 10:07:51. 410: Error/windowmanager (4236): Android. view. windowleaked: Activity CN. bookall. android. zbook. reader. docreader has leaked window android. widget. textview @ 485348b0 that was originally added here
02-29 10:07:51. 410: Error/windowmanager (4236): at Android. View. viewroot. <init> (viewroot. Java: 247)
...

02-29 10:07:51. 410: Error/windowmanager (4236): At CN. bookall. Android. zbook. Reader. docview. showpopwindowcenter (docview. Java: 506)

...

02-29 10:07:51. 600: Error/androidruntime (4236): Java. Lang. illegalargumentexception: view not attached to Window Manager

Cause:
We know that every activity in Android has a windowmanager form manager. Similarly, the dialog box and popupwindow built on an activity also have the corresponding windowmanager form manager. Because the dialog box and popupdomainwn cannot be independent from the activity, when a dialog or popupwindow is being displayed, we finish () the activity that carries the dialog (or popupwindow, the window
Leaked is abnormal because no one in the windowmanager of this dialog (or popupwindow) can attach it, so its form manager has been leaked.
Solution:
Before closing an activity, make sure that the attached dialog or popupwindow has been closed (dismiss.

@ Override
Public void onpause (){
Super. onpause ();
If (PW! = NULL ){
PW. Dismiss ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.