gpt4 book ai didi

java - 查看 android 监视器图时,.release() 不会减少我的内存

转载 作者:行者123 更新时间:2023-11-30 01:04:44 25 4
gpt4 key购买 nike

我已将 .release() 方法添加到我的脚本中并运行了 android 模拟器,在运行时我查看了我的 android 显示器并开始点击 TextView 播放声音。

但是当声音结束时没有释放内存,我做错了什么?

package com.example.android.miwok;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;

public class NumbersActivity extends AppCompatActivity {

private MediaPlayer mMediaPlayer;

private MediaPlayer.OnCompletionListener mCompletionListerner = new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
releaseMediaPlayer();
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.word_list);

final ArrayList<Word> words = new ArrayList<>(); // <-- Creates list

words.add(new Word("one", "lutti", R.raw.number_one, R.drawable.number_one));
words.add(new Word("two", "otiiko", R.raw.number_two, R.drawable.number_two));
words.add(new Word("three", "tolookosu", R.raw.number_three, R.drawable.number_three));
words.add(new Word("four", "oyyisa", R.raw.number_four, R.drawable.number_four));
words.add(new Word("five", "massokka", R.raw.number_five, R.drawable.number_five));
words.add(new Word("six", "temmokka", R.raw.number_six, R.drawable.number_six));
words.add(new Word("seven", "kenekaku", R.raw.number_seven, R.drawable.number_seven));
words.add(new Word("eight", "kawinta", R.raw.number_eight, R.drawable.number_eight));
words.add(new Word("nine", "wo'e", R.raw.number_nine, R.drawable.number_nine));
words.add(new Word("ten", "na'aacha", R.raw.number_ten, R.drawable.number_ten));

WordAdapter adapter = new WordAdapter(this, words, R.color.category_numbers);


ListView listView = (ListView) findViewById(R.id.list); // <-- id the Listview
listView.setAdapter(adapter);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
releaseMediaPlayer();
Word word = words.get(position);
mMediaPlayer = MediaPlayer.create(NumbersActivity.this, word.getAudioResourceId());
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(mCompletionListerner);
}
});
}

private void releaseMediaPlayer() {
if (mMediaPlayer != null) {
mMediaPlayer.release();
mMediaPlayer = null;
}
}
}

最佳答案

Android(更普遍的是 Java)使用 Garbage Collector系统。

此收集器不会立即“收集”空闲内存,您无法准确决定它何时通过!因此,当您 .release() 一个对象或其他任何东西时,您将不得不等到 GC 决定是时候恢复了。

释放引用的唯一效果是告诉 GC 这个对象可以被垃圾回收...仅此而已 ;)

所以,回答你的问题:你没有做错任何事,你只需要等待并希望 GC。

关于java - 查看 android 监视器图时,.release() 不会减少我的内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38984220/

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