0

我在 Android Studio 中创建了一个空白的 Android Wear 2.0 独立应用项目。我注意到,当我在手表上运行空白应用程序时,似乎存在内存泄漏(如下所示)。还有其他人遇到这个问题吗?

在此处输入图像描述

我正在测试的设备是 LG Watch Style,运行 Android Wear 2.0

这是一个空白项目,代码如下:

MainActivity.java

import android.os.Bundle;
import android.support.wearable.activity.WearableActivity;

public class MainActivity extends WearableActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.mc.memorytester">

    <uses-feature android:name="android.hardware.type.watch"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@android:style/Theme.DeviceDefault">
        <uses-library
            android:name="com.google.android.wearable"
            android:required="false"/>

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.DeviceDefault.Light">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

构建.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "26.0.0"
    defaultConfig {
        applicationId "com.mc.memorytester"
        minSdkVersion 25
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.android.support:wearable:2.0.3'
    compile 'com.google.android.gms:play-services-wearable:11.0.4'
    provided 'com.google.android.wearable:wearable:2.0.3'
}
4

1 回答 1

1

这看起来不像是内存泄漏。如果您实际上是在泄漏,则使用的内存总量会随着时间的推移而不断增加,并最终导致非常缓慢的体验和/或OutOfMemoryException. 您在这里看到的是创建对象的循环,然后是垃圾收集。

您仍然可以采取一些措施来改善这一点,因为频繁的垃圾收集可能会导致打嗝/滞后。

我的建议是查看经常执行的方法(onDraw()和类似的东西),并确保您没有在其中创建对象。如果这没有帮助,您可以使用 Android Profiler 深入挖掘并找出正在创建的内容(有关如何执行此操作的详细信息,请参见此处

于 2017-08-01T15:42:58.067 回答