Can somebody tell me why am I having these memory leaks? It happens when splash screen needs to finish itself (basically call a new activity) And again it happens when I'm opening a new page (new activity) on a button click in the menu.
Log cat memory leaks :
Splash screen to main menu :
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
Main menu to a new page :
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
Here is my main :
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));
}
});
}
}
I can't figure out where is the problem? Please don't tell me to increase the heap size. Thank you all in advance!
I don't see any leak here unless you hold a reference to one of these activity somewhere else. For example MPlayer
is not private or protected so if you hold a reference to it in another instance you might have a leak.
It is normal to have garbage collection when the MediaPlayer
is involved. It can take a lot of memory
Your MainActivity
stays in memory even if a new Activity is started. If you don't want that call finish();
after startActivity();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.