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

为什么我的Android应用程序占用了这么多内存?,android,xml,memory-management,android-activity,Android,Xml,Memory Management,Android Activity,我有一个Android应用程序,它有一个非常简单的主活动。它只有两个按钮,一个文本视图和一个编辑文本字段。我启动主活动(我什么都不做,只是从应用程序抽屉启动它),然后我按home按钮进入我的主屏幕,进入settings>appmanager>running>cached background Process,在那里我看到我的应用程序消耗的RAM比其他缓存的后台进程多,大约8 MB。这正常吗?为什么它比其他应用吃得更多?这是我的代码: 主要活动: package com.example.myapp

我有一个Android应用程序,它有一个非常简单的主活动。它只有两个按钮,一个文本视图和一个编辑文本字段。我启动主活动(我什么都不做,只是从应用程序抽屉启动它),然后我按home按钮进入我的主屏幕,进入settings>appmanager>running>cached background Process,在那里我看到我的应用程序消耗的RAM比其他缓存的后台进程多,大约8 MB。这正常吗?为什么它比其他应用吃得更多?这是我的代码:

主要活动:

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>


有人能告诉我为什么它吃了这么多公羊吗?提前谢谢

你考虑过用这个吗?没有,但似乎有帮助,我试试这个。所有这些文章都很有帮助。非常感谢你们!对不起,如果我是哑巴,我想问最后一个问题。在DDMS中,我看到每当我在服务中启动一个新线程时,该线程都会被分配一个新ID,并且ID永远不会被重用,即使我停止服务并再次启动它。我的意思是从12到50。这正常吗?或者这意味着以前的线程永远不会关闭?