【问题标题】:MemoryLeak appears when using InstantSearch-Android使用 InstantSearch-Android 时出现 MemoryLeak
【发布时间】:2017-11-13 21:33:36
【问题描述】:

当我使用 InstantSearch Android 时,出现内存泄漏。

查看下面的MainActivity 泄漏内存...hprof 表明这是com.algolia.instantsearch.ui.views.Hits 的问题。当Activity 被销毁时,有人知道如何销毁该视图吗?

public class MainActivity extends AppCompatActivity {
    private Searcher searcher;
    InstantSearch helper;

    Hits hits;

    LinearLayout activity_main;

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

        hits= (Hits) findViewById(R.id.hits);
        activity_main= (LinearLayout) findViewById(R.id.activity_main);

        searcher = Searcher.create("app_id","secret_key","Items");
        helper = new InstantSearch(this, searcher);
        helper.search();
    }

    @Override
    protected void onDestroy() {
        searcher=null;

        helper=null;
        hits.clear();

        hits.removeAllViewsInLayout();
        hits.removeAllViews();
        hits.destroyDrawingCache();
        hits.setBackground(null);
        hits.setBackgroundResource(0);
        hits=null;

        activity_main.removeAllViewsInLayout();
        activity_main.removeAllViews();
        activity_main.destroyDrawingCache();
        activity_main.setBackground(null);
        activity_main.setBackgroundResource(0);
        activity_main=null;
        Log.i("AppInfo","Destroy");
        super.onDestroy();
    }
}

这是我的activity_main.xml

<LinearLayout
android:id="@+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/white"
xmlns:algolia="http://schemas.android.com/apk/res-auto">


<com.algolia.instantsearch.ui.views.Hits
    android:id="@+id/hits"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    algolia:itemLayout="@layout/hits_item"
    algolia:autoHideKeyboard="true"
    algolia:hitsPerPage="20"/>

这是我的hits_item.xml

<layout xmlns:algolia="http://schemas.android.com/apk/res-auto">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/profImg"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:src="@drawable/placeholder_video"
            algolia:attribute='@{"profilePic"}'/>

    <TextView
        android:id="@+id/hit_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        algolia:attribute='@{"name"}'
        algolia:highlighted='@{true}'/>

 </RelativeLayout>

【问题讨论】:

  • 您能否添加更多信息,例如InstantSearchSearcher 是什么
  • InstantSearch 和 Searcher 是 InstantSearchAndroid 的类
  • 您对其中一个示例应用程序有同样的问题吗? github.com/algolia/instantsearch-android-examples
  • 是的,他们两个...我不知道这是否与数据绑定有关,以及 com.algolia.instantsearch.ui.views.Hits 中的这一行:algolia:itemLayout ="@layout/hits_item" ... itemLayout 属性引用将用于显示结果的每个项目的布局。此布局将包含我们要显示的数据的每个属性的视图...我不明白,如何销毁该布局?
  • 嗨@bojan,我们仍在调查此内存泄漏。尽管我们发现了第一个问题并正在发布修复程序,但我们很难以确定性的方式重现此错误。你能分享你触发内存泄漏的步骤吗?

标签: android algolia instantsearch instantsearch-android


【解决方案1】:

我们只是发布了一个修复程序。如果您更新到 1.1.4,您现在可以解决此问题。只需将您的活动的onDestroy 添加以下代码:

@Override
protected void onDestroy() {
    // ... your code ...
    searcher.destroy(); // This will clean and release different listeners 
    super.onDestroy();
}

你可以看到here

【讨论】:

  • 非常感谢我非常感谢!但是现在,当我在 1.1.4 上更新时,应用程序无法重建,并且我收到此消息:错误:任务“:app:compileDebugJavaWithJavac”的执行失败。 > java.lang.RuntimeException:发现数据绑定错误。 ****/ 数据绑定错误 ****msg:在 android.widget.TextView 上找不到参数类型为 java.lang.String 的属性“algolia:attribute”的设置器。文件:D:\MaterialDesign4-poslednje30\app\src\main\res\layout\hits_item.xml loc:91:33 - 91:39 ****\ 数据绑定错误 **** 请,你知道它是什么是,为什么现在会这样?
  • 您使用的是哪个版本的 Android Studio?
  • @bojan 我们之前在 Android Studio 3.0 上看到过这个问题,但现在应该在 1.1.4 中修复github.com/algolia/instantsearch-android/issues/47
  • 我使用 android studio 2.2.3... 与 Instantsearch 版本 1.0.2 运行良好,但是当我升级到 1.1.4 时,我收到此错误消息...
  • 能否将您的 Android Studio 更新到 3.0 或者有技术原因无法更新?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-11
  • 1970-01-01
  • 2016-01-26
  • 2016-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多