N
N
Neonoviiwolf2019-02-25 15:30:46
Android
Neonoviiwolf, 2019-02-25 15:30:46

Possible memory leak and problem with LeakCanary?

A good
application eats 35mb at startup, turning the device from time to time, the memory size is slowly eaten up and stops at about 55mb, then the collector works and we get 47mb - it doesn’t seem to eat further, no matter how hard we try. I'm not sure if it's still a leak, I decided to install LeakCanary, but when the device is rotated, the application falls with it.
Here 's a solution, but I don't understand the answer

02-25 07:25:28.982 10235-10235/www.test_v2 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: www.test_v2, PID: 10235
    java.lang.RuntimeException: Unable to start activity ComponentInfo{www.test_v2/www.test_v2.MainActivity}: java.lang.UnsupportedOperationException: buildAndInstall() should only be called once.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3912)
        at android.app.ActivityThread.access$900(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.UnsupportedOperationException: buildAndInstall() should only be called once.
        at com.squareup.leakcanary.AndroidRefWatcherBuilder.buildAndInstall(AndroidRefWatcherBuilder.java:89)
        at com.squareup.leakcanary.LeakCanary.install(LeakCanary.java:43)
        at www.test_v2.MainActivity.onCreate(MainActivity.java:48)
        at android.app.Activity.performCreate(Activity.java:5933)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
        at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3912) 
        at android.app.ActivityThread.access$900(ActivityThread.java:144) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5221) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.MenuItem;

import com.facebook.stetho.Stetho;
import com.facebook.stetho.okhttp3.StethoInterceptor;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.tabs.TabLayout;
import com.squareup.leakcanary.LeakCanary;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.viewpager.widget.ViewPager;
import io.reactivex.Maybe;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import www.test_v2.DBRoom.DB.RoomDB;
import www.test_v2.DBRoom.DB.SRoomDB;
import www.test_v2.DBRoom.DB.table.app.menuSimpleFood.MenuSimpleFood;
import www.test_v2.mainFragment.TabsFragmentPageAdapter;
import www.test_v2.mainFragment.fAddProductMenu.AddProductInMenu;
import www.test_v2.mainFragment.fCreateMenu.CreateMenu;
import www.test_v2.mainFragment.fWatchProgress.MainFragmentWatchProgress;
import www.test_v2.network.DownloadStartApp;

public class MainActivity extends AppCompatActivity {

    private RoomDB db;

    @SuppressLint("CheckResult")
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (LeakCanary.isInAnalyzerProcess(this)) {
            // This process is dedicated to LeakCanary for heap analysis.
            // You should not init your app in this process.
            return;
        }

        LeakCanary.install(getApplication()); ////!!!! ругается тут
        // Normal app init code...


        Stetho.initializeWithDefaults(this);
        new OkHttpClient.Builder()
                .addNetworkInterceptor(new StethoInterceptor())
                .build();

LeakCanary.install(getApplication()); - dies here
in gradle

debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.6.3'
releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.6.3'
// Optional, if you use support library fragments:
debugImplementation 'com .squareup.leakcanary:leakcanary-support-fragment:1.6.3'

In general, I want to deal with leakcanary, which she does not like

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
illuzor, 2019-02-25
@Neonoviiwolf

They tell you in the error that you can't call buildAndInstall() more than once.
When the screen is rotated, the configuration changes and onCreate() is called, which calls LeakCanary.install().
You need to initialize LeakCanary in the Application, not in the Activity.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question