1

有人能告诉我为什么会有这些内存泄漏吗?当启动画面需要完成自身(基本上称为新活动)时会发生这种情况,当我在菜单中单击按钮打开新页面(新活动)时会再次发生这种情况。

记录猫内存泄漏:

闪屏到主菜单:

12-07 19:35:58.037: D/dalvikvm(2167): GC_CONCURRENT freed 499K, 21% free 4138K/5228K, paused 8ms+8ms, total 397ms
12-07 19:35:58.045: D/dalvikvm(2167): WAIT_FOR_CONCURRENT_GC blocked 170ms
12-07 19:35:58.116: I/dalvikvm-heap(2167): Grow heap (frag case) to 5.208MB for 1106044-byte allocation
12-07 19:35:58.365: D/dalvikvm(2167): GC_CONCURRENT freed 3K, 18% free 5215K/6312K, paused 8ms+33ms, total 246ms

主菜单到新页面:

12-07 19:38:30.974: D/dalvikvm(2167): GC_FOR_ALLOC freed 512K, 17% free 5272K/6304K, paused 119ms, total 151ms
12-07 19:38:31.034: I/dalvikvm-heap(2167): Grow heap (frag case) to 6.316MB for 1106044-byte allocation
12-07 19:38:31.376: D/dalvikvm(2167): GC_CONCURRENT freed 1129K, 30% free 5224K/7388K, paused 89ms+5ms, total 332ms

这是我的主要内容:

package com.example.prva;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MotionEvent;


public class MainActivity extends Activity {

    MediaPlayer MPlayer; //da MPlayer mozemo koristiti bilo gdje
    protected boolean splashactive = true;
    protected int splashtime = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);        

        MPlayer = MediaPlayer.create(this, R.raw.splash); //dodavanje zvuka MPlayeru
        MPlayer.start();        //play

        Thread tajmer = new Thread(){
            public void run(){
                try{        
                    while(splashactive && splashtime<3000)
                        {
                            sleep(50);
                            splashtime=splashtime+50;
                        }
                    }
                 catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }               
                finally{
                    finish();                   
                    MPlayer.release();
                    startActivity(new Intent(MainActivity.this, Meni_Splash.class));                
                }       
            }           
        };
        tajmer.start(); 
    }       

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            splashactive = false;           

        }       
        return true;
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        MPlayer.release();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        MPlayer.pause();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        MPlayer.start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

Meni_Splash:

package com.example.prva;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Meni_Splash extends Activity{  


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);     

        Button btnv = (Button) findViewById(R.id.buttonv);
        btnv.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent(Meni_Splash.this, button.class));              
            }
        });         
    }
}

我无法弄清楚问题出在哪里?请不要告诉我增加堆大小。谢谢大家!

4

1 回答 1

1

除非您在其他地方持有对其中一项活动的引用,否则我在这里看不到任何泄漏。例如MPlayer,它不是私有的或受保护的,因此如果您在另一个实例中持有对它的引用,您可能会发生泄漏。

涉及到垃圾收集是正常的MediaPlayer。可能会占用大量内存

MainActivity即使开始了新的活动,您也会留在记忆中。如果你不想要那个电话finish();之后startActivity();

于 2012-12-07T20:42:41.100 回答