Java Android OutOfMemoryError,如何使用DDMS进行调试并跟踪导致内存泄漏的原因?

Java Android OutOfMemoryError,如何使用DDMS进行调试并跟踪导致内存泄漏的原因?,java,android,android-studio,memory-leaks,ddms,Java,Android,Android Studio,Memory Leaks,Ddms,我认为我没有遵循android应用程序内存优化的标准通用规则,我一直在摆脱内存错误,现在我正试图在android Studio中使用DDMS调试和查找源代码,但我不知道如何读取它,以及如何找到占用最大空间的对象。下面是我的代码 选择性活动 public class ChooseLevelActivity extends Activity implements View.OnClickListener { ImageView level001, level002, level003, l

我认为我没有遵循android应用程序内存优化的标准通用规则,我一直在摆脱内存错误,现在我正试图在android Studio中使用DDMS调试和查找源代码,但我不知道如何读取它,以及如何找到占用最大空间的对象。下面是我的代码

选择性活动

public class ChooseLevelActivity extends Activity implements View.OnClickListener {

    ImageView level001, level002, level003, level004, level005;
    int last_level, best_move;
    String best_time, level_selected;
    TextView levelName, bestMove, bestTime, no_level_finished;
    Class <? extends Activity> classVariable;
    Button playBtn;

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

        level001 = (ImageView) findViewById(R.id.level001);
        level002 = (ImageView) findViewById(R.id.level002);
        level003 = (ImageView) findViewById(R.id.level003);
        level004 = (ImageView) findViewById(R.id.level004);
        level005 = (ImageView) findViewById(R.id.level005);

        levelName = (TextView) findViewById(R.id.level_name);
        bestMove = (TextView) findViewById(R.id.best_move);
        bestTime = (TextView) findViewById(R.id.best_time);
        no_level_finished = (TextView) findViewById(R.id.no_level_finished);
        playBtn = (Button) findViewById(R.id.playBtn);

        // First check if file exists
        File recordFile = getBaseContext().getFileStreamPath("levels.json");
        if (recordFile.exists()) {

            // Get the JSON Object from the data
            JSONObject parent = parseJSONData("levels.json");

            // Array of ImageView
            final ImageView[] levelsArray = {level001, level002, level003, level004, level005};

            // This will store all the values inside "best_move and time" in an element string
            try {
                last_level = parent.getInt("level");
            } catch (JSONException e) {
                e.printStackTrace();
            }

            for(int i = 0; i < last_level; i++) {
                levelsArray[i].setVisibility(View.VISIBLE);
                levelsArray[i].setOnClickListener(this);
            }

        } else {
            no_level_finished.setVisibility(View.VISIBLE);
            playBtn.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.level001:
                level_selected = "level001";
                classVariable = Level001Activity.class;
                break;
            case R.id.level002:
                level_selected = "level002";
                classVariable = Level002Activity.class;
                break;
            case R.id.level003:
                level_selected = "level003";
                classVariable = Level003Activity.class;
                break;
            case R.id.level004:
                level_selected = "level004";
                classVariable = Level004Activity.class;
                break;
            case R.id.level005:
                level_selected = "level005";
                classVariable = Level005Activity.class;
                break;
        }

        String fileName = "record_" + level_selected + ".json";

        // Get the JSON Object from the data
        JSONObject parents = parseJSONData(fileName);

        // This will store all the values inside "best_move and time" in an element string
        try {
            best_move = parents.getInt("best_move");
            best_time = parents.getString("best_time");
        } catch (JSONException e) {
           // e.printStackTrace();
        }

        levelName.setText("Level " + level_selected.substring(5));
        bestMove.setText("Best Move: " + best_move);
        bestTime.setText("Best Time: " + best_time);

        playBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getApplicationContext(),
                        classVariable);
                startActivity(i);
                finish();
            }
        });

    }

    public JSONObject parseJSONData(String file) {
        String JSONString = null;
        JSONObject jsonObject = null;
        try {
            // Open the inputStream to the file
            FileInputStream fin = openFileInput(file);

            int sizeOfJSONFile = fin.available();

            // array that will store all the data
            byte[] bytes = new byte[sizeOfJSONFile];

            // reading data into the array from the file
            fin.read(bytes);

            // close the input stream
            fin.close();

            JSONString = new String(bytes, "UTF-8");
            jsonObject = new JSONObject(JSONString);

        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } catch (JSONException x) {
            x.printStackTrace();
            return null;
        }
        return jsonObject;
    }
}
公共类ChooseLevel活动扩展活动实现View.OnClickListener{
图像视图标高001、标高002、标高003、标高004、标高005;
int最后一级,最佳移动;
字符串最佳时间,所选级别;
text查看级别名称、最佳移动、最佳时间、未完成级别;
班
DDMS堆选项卡


我如何阅读DDMS并利用它来发现占用如此多内存的问题?请告诉我是否还有其他需要调试的内容,例如DDMS中的分配跟踪器或线程选项卡。感谢您提供的所有帮助,非常感谢。

在我看来,使用DDMS对于开发人员来说很难检测到,而且效率低下应用程序的内存泄漏。
我建议您使用Square的。该库专门用于检测java和android的内存泄漏,您可以从该项目中了解更多信息。

您找到这些资源了吗?是的,这是关于如何使用它的手册,但我在尝试将其连接到我的项目时感到困惑,因此,如果有人能找到更好的方法查找故障,将是一个好主意帮了大忙。谢谢你,我马上就要看youtube了。谢谢你的建议,现在就看吧。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="You have not finish any level yet."
        android:id="@+id/no_level_finished"
        android:visibility="gone"
        />

    <RelativeLayout
        android:id="@+id/level_completed"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="15dp"
        android:paddingBottom="15dp">

        <ImageView
            android:id="@+id/level001"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@drawable/smiley"
            android:visibility="gone"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            />

        <ImageView
            android:id="@+id/level002"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@drawable/heaven_full"
            android:layout_toRightOf="@+id/level001"
            android:visibility="gone"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            />

        <ImageView
            android:id="@+id/level003"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@drawable/smiley"
            android:layout_toRightOf="@+id/level002"
            android:visibility="gone"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            />

        <ImageView
            android:id="@+id/level004"
            android:layout_width="50dp"
            android:layout_height="100dp"

            android:layout_toRightOf="@+id/level003"
            android:visibility="gone"
            android:layout_marginLeft="35dp"
            android:layout_marginRight="35dp"
            />

        <ImageView
            android:id="@+id/level005"
            android:layout_width="100dp"
            android:layout_height="100dp"

            android:layout_toRightOf="@+id/level004"
            android:visibility="gone"
            />

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/level_desc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/level_completed"
        android:paddingLeft="15dp"
        android:paddingTop="15dp"
        android:paddingBottom="15dp"
        >

        <TextView
            android:id="@+id/level_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

       <TextView
            android:id="@+id/best_move"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/level_name"/>

        <TextView
            android:id="@+id/best_time"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/best_move"
            android:layout_marginBottom="15dp"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/playBtn"
            android:layout_below="@+id/best_time"
            android:text="Play Level"/>

    </RelativeLayout>

</RelativeLayout>