【问题标题】:Application crashes because it takes too much memory?应用程序崩溃是因为它占用了太多内存?
【发布时间】:2026-02-18 07:00:01
【问题描述】:

我即将完成我的申请,最近我遇到了一个我无法解决的问题。 我的应用程序包含 5 个布局,每个布局有 1 个活动,5 个布局。 现在,自从我在我的应用程序中添加广告后,我的应用程序就表示在 ram 内存上占用了巨大的空间,并且在切换了几个布局后它崩溃了。 我的应用程序大小为 10.8mb,但在手机上它最多需要 75mb 的 Ram 内存。 这是我从 logcat 得到的警告:http://prntscr.com/2naqr8 这是应用程序崩溃时的 Logcat:http://prntscr.com/2naret 第二部分:http://prntscr.com/2narhi 第三部分:http://prntscr.com/2narhi

现在我的代码非常简单,第一个活动是启动画面,第二个是菜单,其他 3 个是相同的代码。 菜单代码:

package puske.com;
import com.google.ads.AdRequest;
import com.google.ads.AdView;

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

public class Menu extends Activity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu);
    AdView myAdView = (AdView)findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest();
    myAdView.loadAd(adRequest);




Button button1=(Button)findViewById(R.id.menu1);
button1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        startActivity(new Intent(Menu.this, Rifles.class));

        finish();

    }
});


}

在此之后,它会为其他 2 个活动重复 2 次。 我的其他 3 个活动都是一样的,只是资源不同。

    package puske.com;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class Pistols extends Activity {
    private SeekBar volumeSeekbar = null;
    private AudioManager audioManager = null;
    MediaPlayer mp, mp2, mp3, mp4, mp5, mp6;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pistols);
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        initControls(); 

        mp=MediaPlayer.create(this, R.raw.glock);
        ImageButton abt1 = (ImageButton) findViewById(R.id.abt1);
        abt1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if (mp2.isPlaying()){
                mp2.pause();
                mp2.seekTo(0);
            }
            if (mp3.isPlaying()){
                mp3.pause();
                mp3.seekTo(0);
            }
            if (mp4.isPlaying()){
                mp4.pause();
                mp4.seekTo(0);
            }
            if (mp5.isPlaying()){
                mp5.pause();
                mp5.seekTo(0);

            }
            if (mp6.isPlaying()){
                mp6.pause();
                mp6.seekTo(0);

            }
            if (mp.isPlaying()){
                mp.pause();
                mp.seekTo(0);
            }
            else{
                mp.start();
            }
            }

    });

  @Override
protected void onDestroy() {

    super.onDestroy();
     // explicitly release media player

    unbindDrawables(findViewById(R.id.pistolz));
    System.gc();
}

/**
 * Unbinds all drawables in a given view (and its child tree).
 * 
 * @param findViewById     Root view of the tree to unbind
 */
private void unbindDrawables(View view) {
    if (view.getBackground() != null) {
        view.getBackground().setCallback(null);
    }

    if (view instanceof ViewGroup) {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            unbindDrawables(((ViewGroup) view).getChildAt(i));
        }
        try
        {
            ((ViewGroup) view).removeAllViews();
        }
        catch(UnsupportedOperationException ignore)
        {
            //if can't remove all view (e.g. adapter view) - no problem 
        }
    if(mp!=null){
        mp.stop();
        mp.release();
        mp = null;

}
    if(mp2!=null){
        mp2.stop();
        mp2.release();
        mp2 = null;

}
    if(mp3!=null){
        mp3.stop();
        mp3.release();
        mp3 = null;

}
    if(mp4!=null){
        mp4.stop();
        mp4.release();
        mp4 = null;

}
    if(mp5!=null){
        mp5.stop();
        mp5.release();
        mp5 = null;


}


    if(mp6!=null){
        mp6.stop();
        mp6.release();
        mp6 = null;

}
    findViewById(R.id.pistolz).setVisibility(View.GONE);




}
}
@Override
public void onBackPressed() {
    super.onBackPressed();
    startActivity(new Intent(Pistols.this, Menu.class));
    this.finish();
     }
 }

就是这样,整个代码我解除了所有可绘制对象的绑定,释放 MP 和活动,但仍然在设备上占用太多空间并崩溃。有谁知道这可能是什么原因造成的? 最后是我的 XML,同样,除了 IDS 和资源之外的所有布局都是一样的

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@color/black"
android:id="@+id/riflez" >
<SeekBar
    android:id="@+id/sikbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="48dp"/>
<ImageButton
    android:id="@+id/bt1"
    android:layout_width="130dp"
    android:layout_height="90dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="@null"
    android:scaleType="fitXY"
    android:src="@drawable/emka" />
<ImageButton
    android:id="@+id/bt2"
    android:layout_width="130dp"
    android:layout_height="90dp"
    android:layout_alignRight="@+id/sikbar"
    android:layout_alignTop="@+id/bt1"
    android:background="@null"
    android:scaleType="fitXY"
    android:src="@drawable/kalas" />

<ImageButton
    android:id="@+id/bt3"
    android:layout_width="130dp"
    android:layout_height="90dp"
    android:layout_alignLeft="@+id/bt1"
    android:layout_below="@+id/bt1"
    android:layout_marginTop="26dp"
    android:background="@null"
    android:scaleType="fitXY"
    android:src="@drawable/famas" />
<ImageButton
    android:id="@+id/bt4"
    android:layout_width="130dp"
    android:layout_height="90dp"
    android:layout_alignLeft="@+id/bt2"
    android:layout_alignTop="@+id/bt3"
    android:background="@null"
    android:scaleType="fitXY"
    android:src="@drawable/hekler" />
<ImageButton
    android:id="@+id/bt5"
    android:layout_width="130dp"
    android:layout_height="90dp"
    android:layout_alignLeft="@+id/bt3"
    android:layout_below="@+id/bt3"
    android:layout_marginTop="26dp"
    android:background="@null"
    android:scaleType="fitXY"
    android:src="@drawable/sporet" />
<ImageButton
    android:id="@+id/bt6"
    android:layout_width="130dp"
    android:layout_height="90dp"
    android:layout_alignLeft="@+id/bt4"
    android:layout_alignTop="@+id/bt5"
    android:background="@null"
    android:scaleType="fitXY"
    android:src="@drawable/uzi" />
    <com.google.ads.AdView
    android:id="@+id/adView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    ads:adSize="BANNER"
    ads:adUnitId="THE ID"
    ads:loadAdOnCreate="true" >
</com.google.ads.AdView>

【问题讨论】:

    标签: android memory crash admob


    【解决方案1】:

    加载大型位图时通常会发生此错误。您的 ImageButtons 的可绘制对象是高分辨率的吗?如果是这样,这很可能是错误。您应该尝试将它们下采样到适当的分辨率,但为了快速修复,在 AndroidManifest.xml 文件的 &lt;application&gt; 标记下添加 android:largeHeap="true" 可以让您的应用程序加载大图像而不会出现内存不足错误。

    如果以上都不起作用,那么问题出在代码中的其他地方。但从个人经验来看,位图是最有可能导致简单代码因内存问题而崩溃的东西。

    【讨论】:

    • 你是最棒的! android:largeHeap="true" 修复了它!是的,我有很大的可绘制对象,650x350 但 android:largeHeap="true" 完全修复了它!非常感谢!
    • 当我遇到问题时,它总是对我有用。它似乎发生了很多。很高兴知道我能帮上忙:)