为什么我的Android应用程序吃了这么多内存?

时间:2014-04-18 17:37:08

标签: android xml memory-management android-activity

我有一个Android应用程序,主要活动非常简单。它只有两个按钮,一个textview和一个edittext字段。我启动了主要活动(我不做任何事情,只是从应用程序抽屉中启动它),然后按主页按钮进入我的主屏幕,转到设置> appmanager>正在运行>缓存的后台进程,我看到我的应用程序比其他缓存的后台进程占用更多的RAM,大约8 MB的RAM。这是正常的吗?为什么它比其他应用程序吃得更多?这是我的代码:

MainActivity:

package com.example.myapp;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

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

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }

    ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    int memoryClass = am.getMemoryClass();
    long maxMemory = Runtime.getRuntime().maxMemory();
    long totalMemory = Runtime.getRuntime().totalMemory();
    Log.i("ilog", "memoryClass: " + memoryClass);
    Log.i("ilog", "maxMemory: " + maxMemory);
    Log.i("ilog", "totalMemory: " + totalMemory);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    private Button button1;
    private Button button2;
    private TextView textView1;
    private EditText editText1;
    private ComponentName cn;
    private String disable = "Disable app";
    private String enable = "Enable app";
    private int enabledState;
    private PackageManager pm;

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        button1 = (Button) rootView.findViewById(R.id.button1);
        editText1 = (EditText) rootView.findViewById(R.id.editText1);
        textView1 = (TextView) rootView.findViewById(R.id.textView1);
        button2 = (Button) rootView.findViewById(R.id.button2);


        cn = new ComponentName(getActivity(), CallReceiver.class);
        pm = getActivity().getPackageManager();
        enabledState = pm.getComponentEnabledSetting(cn);

        if (enabledState == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
            button1.setText(disable);
        } else {
            button1.setText(enable);
        }

        button1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                enabledState = pm.getComponentEnabledSetting(cn);
                if (enabledState == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
                    pm.setComponentEnabledSetting(cn,
                            PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                            PackageManager.DONT_KILL_APP);
                    button1.setText(enable);
                } else {
                    pm.setComponentEnabledSetting(cn,
                            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                            PackageManager.DONT_KILL_APP);
                    button1.setText(disable);
                }
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (editText1.getText().toString().length() > 0) {
                    Intent callImitateIntent = new Intent(getActivity(), CallReceiver.class);
                    callImitateIntent.putExtra(TelephonyManager.EXTRA_STATE, TelephonyManager.EXTRA_STATE_RINGING);
                    callImitateIntent.putExtra(TelephonyManager.EXTRA_INCOMING_NUMBER, editText1.getText().toString());
                    getActivity().sendBroadcast(callImitateIntent);
                } else textView1.setText("Invalid incoming number");
            }
        });

        return rootView;
    }
}

@Override
public void onDestroy() {
    super.onDestroy();

    Intent serviceStopIntent = new Intent(this, WindowService.class);
    stopService(serviceStopIntent);
}

}

fragment_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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"
tools:context="com.example.myapp.MainActivity$PlaceholderFragment" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="@string/welcome" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp"
    android:maxLength="12"
    android:hint="@string/edittext1" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp"
    android:text="@string/button2" />

</RelativeLayout>

所以有人能告诉我为什么它吃了这么多内存?提前谢谢!

0 个答案:

没有答案