这是正常的记忆行为吗?

时间:2016-05-21 14:21:06

标签: java android memory memory-leaks garbage-collection

当我的Android应用程序空闲时运行(即什么都不做)。分配的内存慢慢增加,直到没有更多的“免费”内存,当它达到上限时,我认为内存会被垃圾收集。

请查看下面的图片。

memory over time

MainActivity.class

public class MainActivity extends AppCompatActivity {

    private final String TAG = "MainActivity";

    // FIREBASE
    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);

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

        // Initialize Facebook Login button
        FBcallbackManager = CallbackManager.Factory.create();
        LoginButton loginButton = (LoginButton) findViewById(R.id.facebookButton);
        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) {

            }
        });

        final Intent intent = new Intent(MainActivity.this, NavigationActivity.class);

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

                if (user != null) {

                    Log.d(TAG, user.getDisplayName());

                    // User is signed in
                    // if so start next activty and close this one
                    Log.d(TAG, "User is logged in");

                    startActivity(intent);
                    finish();
                } else {
                    // User is signed out
                    Log.d(TAG, "User is logged out");
                }

            }
        };


    }

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

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

    @Override
    protected void onStop() {
        super.onStop();

        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) {

                if (task.isSuccessful()) {
                    Log.d(TAG, "Authentication successful");
                } else {
                    Log.d(TAG, "Authentication not successful");
                    Log.d(TAG, task.getException().getMessage());
                }

            }
        });
}
}

HPROF

hprof

MAT给我这些问题:

  • 问题疑似1

74.846个“java.lang.String”实例,由“”加载占用6.777.960(29,27%)个字节。

  • 问题疑似2

33个“java.lang.DexCache”实例,由“”加载占用5.293.808(22,86%)个字节。

  • 问题疑似3

“long []”的19个实例,由“”加载占用3.970.432(17,15%)个字节。

  • 问题可疑4

5.082个“java.lang.Class”实例,由“”加载占用2.811.328(12,14%)个字节。

1 个答案:

答案 0 :(得分:0)

不,这不正常。听起来好像你一直在创造新的物体。