2

When I'm using InstantSearch Android, a memory leak appears.

See the following MainActivity which leaks memory... hprof shows that it is a problem with com.algolia.instantsearch.ui.views.Hits. Does anybody know how to destroy that View when the Activity get destroyed?

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();
    }
}

here is my 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"/>

here is my 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>

PLNech
  • 3,087
  • 1
  • 23
  • 52
bojan
  • 1,269
  • 2
  • 9
  • 13
  • Can you please add some more info like what is `InstantSearch` and `Searcher` – Soham Oct 23 '17 at 13:36
  • 1
    InstantSearch and Searcher are classes from InstantSearchAndroid – bojan Oct 23 '17 at 13:43
  • Do you have the same issue with one of the example apps? https://github.com/algolia/instantsearch-android-examples – PLNech Oct 23 '17 at 14:15
  • yes,with both of them...I do not know if that has something to do with data binding,and this row in com.algolia.instantsearch.ui.views.Hits: algolia:itemLayout="@layout/hits_item" ...The itemLayout attribute references a layout that will be used to display each item of the results. This layout will contain a View for each attribute of our data that we want to display...I dont understand,how to destroy that layout? – bojan Oct 23 '17 at 14:39
  • Hi @bojan, we are still investigating this memory leak. Although we found a first problem and are issuing a fix, we have a hard time reproducing this bug in a deterministic way. Could you share your steps to trigger the memory leak? – PLNech Nov 06 '17 at 15:32
  • so,when i try destroy this common mainactivity,activity becomes leakedactivity...steps are:start this activity-finish()-leaked activitty! in hprof :mContext in com.algolia.instantsearch. ui.views.Hits... no special steps,simple,can not be destroyed mainactivity when contains com.algolia.instantsearch.ui.views.Hits view...so,is there a way to destroy Hits view ? – bojan Nov 06 '17 at 22:36
  • @bojan we identified the issue and currently working on a fix. I'll get back to you when the change is pushed – Robert D. Mogos Nov 09 '17 at 09:51

1 Answers1

2

We just release a fix. If you update to 1.1.4 you can now fix the issue. Just add your activity's onDestroy the following code:

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

You can see it here

Robert D. Mogos
  • 900
  • 7
  • 14
  • thank you very much I am very grateful! but now,when i update on 1.1.4,application cant rebuild,and i get this message: Error:Execution failed for task ':app:compileDebugJavaWithJavac'. > java.lang.RuntimeException: Found data binding errors. ****/ data binding error ****msg:Cannot find the setter for attribute 'algolia:attribute' with parameter type java.lang.String on android.widget.TextView. file:D:\MaterialDesign4-poslednje30\app\src\main\res\layout\hits_item.xml loc:91:33 - 91:39 ****\ data binding error **** please,do you know what it is,why is this happening now? – bojan Nov 10 '17 at 02:09
  • Which version of Android Studio are you using? – Robert D. Mogos Nov 10 '17 at 09:15
  • @bojan We saw this issue before on Android Studio 3.0 but should be fixed now in 1.1.4 https://github.com/algolia/instantsearch-android/issues/47 – Robert D. Mogos Nov 10 '17 at 09:23
  • i use android studio 2.2.3... with instantsearch version 1.0.2 works well,but when i upgrade on 1.1.4,i get this error message... – bojan Nov 10 '17 at 14:45
  • Can you update your Android Studio to 3.0 or there are technical reasons for not updating ? – Robert D. Mogos Nov 10 '17 at 15:30
  • so i only need to update android studio on 3.0 and the problem will be solved? ok i do that,thanx a lot man! – bojan Nov 10 '17 at 15:55
  • I update android studio on 3.0 and now everything works perfect,with version 1.1.4,no more memory leak with instantsearch! – bojan Nov 11 '17 at 02:10
  • @bojan I edited your question and removed the APP_ID and the API_KEY from the code. You should never post those publically and I advise you to revoke your API key and use a new one. – Robert D. Mogos Nov 11 '17 at 14:01