gpt4 book ai didi

java - Android 内存泄漏堆 gc_concurrent

转载 作者:行者123 更新时间:2023-11-30 03:53:21 24 4
gpt4 key购买 nike

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

记录猫内存泄漏:

启动画面到主菜单:

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

}

美尼飞溅:

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

我想不通问题出在哪里?请不要告诉我增加堆大小。提前谢谢大家!

最佳答案

除非您在其他地方持有对这些 Activity 之一的引用,否则我在这里看不到任何泄漏。例如 MPlayer 不是私有(private)的或 protected ,所以如果您在另一个实例中持有对它的引用,您可能会发生泄漏。

当涉及MediaPlayer 时,有垃圾收集是正常的。它会占用大量内存

即使启动了新的 Activity ,您的MainActivity 也会保留在内存中。如果您不希望在 startActivity();

之后调用 finish();

关于java - Android 内存泄漏堆 gc_concurrent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13769948/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com