先日 メモリリークのついての質問 をさせていただき、その時点での問題は解決できたのですが、その後 Activity または Fragment のレイアウトXMLにて ScrollView または RecyclerView を配置するとメモリリークが起きてしまうことが判明しました。
これについても、手元にある端末のうち AQUOS sense6s(SH-RM19s) でのみ発生するようです。
原因を探るため、試しに必要最低限の内容のみの Activity 2つで構成したアプリを作成しました。
2つ目の Activity のレイアウトXMLに ScrollView を設置しています。
これを起動し、1つ目の Activity から2つ目の Activity に遷移して戻るボタンで1つ目の Activity に戻ると、LeakCanary でメモリリークが検出されます。
Android13 に起因するものかもしれないと考え Android13 のエミュレータでも実行してみましたが、メモリリークは起こりませんでした。
自分ではどこに原因があるのか全く見当がつかない状態ですので、何か少しでもわかる方がいらっしゃいましたらぜひよろしくお願いいたします。
~追記~
ちなみに gradle:app の dependencies から LeakCanary を削除して実行しても下画面のように結果は同じですので、LeakCanary の問題ではないようです。
build.gradle(:app)
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
namespace 'com.example.recyclerviewtestapplication'
compileSdk 33
defaultConfig {
applicationId "com.example.recyclerviewtestapplication"
minSdk 24
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary true
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.3.2'
}
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.1'
implementation 'androidx.activity:activity-compose:1.6.1'
implementation platform('androidx.compose:compose-bom:2022.10.00')
implementation 'androidx.compose.ui:ui'
implementation 'androidx.compose.ui:ui-graphics'
implementation 'androidx.compose.ui:ui-tooling-preview'
implementation 'androidx.compose.material3:material3'
implementation 'androidx.recyclerview:recyclerview:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.fragment:fragment:1.4.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation platform('androidx.compose:compose-bom:2022.10.00')
androidTestImplementation 'androidx.compose.ui:ui-test-junit4'
debugImplementation 'androidx.compose.ui:ui-tooling'
debugImplementation 'androidx.compose.ui:ui-test-manifest'
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.9.1'
}
マニフェスト
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.RecyclerViewTestApplication"
tools:targetApi="31">
<activity android:name=".ui.theme.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ui.theme.TestActivity"
android:exported="true">
</activity>
</application>
</manifest>
遷移元のアクティビティ
package com.example.recyclerviewtestapplication.ui.theme;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import androidx.annotation.Nullable;
import com.example.recyclerviewtestapplication.R;
public class MainActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setOnClickListener(v -> {
// TestActivity に遷移
Intent intent = new Intent(this, TestActivity.class);
startActivity(intent);
});
}
}
遷移元のレイアウト
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="Move"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
遷移先のアクティビティ
package com.example.recyclerviewtestapplication.ui.theme;
import android.app.Activity;
import android.os.Bundle;
import androidx.annotation.Nullable;
import com.example.recyclerviewtestapplication.R;
public class TestActivity extends Activity {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
}
}
遷移先のレイアウト
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>-->
<!-- メモリリーク発生 -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</ScrollView>
</LinearLayout>
メモリリーク発生までのログ
2023-07-11 04:23:44.852 9322-9361 LeakCanary com...e.recyclerviewtestapplication D LeakCanary is running and ready to detect memory leaks.
2023-07-11 04:23:51.804 9322-9344 System com...e.recyclerviewtestapplication W A resource failed to call close.
2023-07-11 04:23:51.968 9322-9425 ProfileInstaller com...e.recyclerviewtestapplication D Installing profile for com.example.recyclerviewtestapplication
2023-07-11 04:24:06.428 9322-9362 AdrenoGLES-0 com...e.recyclerviewtestapplication I QUALCOMM build : cac6e6f805, I5187d04b75
Build Date : 11/22/21
OpenGL ES Shader Compiler Version: EV031.35.01.10
Local Branch :
Remote Branch :
Remote Branch :
Reconstruct Branch :
2023-07-11 04:24:06.428 9322-9362 AdrenoGLES-0 com...e.recyclerviewtestapplication I Build Config : S P 10.0.7 AArch64
2023-07-11 04:24:06.428 9322-9362 AdrenoGLES-0 com...e.recyclerviewtestapplication I Driver Path : /vendor/lib64/egl/libGLESv2_adreno.so
2023-07-11 04:24:06.516 9322-9362 AdrenoGLES-0 com...e.recyclerviewtestapplication I PFP: 0x016ee201, ME: 0x00000000
2023-07-11 04:24:07.034 9322-9362 Parcel com...e.recyclerviewtestapplication W Expecting binder but got null!
2023-07-11 04:24:07.037 9322-9322 Choreographer com...e.recyclerviewtestapplication I Skipped 38 frames! The application may be doing too much work on its main thread.
2023-07-11 04:24:07.053 9322-9347 OpenGLRenderer com...e.recyclerviewtestapplication I Davey! duration=727ms; Flags=1, FrameTimelineVsyncId=131891258, IntendedVsync=1284455148176085, Vsync=1284455148176085, InputEventId=0, HandleInputStart=1284455160179027, AnimationStart=1284455160181631, PerformTraversalsStart=1284455160182412, DrawStart=1284455745678766, FrameDeadline=1284455164842751, FrameInterval=1284455160165121, FrameStartTime=16649441, SyncQueued=1284455761423089, SyncStart=1284455761603558, IssueDrawCommandsStart=1284455762601579, SwapBuffers=1284455873589339, FrameCompleted=1284455875854756, DequeueBufferDuration=24106562, QueueBufferDuration=359948, GpuCompleted=1284455875854756, SwapBuffersCompleted=1284455874544704, DisplayPresentTime=51011326586448, CommandSubmissionCompleted=1284455873589339,
2023-07-11 04:24:15.996 9322-9322 Compatibil...geReporter com...e.recyclerviewtestapplication D Compat change id reported: 171228096; UID 10437; state: ENABLED
2023-07-11 04:24:16.106 9322-9362 Parcel com...e.recyclerviewtestapplication W Expecting binder but got null!
2023-07-11 04:24:19.934 9322-9322 LeakCanary com...e.recyclerviewtestapplication D Watching instance of com.example.recyclerviewtestapplication.ui.theme.TestActivity (com.example.recyclerviewtestapplication.ui.theme.TestActivity received Activity#onDestroy() callback) with key 136f7d8f-32a0-427d-8ead-9b5617359d00
2023-07-11 04:24:27.341 9322-9361 testapplication com...e.recyclerviewtestapplication I Explicit concurrent copying GC freed 7672(394KB) AllocSpace objects, 0(0B) LOS objects, 24% free, 2470KB/3293KB, paused 198us,40us total 40.057ms
2023-07-11 04:24:27.451 9322-9361 LeakCanary com...e.recyclerviewtestapplication D Found 1 object retained, dumping heap now (app is invisible)
2023-07-11 04:24:27.556 9322-9361 LeakCanary com...e.recyclerviewtestapplication D Removing 1 heap dumps
2023-07-11 04:24:27.752 9322-9361 testapplication com...e.recyclerviewtestapplication I hprof: heap dump "/storage/emulated/0/Download/leakcanary-com.example.recyclerviewtestapplication/2023-07-11_04-24-27_685.hprof" starting...
2023-07-11 04:24:33.162 9322-9361 testapplication com...e.recyclerviewtestapplication I hprof: heap dump completed (33MB) in 5.409s objects 478748 objects with stack traces 0
2023-07-11 04:24:33.408 9322-9342 testapplication com...e.recyclerviewtestapplication I WaitForGcToComplete blocked HeapTrim on Hprof for 1.066s
2023-07-11 04:24:33.436 9322-10743 LeakCanary com...e.recyclerviewtestapplication D Analysis in progress, 0% done, working on Parsing heap dump
2023-07-11 04:24:41.157 9322-10743 LeakCanary com...e.recyclerviewtestapplication D Analysis in progress, 10% done, working on Extracting metadata
2023-07-11 04:24:47.993 9322-10743 LeakCanary com...e.recyclerviewtestapplication D Analysis in progress, 20% done, working on Finding retained objects
2023-07-11 04:24:48.001 9322-10743 LeakCanary com...e.recyclerviewtestapplication D Analysis in progress, 30% done, working on Finding paths to retained objects
2023-07-11 04:24:56.382 9322-10743 LeakCanary com...e.recyclerviewtestapplication D Analysis in progress, 40% done, working on Finding dominators
2023-07-11 04:25:24.152 9322-10743 LeakCanary com...e.recyclerviewtestapplication D Found 1 paths to retained objects
2023-07-11 04:25:24.153 9322-10743 LeakCanary com...e.recyclerviewtestapplication D Analysis in progress, 50% done, working on Inspecting objects
2023-07-11 04:25:24.170 9322-10743 LeakCanary com...e.recyclerviewtestapplication D Analysis in progress, 60% done, working on Computing native retained size
2023-07-11 04:25:24.175 9322-10743 LeakCanary com...e.recyclerviewtestapplication D Analysis in progress, 70% done, working on Computing retained size
2023-07-11 04:25:24.907 9322-10743 LeakCanary com...e.recyclerviewtestapplication D Analysis in progress, 80% done, working on Building leak traces
2023-07-11 04:25:24.923 9322-10743 LeakCanary com...e.recyclerviewtestapplication D Analysis in progress, 90% done, working on Reporting heap analysis
LeakCanary のログ
====================================
HEAP ANALYSIS RESULT
====================================
1 APPLICATION LEAKS
References underlined with "~~~" are likely causes.
Learn more at https://squ.re/leaks.
64948 bytes retained by leaking objects
Signature: 1f48c2e3c9d42514de20b1df25c05a32d67be916
┬───
│ GC Root: System class
│
├─ com.qualcomm.qti.Performance class
│ Leaking: NO (a class is never leaking)
│ ↓ static Performance.mContext
│ ~~~~~~~~
╰→ com.example.recyclerviewtestapplication.ui.theme.TestActivity instance
Leaking: YES (ObjectWatcher was watching this because com.example.recyclerviewtestapplication.ui.t
TestActivity received Activity#onDestroy() callback and Activity#mDestroyed is true)
Retaining 64.9 kB in 854 objects
key = 136f7d8f-32a0-427d-8ead-9b5617359d00
watchDurationMillis = 7818
retainedDurationMillis = 2817
mApplication instance of android.app.Application
mBase instance of android.app.ContextImpl
====================================
0 LIBRARY LEAKS
A Library Leak is a leak caused by a known bug in 3rd party code that you do not have control over.
See https://square.github.io/leakcanary/fundamentals-how-leakcanary-works/#4-categorizing-leaks
====================================
0 UNREACHABLE OBJECTS
An unreachable object is still in memory but LeakCanary could not find a strong reference path
from GC roots.
====================================
METADATA
Please include this in bug reports and Stack Overflow questions.
Build.VERSION.SDK_INT: 33
Build.MANUFACTURER: SHARP
LeakCanary version: 2.9.1
App process name: com.example.recyclerviewtestapplication
Class count: 22044
Instance count: 167931
Primitive array count: 120350
Object array count: 21450
Thread count: 19
Heap total bytes: 22986679
Bitmap count: 1
Bitmap total bytes: 31945
Large bitmap count: 0
Large bitmap total bytes: 0
Stats: LruCache[maxSize=3000,hits=106703,misses=166176,hitRate=39%]
RandomAccess[bytes=8508532,reads=166176,travel=58166189200,range=27722169,size=34672205]
Heap dump reason: 1 retained objects, app is not visible
Analysis duration: 45342 ms
Heap dump file path: /storage/emulated/0/Download/leakcanary-com.example.
recyclerviewtestapplication/2023-07-11_04-24-27_685.hprof
Heap dump timestamp: 1689017124920
Heap dump duration: 5656 ms
====================================