三星Android 13(One UI 5)含WebView的Activity内存泄漏问题排查
三星Android 13(One UI 5)设备WebView内存泄漏问题
我们通过LeakCanary工具检测发现,搭载Android 13(One UI 5)的三星设备上,使用WebView的Activity存在内存泄漏问题:用户关闭Activity后,即便onDestroy()方法已执行,该Activity仍驻留在内存中,受影响设备的ANR(应用无响应)情况明显增加。该问题在运行Android 13的Pixel 4设备上无法复现。
只需加载包含WebView的布局并关闭Activity,即可触发该内存泄漏。以下是可复现问题的代码及LeakCanary检测报告:
AndroidManifest.xml
<activity android:name=".HomeActivity" android:exported="false" android:theme="@style/AppTheme.NoActionBar"> </activity>
activity_home.xml(包含WebView组件)
<?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="br.com.gabba.myapp.presentation.WebViewActivity" tools:viewBindingIgnore="true"> <com.google.android.material.appbar.AppBarLayout android:id="@+id/appBarLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay" android:background="@drawable/bg_header" android:gravity="bottom" app:elevation="0dp"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:paddingStart="8dp" app:contentInsetLeft="0dp" app:contentInsetStart="0dp" app:contentInsetStartWithNavigation="0dp" app:popupTheme="@style/AppTheme.PopupOverlay" app:title="@string/app_name" app:titleTextAppearance="@style/Toolbar.TitleTextExterno" /> </com.google.android.material.appbar.AppBarLayout> <FrameLayout android:id="@+id/frameLayout" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent" /> </FrameLayout> </androidx.coordinatorlayout.widget.CoordinatorLayout>
HomeActivity.kt(加载含WebView的布局即可触发问题)
package br.com.gabba.myapp.presentation import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import br.com.gabba.myapp.R class HomeActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_home) } }
app/build.gradle
(...) implementation 'androidx.activity:activity-ktx:1.6.1' (...) debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.10'
LeakCanary检测报告
│ GC Root: Global variable in native code │ ├─ J1 instance │ Leaking: UNKNOWN │ Retaining 137,3 kB in 1971 objects │ ↓ WindowAndroid.p │ ~ ├─ b30 instance │ Leaking: UNKNOWN │ Retaining 136,2 kB in 1930 objects │ ↓ b30.b │ ~ ├─ com.android.internal.policy.PhoneWindow instance │ Leaking: YES (Window#mDestroyed is true) │ Retaining 136,2 kB in 1929 objects │ mContext instance of br.com.gabba.myapp.presentation.HomeActivity with mDestroyed = true │ mOnWindowDismissedCallback instance of br.com.gabba.myapp.presentation.HomeActivity with mDestroyed = true │ ↓ Window.mContext ╰→ br.com.gabba.myapp.presentation.HomeActivity instance Leaking: YES (ObjectWatcher was watching this because br.com.gabba.myapp.presentation.HomeActivity received Activity#onDestroy() callback and Activity#mDestroyed is true) Retaining 6,8 kB in 245 objects key = afa4c248-0006-4b43-9660-d2131e74893a watchDurationMillis = 8817 retainedDurationMillis = 3814 mApplication instance of br.com.gabba.myapp.myappApplication mBase instance of androidx.appcompat.view.ContextThemeWrapper METADATA Build.VERSION.SDK_INT: 33 Build.MANUFACTURER: samsung LeakCanary version: 2.10 App process name: br.com.gabba.myapp Class count: 29960 Instance count: 230083 Primitive array count: 152647 Object array count: 33977 Thread count: 72 Heap total bytes: 32257183 Bitmap count: 7 Bitmap total bytes: 5674439 Large bitmap count: 0 Large bitmap total bytes: 0 Db 1: closed /data/user/0/br.com.gabba.myapp/databases/google_app_measurement_local.db Db 2: open /data/user/0/br.com.gabba.myapp/databases/leaks.db Db 3: open /data/user/0/br.com.gabba.myapp/databases/com.microsoft.appcenter.persistence Db 4: open /data/user/0/br.com.gabba.myapp/no_backup/androidx.work.workdb Db 5: open /data/user/0/br.com.gabba.myapp/databases/com.google.android.datatransport.events Stats: LruCache[maxSize=3000,hits=119796,misses=220169,hitRate=35%] RandomAccess[bytes=11094589,reads=220169,travel=116557590219,range=37155332,size=47075038] Analysis duration: 10730 ms
内容的提问来源于stack exchange,提问作者DRBF
相关产品推荐
相关产品推荐

