【问题标题】:Android Wear 2.0 Memory LeakAndroid Wear 2.0 内存泄漏
【发布时间】:2017-07-31 22:19:54
【问题描述】:

我在 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>

build.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'
}

【问题讨论】:

  • 你能发布你的清单和活动代码吗?
  • 已发布!这是一个空白的 android wear 项目,这就是为什么我发现内存使用图令人困惑的原因。
  • 可以在项目中添加leak canary重新测试吗?

标签: android wear-os android-wear-2.0


【解决方案1】:

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

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

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

【讨论】:

  • 我已经发布了代码。你可能是对的,这不是内存泄漏,但我对一个什么都不做的空白项目的内存使用模式感到困惑。上面的截图来自 Android Profiler
  • 这确实是一个非常空白的项目。 :) 在其中一个周期内尝试在分析器中“记录内存分配”,看看您是否可以追踪正在创建的对象类型以及从何处创建。但是,如果它不在您的代码中,您可能无法做很多事情......
  • 似乎是 Android Wear 2.0 的问题。我在这里向他们报告了这个错误:issuetracker.google.com/issues/64296127
猜你喜欢
  • 1970-01-01
  • 2011-12-31
  • 1970-01-01
  • 1970-01-01
  • 2012-11-14
  • 2014-08-05
  • 2015-07-20
  • 2013-08-23
相关资源
最近更新 更多