33

After a lot of debugging I finally found what is causing this error! Garbage Collection!

I have a video playing in media view and in the background I am looking for new videos from a Rest API.

Every now and then I see the Garbage collection running:

02-22 13:14:57.969: D/dalvikvm(16888): GC_EXPLICIT freed 152K, 4% free 6746K/6979K, paused 2ms+2ms

And the straight after that:

02-22 13:14:57.969: W/MediaPlayer-JNI(16888): MediaPlayer finalized without being released

So I tested it by calling System.gc() every 5 seconds.

As soon as the first GC is called it happens!

02-22 13:19:47.813: D/dalvikvm(17060): GC_EXPLICIT freed 167K, 5% free 6745K/7047K, paused 2ms+2ms ---- I call GC
02-22 13:19:47.813: W/MediaPlayer-JNI(17060): MediaPlayer finalized without being released ---- VIDEO PLAY INTERRUPTED

Why does this happen? Can I prevent it?

Playing the video:

private void playMedia(int playListIndex) throws IOException {
        File mediadir = getDir("tvr", Context.MODE_PRIVATE);
        filelist = mediadir.listFiles();
        Log.i("media player", "play media!");
        String path = filelist[playListIndex].getAbsolutePath();
        FileInputStream fileInputStream = new FileInputStream(path);
        final Uri uri = Uri.parse(path);
        String filename = filelist[playListIndex].getName();
        if (filename.contains("image")) {
            imageView = (ImageView)findViewById(R.id.imageView);
            imageView.setVisibility(View.VISIBLE);
            imageView.setImageURI(uri);
            mHandler.postDelayed(new Runnable() {
                public void run() {
                    imageView.setVisibility(View.GONE);
                    imageView.setImageURI(uri);
                    onCompletion(null);
                }
            }, 4000);
        } else if (filename.contains("video")) {

            MediaPlayer pl = new MediaPlayer();
            pl.setOnCompletionListener(this);
            pl.setDisplay(holder);
            pl.setDataSource(fileInputStream.getFD());
            pl.prepare();
            pl.start();
        }
    }

And when it is done:

@Override
    public void onCompletion(MediaPlayer mp) {
        Log.i("media player", "play next please!");
        if (mp != null) {
            mp.release();
        }
//      play next video
        currentMedia++;
        if (currentMedia > playList.size() - 1) {
            currentMedia = 0;
        }
        try {
            playMedia(currentMedia);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
4

2 回答 2

72

我认为这是因为您在方法范围内创建了媒体播放器,因此,当方法完成时,它超出了范围。这意味着没有引用,因此可以进行垃圾收集。

这意味着,它甚至可以在调用 onCompletion 之前被 GC 释放,因此在清除之前不会释放。相反,您需要将媒体播放器的引用作为成员变量存储在您的类中。

于 2013-02-22T11:43:46.973 回答
11

Java GC 只管理内存。所以当使用其他资源(例如文件、套接字等)时,您需要手动管理它们。对于 MediaPlayer,文档提到:

还建议一旦不再使用 MediaPlayer 对象,立即调用 release() 以便与 MediaPlayer 对象关联的内部播放器引擎使用的资源可以立即释放。资源可能包括单例资源,例如硬件加速组件,调用 release() 失败可能会导致 MediaPlayer 对象的后续实例回退到软件实现或完全失败。

因此,当您完成 MediaPlayer 实例时,您需要确保在该实例上显式调用 release()。执行此操作的好地方可能是包含 Activity 的生命周期方法,例如 onDestroy()。否则,当该 MediaPlayer 实例最终被垃圾回收时(在您不再引用它之后的某个任意时间),终结器会注意到您从未调用 release() 并将输出您看到的警告。

于 2013-02-22T11:30:32.970 回答