使用Firebase和Facebook身份验证,分配的内存正在快速增长

时间:2016-06-22 21:01:55

标签: android memory-leaks firebase firebase-authentication android-memory

我正在尝试使用Facebook和Firebase存档简单的身份验证。但不知怎的,我分配的内存正在疯狂增加。我不知道究竟是什么原因造成的..请问有什么帮助我吗?

很难看到,但分配的大小正在增加,约为0.2 MB / s

memory monitor

MainActivity

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private Intent navigationIntent;

    private SharedPreferences sharedPreferences;

    // FIREBASE
    private FirebaseAPI firebaseAPI;
    private FirebaseAuth firebaseAuth;
    private FirebaseAuth.AuthStateListener firebaseAuthListener;

    // FACEBOOK
    private CallbackManager FBcallbackManager;


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

        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

        navigationIntent = new Intent(this, NavigationActivity.class);

        // Initialize Firebase
        firebaseAuth = FirebaseAuth.getInstance();

        firebaseAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();

                if (user != null) {
                    user.getToken(false).addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {
                        @Override
                        public void onComplete(@NonNull Task<GetTokenResult> task) {
                            sharedPreferences.edit()
                                    .putString("token", task.getResult().getToken())
                                    .apply();

                            Profile.fetchProfileForCurrentAccessToken();
                            firebaseAPI = new FirebaseAPI();
                            firebaseAPI.writeUserProfile(Profile.getCurrentProfile());

                            startHomeActivity();
                        }
                    });
                }
            }
        };

        // Initialize Facebook Login button
        FBcallbackManager = CallbackManager.Factory.create();
        final LoginButton loginButton = (LoginButton) findViewById(R.id.facebook_button);
        loginButton.setReadPermissions("email", "public_profile");
        loginButton.registerCallback(FBcallbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                handleFacebookAccessToken(loginResult.getAccessToken());
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onError(FacebookException error) {

            }
        });


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        FBcallbackManager.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onResume() {
        super.onResume();
        firebaseAuth.addAuthStateListener(firebaseAuthListener);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (firebaseAuthListener != null) {
            firebaseAuth.removeAuthStateListener(firebaseAuthListener);
        }

    }

    private void handleFacebookAccessToken(AccessToken accessToken) {

        final AuthCredential authCredential = FacebookAuthProvider.getCredential(accessToken.getToken());

        firebaseAuth.signInWithCredential(authCredential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {

            }
        });

    }


    private void startHomeActivity() {
        if (navigationIntent != null) {

            startActivity(navigationIntent);
            finish();
        }
    }

应用

public class App extends Application {

    private static Context context;

    public static Context getContext() {
        return context;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        FacebookSdk.sdkInitialize(getApplicationContext());
        AppEventsLogger.activateApp(this);
        App.context = getApplicationContext();
    }
}

build.gradle(app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24"

    defaultConfig {
        applicationId "com.xxxxx.xxxxxxxxxx"
        minSdkVersion 16
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

repositories {
    mavenCentral()
    flatDir {
        dirs 'libs'
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.facebook.android:facebook-android-sdk:4.13.1'
    compile 'com.google.code.gson:gson:2.7'
    compile 'com.google.firebase:firebase-database:9.0.2'
    compile 'com.google.firebase:firebase-auth:9.0.2'
    compile 'com.google.firebase:firebase-crash:9.0.2'
    compile 'com.squareup.okhttp3:okhttp:3.3.1'
    compile 'com.google.android.gms:play-services-location:9.0.2'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.android.support:recyclerview-v7:24.0.0'
    compile 'com.android.support:cardview-v7:24.0.0'
    compile 'com.android.support:design:24.0.0'
    compile 'com.android.support:appcompat-v7:24.0.0'
    compile 'com.android.support:preference-v14:24.0.0'
}

apply plugin: 'com.google.gms.google-services'

build.gradle(project)

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
        classpath 'com.google.gms:google-services:3.0.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }


}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

2 个答案:

答案 0 :(得分:0)

猜测:尝试从构建中删除崩溃报告。 documentation describes a known issue会影响子类Application的应用。目前尚不清楚您在App.onCreate()方法中执行的初始化操作是什么,但正如已知问题说明所解释的那样,处理将执行两次:一次针对您的应用流程,一次针对崩溃报告流程。

答案 1 :(得分:0)

完全删除我的项目并从Version Control重建后,内存错误消失了..(: